1 #ifndef MPLS_INTERNAL_H
2 #define MPLS_INTERNAL_H
5 /* put a reasonable limit on the number of labels
6 * we will accept from userspace
8 #define MAX_NEW_LABELS 30
10 struct mpls_entry_decoded
{
17 struct mpls_pcpu_stats
{
18 struct mpls_link_stats stats
;
19 struct u64_stats_sync syncp
;
24 struct net_device
*dev
;
25 struct mpls_pcpu_stats __percpu
*stats
;
27 struct ctl_table_header
*sysctl
;
31 #if BITS_PER_LONG == 32
33 #define MPLS_INC_STATS_LEN(mdev, len, pkts_field, bytes_field) \
35 __typeof__(*(mdev)->stats) *ptr = \
36 raw_cpu_ptr((mdev)->stats); \
38 u64_stats_update_begin(&ptr->syncp); \
39 ptr->stats.pkts_field++; \
40 ptr->stats.bytes_field += (len); \
41 u64_stats_update_end(&ptr->syncp); \
45 #define MPLS_INC_STATS(mdev, field) \
47 __typeof__(*(mdev)->stats) *ptr = \
48 raw_cpu_ptr((mdev)->stats); \
50 u64_stats_update_begin(&ptr->syncp); \
52 u64_stats_update_end(&ptr->syncp); \
58 #define MPLS_INC_STATS_LEN(mdev, len, pkts_field, bytes_field) \
60 this_cpu_inc((mdev)->stats->stats.pkts_field); \
61 this_cpu_add((mdev)->stats->stats.bytes_field, (len)); \
64 #define MPLS_INC_STATS(mdev, field) \
65 this_cpu_inc((mdev)->stats->stats.field)
71 #define LABEL_NOT_SPECIFIED (1 << 20)
73 /* This maximum ha length copied from the definition of struct neighbour */
74 #define VIA_ALEN_ALIGN sizeof(unsigned long)
75 #define MAX_VIA_ALEN (ALIGN(MAX_ADDR_LEN, VIA_ALEN_ALIGN))
77 enum mpls_payload_type
{
78 MPT_UNSPEC
, /* IPv4 or IPv6 */
82 /* Other types not implemented:
83 * - Pseudo-wire with or without control word (RFC4385)
88 struct mpls_nh
{ /* next hop label forwarding entry */
89 struct net_device __rcu
*nh_dev
;
91 /* nh_flags is accessed under RCU in the packet path; it is
92 * modified handling netdev events with rtnl lock held
94 unsigned int nh_flags
;
103 /* offset of via from beginning of mpls_nh */
104 #define MPLS_NH_VIA_OFF(num_labels) \
105 ALIGN(sizeof(struct mpls_nh) + (num_labels) * sizeof(u32), \
108 /* all nexthops within a route have the same size based on the
109 * max number of labels and max via length across all nexthops
111 #define MPLS_NH_SIZE(num_labels, max_via_alen) \
112 (MPLS_NH_VIA_OFF((num_labels)) + \
113 ALIGN((max_via_alen), VIA_ALEN_ALIGN))
115 enum mpls_ttl_propagation
{
116 MPLS_TTL_PROP_DEFAULT
,
117 MPLS_TTL_PROP_ENABLED
,
118 MPLS_TTL_PROP_DISABLED
,
121 /* The route, nexthops and vias are stored together in the same memory
124 * +----------------------+
126 * +----------------------+
128 * +----------------------+
129 * | alignment padding | 4 bytes for odd number of labels
130 * +----------------------+
131 * | via[rt_max_alen] 0 |
132 * +----------------------+
133 * | alignment padding | via's aligned on sizeof(unsigned long)
134 * +----------------------+
136 * +----------------------+
138 * +----------------------+
139 * | via[rt_max_alen] n-1 |
140 * +----------------------+
142 struct mpls_route
{ /* next hop label forwarding entry */
143 struct rcu_head rt_rcu
;
149 /* rt_nhn_alive is accessed under RCU in the packet path; it
150 * is modified handling netdev events with rtnl lock held
156 struct mpls_nh rt_nh
[0];
159 #define for_nexthops(rt) { \
160 int nhsel; struct mpls_nh *nh; u8 *__nh; \
161 for (nhsel = 0, nh = (rt)->rt_nh, __nh = (u8 *)((rt)->rt_nh); \
162 nhsel < (rt)->rt_nhn; \
163 __nh += rt->rt_nh_size, nh = (struct mpls_nh *)__nh, nhsel++)
165 #define change_nexthops(rt) { \
166 int nhsel; struct mpls_nh *nh; u8 *__nh; \
167 for (nhsel = 0, nh = (struct mpls_nh *)((rt)->rt_nh), \
168 __nh = (u8 *)((rt)->rt_nh); \
169 nhsel < (rt)->rt_nhn; \
170 __nh += rt->rt_nh_size, nh = (struct mpls_nh *)__nh, nhsel++)
172 #define endfor_nexthops(rt) }
174 static inline struct mpls_shim_hdr
mpls_entry_encode(u32 label
, unsigned ttl
, unsigned tc
, bool bos
)
176 struct mpls_shim_hdr result
;
177 result
.label_stack_entry
=
178 cpu_to_be32((label
<< MPLS_LS_LABEL_SHIFT
) |
179 (tc
<< MPLS_LS_TC_SHIFT
) |
180 (bos
? (1 << MPLS_LS_S_SHIFT
) : 0) |
181 (ttl
<< MPLS_LS_TTL_SHIFT
));
185 static inline struct mpls_entry_decoded
mpls_entry_decode(struct mpls_shim_hdr
*hdr
)
187 struct mpls_entry_decoded result
;
188 unsigned entry
= be32_to_cpu(hdr
->label_stack_entry
);
190 result
.label
= (entry
& MPLS_LS_LABEL_MASK
) >> MPLS_LS_LABEL_SHIFT
;
191 result
.ttl
= (entry
& MPLS_LS_TTL_MASK
) >> MPLS_LS_TTL_SHIFT
;
192 result
.tc
= (entry
& MPLS_LS_TC_MASK
) >> MPLS_LS_TC_SHIFT
;
193 result
.bos
= (entry
& MPLS_LS_S_MASK
) >> MPLS_LS_S_SHIFT
;
198 static inline struct mpls_dev
*mpls_dev_get(const struct net_device
*dev
)
200 return rcu_dereference_rtnl(dev
->mpls_ptr
);
203 int nla_put_labels(struct sk_buff
*skb
, int attrtype
, u8 labels
,
205 int nla_get_labels(const struct nlattr
*nla
, u8 max_labels
, u8
*labels
,
206 u32 label
[], struct netlink_ext_ack
*extack
);
207 bool mpls_output_possible(const struct net_device
*dev
);
208 unsigned int mpls_dev_mtu(const struct net_device
*dev
);
209 bool mpls_pkt_too_big(const struct sk_buff
*skb
, unsigned int mtu
);
210 void mpls_stats_inc_outucastpkts(struct net_device
*dev
,
211 const struct sk_buff
*skb
);
213 #endif /* MPLS_INTERNAL_H */