Blame view

net/mpls/internal.h 5.81 KB
b24413180   Greg Kroah-Hartman   License cleanup: ...
1
  /* SPDX-License-Identifier: GPL-2.0 */
0189197f4   Eric W. Biederman   mpls: Basic routi...
2
3
  #ifndef MPLS_INTERNAL_H
  #define MPLS_INTERNAL_H
9095e10ed   Jiri Benc   mpls: move mpls_h...
4
  #include <net/mpls.h>
0189197f4   Eric W. Biederman   mpls: Basic routi...
5

1511009cd   David Ahern   net: mpls: Increa...
6
7
8
9
  /* put a reasonable limit on the number of labels
   * we will accept from userspace
   */
  #define MAX_NEW_LABELS 30
0189197f4   Eric W. Biederman   mpls: Basic routi...
10
11
12
13
14
15
  struct mpls_entry_decoded {
  	u32 label;
  	u8 ttl;
  	u8 tc;
  	u8 bos;
  };
27d691056   Robert Shearman   mpls: Packet stats
16
17
18
19
  struct mpls_pcpu_stats {
  	struct mpls_link_stats	stats;
  	struct u64_stats_sync	syncp;
  };
03c57747a   Robert Shearman   mpls: Per-device ...
20
  struct mpls_dev {
27d691056   Robert Shearman   mpls: Packet stats
21
  	int				input_enabled;
24045a03b   David Ahern   net: mpls: Add su...
22
  	struct net_device		*dev;
27d691056   Robert Shearman   mpls: Packet stats
23
24
25
26
  	struct mpls_pcpu_stats __percpu	*stats;
  
  	struct ctl_table_header		*sysctl;
  	struct rcu_head			rcu;
03c57747a   Robert Shearman   mpls: Per-device ...
27
  };
27d691056   Robert Shearman   mpls: Packet stats
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
  #if BITS_PER_LONG == 32
  
  #define MPLS_INC_STATS_LEN(mdev, len, pkts_field, bytes_field)		\
  	do {								\
  		__typeof__(*(mdev)->stats) *ptr =			\
  			raw_cpu_ptr((mdev)->stats);			\
  		local_bh_disable();					\
  		u64_stats_update_begin(&ptr->syncp);			\
  		ptr->stats.pkts_field++;				\
  		ptr->stats.bytes_field += (len);			\
  		u64_stats_update_end(&ptr->syncp);			\
  		local_bh_enable();					\
  	} while (0)
  
  #define MPLS_INC_STATS(mdev, field)					\
  	do {								\
  		__typeof__(*(mdev)->stats) *ptr =			\
  			raw_cpu_ptr((mdev)->stats);			\
  		local_bh_disable();					\
  		u64_stats_update_begin(&ptr->syncp);			\
  		ptr->stats.field++;					\
  		u64_stats_update_end(&ptr->syncp);			\
  		local_bh_enable();					\
  	} while (0)
  
  #else
  
  #define MPLS_INC_STATS_LEN(mdev, len, pkts_field, bytes_field)		\
  	do {								\
  		this_cpu_inc((mdev)->stats->stats.pkts_field);		\
  		this_cpu_add((mdev)->stats->stats.bytes_field, (len));	\
  	} while (0)
  
  #define MPLS_INC_STATS(mdev, field)			\
  	this_cpu_inc((mdev)->stats->stats.field)
  
  #endif
0189197f4   Eric W. Biederman   mpls: Basic routi...
65
  struct sk_buff;
f8efb73c9   Roopa Prabhu   mpls: multipath r...
66
  #define LABEL_NOT_SPECIFIED (1 << 20)
f8efb73c9   Roopa Prabhu   mpls: multipath r...
67
68
  
  /* This maximum ha length copied from the definition of struct neighbour */
cf4b24f00   Robert Shearman   mpls: reduce memo...
69
70
  #define VIA_ALEN_ALIGN sizeof(unsigned long)
  #define MAX_VIA_ALEN (ALIGN(MAX_ADDR_LEN, VIA_ALEN_ALIGN))
f8efb73c9   Roopa Prabhu   mpls: multipath r...
71
72
73
74
75
76
77
78
79
80
81
82
83
84
  
  enum mpls_payload_type {
  	MPT_UNSPEC, /* IPv4 or IPv6 */
  	MPT_IPV4 = 4,
  	MPT_IPV6 = 6,
  
  	/* Other types not implemented:
  	 *  - Pseudo-wire with or without control word (RFC4385)
  	 *  - GAL (RFC5586)
  	 */
  };
  
  struct mpls_nh { /* next hop label forwarding entry */
  	struct net_device __rcu *nh_dev;
39eb8cd17   David Ahern   net: mpls: rt_nhn...
85
86
87
88
  
  	/* nh_flags is accessed under RCU in the packet path; it is
  	 * modified handling netdev events with rtnl lock held
  	 */
c89359a42   Roopa Prabhu   mpls: support for...
89
  	unsigned int		nh_flags;
f8efb73c9   Roopa Prabhu   mpls: multipath r...
90
91
92
  	u8			nh_labels;
  	u8			nh_via_alen;
  	u8			nh_via_table;
59b209667   David Ahern   net: mpls: change...
93
94
95
  	u8			nh_reserved1;
  
  	u32			nh_label[0];
f8efb73c9   Roopa Prabhu   mpls: multipath r...
96
  };
59b209667   David Ahern   net: mpls: change...
97
98
99
100
101
102
103
104
105
106
107
  /* offset of via from beginning of mpls_nh */
  #define MPLS_NH_VIA_OFF(num_labels) \
  		ALIGN(sizeof(struct mpls_nh) + (num_labels) * sizeof(u32), \
  		      VIA_ALEN_ALIGN)
  
  /* all nexthops within a route have the same size based on the
   * max number of labels and max via length across all nexthops
   */
  #define MPLS_NH_SIZE(num_labels, max_via_alen)		\
  		(MPLS_NH_VIA_OFF((num_labels)) +	\
  		ALIGN((max_via_alen), VIA_ALEN_ALIGN))
5b441ac87   Robert Shearman   mpls: allow TTL p...
108
109
110
111
112
  enum mpls_ttl_propagation {
  	MPLS_TTL_PROP_DEFAULT,
  	MPLS_TTL_PROP_ENABLED,
  	MPLS_TTL_PROP_DISABLED,
  };
cf4b24f00   Robert Shearman   mpls: reduce memo...
113
114
115
116
117
118
119
120
  /* The route, nexthops and vias are stored together in the same memory
   * block:
   *
   * +----------------------+
   * | mpls_route           |
   * +----------------------+
   * | mpls_nh 0            |
   * +----------------------+
59b209667   David Ahern   net: mpls: change...
121
   * | alignment padding    |   4 bytes for odd number of labels
cf4b24f00   Robert Shearman   mpls: reduce memo...
122
123
124
   * +----------------------+
   * | via[rt_max_alen] 0   |
   * +----------------------+
59b209667   David Ahern   net: mpls: change...
125
126
   * | alignment padding    |   via's aligned on sizeof(unsigned long)
   * +----------------------+
cf4b24f00   Robert Shearman   mpls: reduce memo...
127
128
   * | ...                  |
   * +----------------------+
59b209667   David Ahern   net: mpls: change...
129
130
   * | mpls_nh n-1          |
   * +----------------------+
cf4b24f00   Robert Shearman   mpls: reduce memo...
131
132
133
   * | via[rt_max_alen] n-1 |
   * +----------------------+
   */
f8efb73c9   Roopa Prabhu   mpls: multipath r...
134
135
136
137
  struct mpls_route { /* next hop label forwarding entry */
  	struct rcu_head		rt_rcu;
  	u8			rt_protocol;
  	u8			rt_payload_type;
cf4b24f00   Robert Shearman   mpls: reduce memo...
138
  	u8			rt_max_alen;
5b441ac87   Robert Shearman   mpls: allow TTL p...
139
  	u8			rt_ttl_propagate;
77ef013aa   David Ahern   net: mpls: Conver...
140
  	u8			rt_nhn;
39eb8cd17   David Ahern   net: mpls: rt_nhn...
141
142
143
  	/* rt_nhn_alive is accessed under RCU in the packet path; it
  	 * is modified handling netdev events with rtnl lock held
  	 */
77ef013aa   David Ahern   net: mpls: Conver...
144
  	u8			rt_nhn_alive;
59b209667   David Ahern   net: mpls: change...
145
146
147
  	u8			rt_nh_size;
  	u8			rt_via_offset;
  	u8			rt_reserved1;
f8efb73c9   Roopa Prabhu   mpls: multipath r...
148
149
150
151
  	struct mpls_nh		rt_nh[0];
  };
  
  #define for_nexthops(rt) {						\
59b209667   David Ahern   net: mpls: change...
152
153
  	int nhsel; struct mpls_nh *nh;  u8 *__nh;			\
  	for (nhsel = 0, nh = (rt)->rt_nh, __nh = (u8 *)((rt)->rt_nh);	\
f8efb73c9   Roopa Prabhu   mpls: multipath r...
154
  	     nhsel < (rt)->rt_nhn;					\
59b209667   David Ahern   net: mpls: change...
155
  	     __nh += rt->rt_nh_size, nh = (struct mpls_nh *)__nh, nhsel++)
f8efb73c9   Roopa Prabhu   mpls: multipath r...
156
157
  
  #define change_nexthops(rt) {						\
59b209667   David Ahern   net: mpls: change...
158
159
160
  	int nhsel; struct mpls_nh *nh; u8 *__nh;			\
  	for (nhsel = 0, nh = (struct mpls_nh *)((rt)->rt_nh),		\
  			__nh = (u8 *)((rt)->rt_nh);			\
f8efb73c9   Roopa Prabhu   mpls: multipath r...
161
  	     nhsel < (rt)->rt_nhn;					\
59b209667   David Ahern   net: mpls: change...
162
  	     __nh += rt->rt_nh_size, nh = (struct mpls_nh *)__nh, nhsel++)
f8efb73c9   Roopa Prabhu   mpls: multipath r...
163
164
  
  #define endfor_nexthops(rt) }
0189197f4   Eric W. Biederman   mpls: Basic routi...
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
  static inline struct mpls_shim_hdr mpls_entry_encode(u32 label, unsigned ttl, unsigned tc, bool bos)
  {
  	struct mpls_shim_hdr result;
  	result.label_stack_entry =
  		cpu_to_be32((label << MPLS_LS_LABEL_SHIFT) |
  			    (tc << MPLS_LS_TC_SHIFT) |
  			    (bos ? (1 << MPLS_LS_S_SHIFT) : 0) |
  			    (ttl << MPLS_LS_TTL_SHIFT));
  	return result;
  }
  
  static inline struct mpls_entry_decoded mpls_entry_decode(struct mpls_shim_hdr *hdr)
  {
  	struct mpls_entry_decoded result;
  	unsigned entry = be32_to_cpu(hdr->label_stack_entry);
  
  	result.label = (entry & MPLS_LS_LABEL_MASK) >> MPLS_LS_LABEL_SHIFT;
  	result.ttl = (entry & MPLS_LS_TTL_MASK) >> MPLS_LS_TTL_SHIFT;
  	result.tc =  (entry & MPLS_LS_TC_MASK) >> MPLS_LS_TC_SHIFT;
  	result.bos = (entry & MPLS_LS_S_MASK) >> MPLS_LS_S_SHIFT;
  
  	return result;
  }
27d691056   Robert Shearman   mpls: Packet stats
188
189
190
191
  static inline struct mpls_dev *mpls_dev_get(const struct net_device *dev)
  {
  	return rcu_dereference_rtnl(dev->mpls_ptr);
  }
face0188e   Roopa Prabhu   mpls: export mpls...
192
193
  int nla_put_labels(struct sk_buff *skb, int attrtype,  u8 labels,
  		   const u32 label[]);
a4ac8c986   David Ahern   net: mpls: bump m...
194
  int nla_get_labels(const struct nlattr *nla, u8 max_labels, u8 *labels,
a1f10abe1   David Ahern   net: Fill in exta...
195
  		   u32 label[], struct netlink_ext_ack *extack);
face0188e   Roopa Prabhu   mpls: export mpls...
196
197
198
  bool mpls_output_possible(const struct net_device *dev);
  unsigned int mpls_dev_mtu(const struct net_device *dev);
  bool mpls_pkt_too_big(const struct sk_buff *skb, unsigned int mtu);
27d691056   Robert Shearman   mpls: Packet stats
199
200
  void mpls_stats_inc_outucastpkts(struct net_device *dev,
  				 const struct sk_buff *skb);
966bae334   Eric W. Biederman   mpls: Functions f...
201

0189197f4   Eric W. Biederman   mpls: Basic routi...
202
  #endif /* MPLS_INTERNAL_H */