blob: 2ac97433c3b7a7a9a4bef51cc172fbf35664c45b [file] [log] [blame]
Eric W. Biederman01891972015-03-03 19:10:47 -06001#ifndef MPLS_INTERNAL_H
2#define MPLS_INTERNAL_H
Jiri Benc9095e102016-09-30 19:08:06 +02003#include <net/mpls.h>
Eric W. Biederman01891972015-03-03 19:10:47 -06004
5struct mpls_entry_decoded {
6 u32 label;
7 u8 ttl;
8 u8 tc;
9 u8 bos;
10};
11
Robert Shearman27d69102017-01-16 14:16:37 +000012struct mpls_pcpu_stats {
13 struct mpls_link_stats stats;
14 struct u64_stats_sync syncp;
Robert Shearman03c57742015-04-22 11:14:37 +010015};
16
Robert Shearman27d69102017-01-16 14:16:37 +000017struct mpls_dev {
18 int input_enabled;
David Ahern24045a02017-02-20 08:03:30 -080019 struct net_device *dev;
Robert Shearman27d69102017-01-16 14:16:37 +000020 struct mpls_pcpu_stats __percpu *stats;
21
22 struct ctl_table_header *sysctl;
23 struct rcu_head rcu;
24};
25
26#if BITS_PER_LONG == 32
27
28#define MPLS_INC_STATS_LEN(mdev, len, pkts_field, bytes_field) \
29 do { \
30 __typeof__(*(mdev)->stats) *ptr = \
31 raw_cpu_ptr((mdev)->stats); \
32 local_bh_disable(); \
33 u64_stats_update_begin(&ptr->syncp); \
34 ptr->stats.pkts_field++; \
35 ptr->stats.bytes_field += (len); \
36 u64_stats_update_end(&ptr->syncp); \
37 local_bh_enable(); \
38 } while (0)
39
40#define MPLS_INC_STATS(mdev, field) \
41 do { \
42 __typeof__(*(mdev)->stats) *ptr = \
43 raw_cpu_ptr((mdev)->stats); \
44 local_bh_disable(); \
45 u64_stats_update_begin(&ptr->syncp); \
46 ptr->stats.field++; \
47 u64_stats_update_end(&ptr->syncp); \
48 local_bh_enable(); \
49 } while (0)
50
51#else
52
53#define MPLS_INC_STATS_LEN(mdev, len, pkts_field, bytes_field) \
54 do { \
55 this_cpu_inc((mdev)->stats->stats.pkts_field); \
56 this_cpu_add((mdev)->stats->stats.bytes_field, (len)); \
57 } while (0)
58
59#define MPLS_INC_STATS(mdev, field) \
60 this_cpu_inc((mdev)->stats->stats.field)
61
62#endif
63
Eric W. Biederman01891972015-03-03 19:10:47 -060064struct sk_buff;
65
Roopa Prabhuf8efb732015-10-23 06:03:27 -070066#define LABEL_NOT_SPECIFIED (1 << 20)
67#define MAX_NEW_LABELS 2
68
69/* This maximum ha length copied from the definition of struct neighbour */
Robert Shearmancf4b24f2015-10-27 00:37:36 +000070#define VIA_ALEN_ALIGN sizeof(unsigned long)
71#define MAX_VIA_ALEN (ALIGN(MAX_ADDR_LEN, VIA_ALEN_ALIGN))
Roopa Prabhuf8efb732015-10-23 06:03:27 -070072
73enum mpls_payload_type {
74 MPT_UNSPEC, /* IPv4 or IPv6 */
75 MPT_IPV4 = 4,
76 MPT_IPV6 = 6,
77
78 /* Other types not implemented:
79 * - Pseudo-wire with or without control word (RFC4385)
80 * - GAL (RFC5586)
81 */
82};
83
84struct mpls_nh { /* next hop label forwarding entry */
85 struct net_device __rcu *nh_dev;
David Ahern39eb8cd2017-03-31 07:13:59 -070086
87 /* nh_flags is accessed under RCU in the packet path; it is
88 * modified handling netdev events with rtnl lock held
89 */
Roopa Prabhuc89359a2015-12-01 22:18:11 -080090 unsigned int nh_flags;
Roopa Prabhuf8efb732015-10-23 06:03:27 -070091 u32 nh_label[MAX_NEW_LABELS];
92 u8 nh_labels;
93 u8 nh_via_alen;
94 u8 nh_via_table;
Roopa Prabhuf8efb732015-10-23 06:03:27 -070095};
96
Robert Shearman5b441ac2017-03-10 20:43:24 +000097enum mpls_ttl_propagation {
98 MPLS_TTL_PROP_DEFAULT,
99 MPLS_TTL_PROP_ENABLED,
100 MPLS_TTL_PROP_DISABLED,
101};
102
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000103/* The route, nexthops and vias are stored together in the same memory
104 * block:
105 *
106 * +----------------------+
107 * | mpls_route |
108 * +----------------------+
109 * | mpls_nh 0 |
110 * +----------------------+
111 * | ... |
112 * +----------------------+
113 * | mpls_nh n-1 |
114 * +----------------------+
115 * | alignment padding |
116 * +----------------------+
117 * | via[rt_max_alen] 0 |
118 * +----------------------+
119 * | ... |
120 * +----------------------+
121 * | via[rt_max_alen] n-1 |
122 * +----------------------+
123 */
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700124struct mpls_route { /* next hop label forwarding entry */
125 struct rcu_head rt_rcu;
126 u8 rt_protocol;
127 u8 rt_payload_type;
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000128 u8 rt_max_alen;
Robert Shearman5b441ac2017-03-10 20:43:24 +0000129 u8 rt_ttl_propagate;
David Ahern77ef013a2017-03-31 07:14:00 -0700130 u8 rt_nhn;
David Ahern39eb8cd2017-03-31 07:13:59 -0700131
132 /* rt_nhn_alive is accessed under RCU in the packet path; it
133 * is modified handling netdev events with rtnl lock held
134 */
David Ahern77ef013a2017-03-31 07:14:00 -0700135 u8 rt_nhn_alive;
136 u16 rt_reserved1;
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700137 struct mpls_nh rt_nh[0];
138};
139
140#define for_nexthops(rt) { \
141 int nhsel; struct mpls_nh *nh; \
142 for (nhsel = 0, nh = (rt)->rt_nh; \
143 nhsel < (rt)->rt_nhn; \
144 nh++, nhsel++)
145
146#define change_nexthops(rt) { \
147 int nhsel; struct mpls_nh *nh; \
148 for (nhsel = 0, nh = (struct mpls_nh *)((rt)->rt_nh); \
149 nhsel < (rt)->rt_nhn; \
150 nh++, nhsel++)
151
152#define endfor_nexthops(rt) }
153
Eric W. Biederman01891972015-03-03 19:10:47 -0600154static inline struct mpls_shim_hdr mpls_entry_encode(u32 label, unsigned ttl, unsigned tc, bool bos)
155{
156 struct mpls_shim_hdr result;
157 result.label_stack_entry =
158 cpu_to_be32((label << MPLS_LS_LABEL_SHIFT) |
159 (tc << MPLS_LS_TC_SHIFT) |
160 (bos ? (1 << MPLS_LS_S_SHIFT) : 0) |
161 (ttl << MPLS_LS_TTL_SHIFT));
162 return result;
163}
164
165static inline struct mpls_entry_decoded mpls_entry_decode(struct mpls_shim_hdr *hdr)
166{
167 struct mpls_entry_decoded result;
168 unsigned entry = be32_to_cpu(hdr->label_stack_entry);
169
170 result.label = (entry & MPLS_LS_LABEL_MASK) >> MPLS_LS_LABEL_SHIFT;
171 result.ttl = (entry & MPLS_LS_TTL_MASK) >> MPLS_LS_TTL_SHIFT;
172 result.tc = (entry & MPLS_LS_TC_MASK) >> MPLS_LS_TC_SHIFT;
173 result.bos = (entry & MPLS_LS_S_MASK) >> MPLS_LS_S_SHIFT;
174
175 return result;
176}
177
Robert Shearman27d69102017-01-16 14:16:37 +0000178static inline struct mpls_dev *mpls_dev_get(const struct net_device *dev)
179{
180 return rcu_dereference_rtnl(dev->mpls_ptr);
181}
182
Roopa Prabhuface0182015-07-21 10:43:52 +0200183int nla_put_labels(struct sk_buff *skb, int attrtype, u8 labels,
184 const u32 label[]);
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700185int nla_get_labels(const struct nlattr *nla, u32 max_labels, u8 *labels,
Roopa Prabhuface0182015-07-21 10:43:52 +0200186 u32 label[]);
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700187int nla_get_via(const struct nlattr *nla, u8 *via_alen, u8 *via_table,
188 u8 via[]);
Roopa Prabhuface0182015-07-21 10:43:52 +0200189bool mpls_output_possible(const struct net_device *dev);
190unsigned int mpls_dev_mtu(const struct net_device *dev);
191bool mpls_pkt_too_big(const struct sk_buff *skb, unsigned int mtu);
Robert Shearman27d69102017-01-16 14:16:37 +0000192void mpls_stats_inc_outucastpkts(struct net_device *dev,
193 const struct sk_buff *skb);
Eric W. Biederman966bae32015-03-03 19:13:19 -0600194
Eric W. Biederman01891972015-03-03 19:10:47 -0600195#endif /* MPLS_INTERNAL_H */