Loading...
1#ifndef __LINUX_ERSPAN_H
2#define __LINUX_ERSPAN_H
3
4/*
5 * GRE header for ERSPAN encapsulation (8 octets [34:41]) -- 8 bytes
6 * 0 1 2 3
7 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
8 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
9 * |0|0|0|1|0|00000|000000000|00000| Protocol Type for ERSPAN |
10 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
11 * | Sequence Number (increments per packet per session) |
12 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
13 *
14 * Note that in the above GRE header [RFC1701] out of the C, R, K, S,
15 * s, Recur, Flags, Version fields only S (bit 03) is set to 1. The
16 * other fields are set to zero, so only a sequence number follows.
17 *
18 * ERSPAN Version 1 (Type II) header (8 octets [42:49])
19 * 0 1 2 3
20 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
21 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
22 * | Ver | VLAN | COS | En|T| Session ID |
23 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
24 * | Reserved | Index |
25 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
26 *
27 *
28 * ERSPAN Version 2 (Type III) header (12 octets [42:49])
29 * 0 1 2 3
30 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
31 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
32 * | Ver | VLAN | COS |BSO|T| Session ID |
33 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
34 * | Timestamp |
35 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
36 * | SGT |P| FT | Hw ID |D|Gra|O|
37 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
38 *
39 * Platform Specific SubHeader (8 octets, optional)
40 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
41 * | Platf ID | Platform Specific Info |
42 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
43 * | Platform Specific Info |
44 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
45 *
46 * GRE proto ERSPAN type II = 0x88BE, type III = 0x22EB
47 */
48
49#include <uapi/linux/erspan.h>
50
51#define ERSPAN_VERSION 0x1 /* ERSPAN type II */
52#define VER_MASK 0xf000
53#define VLAN_MASK 0x0fff
54#define COS_MASK 0xe000
55#define EN_MASK 0x1800
56#define T_MASK 0x0400
57#define ID_MASK 0x03ff
58#define INDEX_MASK 0xfffff
59
60#define ERSPAN_VERSION2 0x2 /* ERSPAN type III*/
61#define BSO_MASK EN_MASK
62#define SGT_MASK 0xffff0000
63#define P_MASK 0x8000
64#define FT_MASK 0x7c00
65#define HWID_MASK 0x03f0
66#define DIR_MASK 0x0008
67#define GRA_MASK 0x0006
68#define O_MASK 0x0001
69
70#define HWID_OFFSET 4
71#define DIR_OFFSET 3
72
73enum erspan_encap_type {
74 ERSPAN_ENCAP_NOVLAN = 0x0, /* originally without VLAN tag */
75 ERSPAN_ENCAP_ISL = 0x1, /* originally ISL encapsulated */
76 ERSPAN_ENCAP_8021Q = 0x2, /* originally 802.1Q encapsulated */
77 ERSPAN_ENCAP_INFRAME = 0x3, /* VLAN tag perserved in frame */
78};
79
80#define ERSPAN_V1_MDSIZE 4
81#define ERSPAN_V2_MDSIZE 8
82
83struct erspan_base_hdr {
84#if defined(__LITTLE_ENDIAN_BITFIELD)
85 __u8 vlan_upper:4,
86 ver:4;
87 __u8 vlan:8;
88 __u8 session_id_upper:2,
89 t:1,
90 en:2,
91 cos:3;
92 __u8 session_id:8;
93#elif defined(__BIG_ENDIAN_BITFIELD)
94 __u8 ver: 4,
95 vlan_upper:4;
96 __u8 vlan:8;
97 __u8 cos:3,
98 en:2,
99 t:1,
100 session_id_upper:2;
101 __u8 session_id:8;
102#else
103#error "Please fix <asm/byteorder.h>"
104#endif
105};
106
107static inline void set_session_id(struct erspan_base_hdr *ershdr, u16 id)
108{
109 ershdr->session_id = id & 0xff;
110 ershdr->session_id_upper = (id >> 8) & 0x3;
111}
112
113static inline u16 get_session_id(const struct erspan_base_hdr *ershdr)
114{
115 return (ershdr->session_id_upper << 8) + ershdr->session_id;
116}
117
118static inline void set_vlan(struct erspan_base_hdr *ershdr, u16 vlan)
119{
120 ershdr->vlan = vlan & 0xff;
121 ershdr->vlan_upper = (vlan >> 8) & 0xf;
122}
123
124static inline u16 get_vlan(const struct erspan_base_hdr *ershdr)
125{
126 return (ershdr->vlan_upper << 8) + ershdr->vlan;
127}
128
129static inline void set_hwid(struct erspan_md2 *md2, u8 hwid)
130{
131 md2->hwid = hwid & 0xf;
132 md2->hwid_upper = (hwid >> 4) & 0x3;
133}
134
135static inline u8 get_hwid(const struct erspan_md2 *md2)
136{
137 return (md2->hwid_upper << 4) + md2->hwid;
138}
139
140static inline int erspan_hdr_len(int version)
141{
142 return sizeof(struct erspan_base_hdr) +
143 (version == 1 ? ERSPAN_V1_MDSIZE : ERSPAN_V2_MDSIZE);
144}
145
146static inline u8 tos_to_cos(u8 tos)
147{
148 u8 dscp, cos;
149
150 dscp = tos >> 2;
151 cos = dscp >> 3;
152 return cos;
153}
154
155static inline void erspan_build_header(struct sk_buff *skb,
156 u32 id, u32 index,
157 bool truncate, bool is_ipv4)
158{
159 struct ethhdr *eth = (struct ethhdr *)skb->data;
160 enum erspan_encap_type enc_type;
161 struct erspan_base_hdr *ershdr;
162 struct qtag_prefix {
163 __be16 eth_type;
164 __be16 tci;
165 } *qp;
166 u16 vlan_tci = 0;
167 u8 tos;
168 __be32 *idx;
169
170 tos = is_ipv4 ? ip_hdr(skb)->tos :
171 (ipv6_hdr(skb)->priority << 4) +
172 (ipv6_hdr(skb)->flow_lbl[0] >> 4);
173
174 enc_type = ERSPAN_ENCAP_NOVLAN;
175
176 /* If mirrored packet has vlan tag, extract tci and
177 * perserve vlan header in the mirrored frame.
178 */
179 if (eth->h_proto == htons(ETH_P_8021Q)) {
180 qp = (struct qtag_prefix *)(skb->data + 2 * ETH_ALEN);
181 vlan_tci = ntohs(qp->tci);
182 enc_type = ERSPAN_ENCAP_INFRAME;
183 }
184
185 skb_push(skb, sizeof(*ershdr) + ERSPAN_V1_MDSIZE);
186 ershdr = (struct erspan_base_hdr *)skb->data;
187 memset(ershdr, 0, sizeof(*ershdr) + ERSPAN_V1_MDSIZE);
188
189 /* Build base header */
190 ershdr->ver = ERSPAN_VERSION;
191 ershdr->cos = tos_to_cos(tos);
192 ershdr->en = enc_type;
193 ershdr->t = truncate;
194 set_vlan(ershdr, vlan_tci);
195 set_session_id(ershdr, id);
196
197 /* Build metadata */
198 idx = (__be32 *)(ershdr + 1);
199 *idx = htonl(index & INDEX_MASK);
200}
201
202/* ERSPAN GRA: timestamp granularity
203 * 00b --> granularity = 100 microseconds
204 * 01b --> granularity = 100 nanoseconds
205 * 10b --> granularity = IEEE 1588
206 * Here we only support 100 microseconds.
207 */
208static inline __be32 erspan_get_timestamp(void)
209{
210 u64 h_usecs;
211 ktime_t kt;
212
213 kt = ktime_get_real();
214 h_usecs = ktime_divns(kt, 100 * NSEC_PER_USEC);
215
216 /* ERSPAN base header only has 32-bit,
217 * so it wraps around 4 days.
218 */
219 return htonl((u32)h_usecs);
220}
221
222/* ERSPAN BSO (Bad/Short/Oversized), see RFC1757
223 * 00b --> Good frame with no error, or unknown integrity
224 * 01b --> Payload is a Short Frame
225 * 10b --> Payload is an Oversized Frame
226 * 11b --> Payload is a Bad Frame with CRC or Alignment Error
227 */
228enum erspan_bso {
229 BSO_NOERROR = 0x0,
230 BSO_SHORT = 0x1,
231 BSO_OVERSIZED = 0x2,
232 BSO_BAD = 0x3,
233};
234
235static inline u8 erspan_detect_bso(struct sk_buff *skb)
236{
237 /* BSO_BAD is not handled because the frame CRC
238 * or alignment error information is in FCS.
239 */
240 if (skb->len < ETH_ZLEN)
241 return BSO_SHORT;
242
243 if (skb->len > ETH_FRAME_LEN)
244 return BSO_OVERSIZED;
245
246 return BSO_NOERROR;
247}
248
249static inline void erspan_build_header_v2(struct sk_buff *skb,
250 u32 id, u8 direction, u16 hwid,
251 bool truncate, bool is_ipv4)
252{
253 struct ethhdr *eth = (struct ethhdr *)skb->data;
254 struct erspan_base_hdr *ershdr;
255 struct erspan_md2 *md2;
256 struct qtag_prefix {
257 __be16 eth_type;
258 __be16 tci;
259 } *qp;
260 u16 vlan_tci = 0;
261 u8 gra = 0; /* 100 usec */
262 u8 bso = 0; /* Bad/Short/Oversized */
263 u8 sgt = 0;
264 u8 tos;
265
266 tos = is_ipv4 ? ip_hdr(skb)->tos :
267 (ipv6_hdr(skb)->priority << 4) +
268 (ipv6_hdr(skb)->flow_lbl[0] >> 4);
269
270 /* Unlike v1, v2 does not have En field,
271 * so only extract vlan tci field.
272 */
273 if (eth->h_proto == htons(ETH_P_8021Q)) {
274 qp = (struct qtag_prefix *)(skb->data + 2 * ETH_ALEN);
275 vlan_tci = ntohs(qp->tci);
276 }
277
278 bso = erspan_detect_bso(skb);
279 skb_push(skb, sizeof(*ershdr) + ERSPAN_V2_MDSIZE);
280 ershdr = (struct erspan_base_hdr *)skb->data;
281 memset(ershdr, 0, sizeof(*ershdr) + ERSPAN_V2_MDSIZE);
282
283 /* Build base header */
284 ershdr->ver = ERSPAN_VERSION2;
285 ershdr->cos = tos_to_cos(tos);
286 ershdr->en = bso;
287 ershdr->t = truncate;
288 set_vlan(ershdr, vlan_tci);
289 set_session_id(ershdr, id);
290
291 /* Build metadata */
292 md2 = (struct erspan_md2 *)(ershdr + 1);
293 md2->timestamp = erspan_get_timestamp();
294 md2->sgt = htons(sgt);
295 md2->p = 1;
296 md2->ft = 0;
297 md2->dir = direction;
298 md2->gra = gra;
299 md2->o = 0;
300 set_hwid(md2, hwid);
301}
302
303#endif
1#ifndef __LINUX_ERSPAN_H
2#define __LINUX_ERSPAN_H
3
4/*
5 * GRE header for ERSPAN type I encapsulation (4 octets [34:37])
6 * 0 1 2 3
7 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
8 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
9 * |0|0|0|0|0|00000|000000000|00000| Protocol Type for ERSPAN |
10 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
11 *
12 * The Type I ERSPAN frame format is based on the barebones IP + GRE
13 * encapsulation (as described above) on top of the raw mirrored frame.
14 * There is no extra ERSPAN header.
15 *
16 *
17 * GRE header for ERSPAN type II and II encapsulation (8 octets [34:41])
18 * 0 1 2 3
19 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
20 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
21 * |0|0|0|1|0|00000|000000000|00000| Protocol Type for ERSPAN |
22 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
23 * | Sequence Number (increments per packet per session) |
24 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
25 *
26 * Note that in the above GRE header [RFC1701] out of the C, R, K, S,
27 * s, Recur, Flags, Version fields only S (bit 03) is set to 1. The
28 * other fields are set to zero, so only a sequence number follows.
29 *
30 * ERSPAN Version 1 (Type II) header (8 octets [42:49])
31 * 0 1 2 3
32 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
33 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
34 * | Ver | VLAN | COS | En|T| Session ID |
35 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
36 * | Reserved | Index |
37 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
38 *
39 *
40 * ERSPAN Version 2 (Type III) header (12 octets [42:49])
41 * 0 1 2 3
42 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
43 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
44 * | Ver | VLAN | COS |BSO|T| Session ID |
45 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
46 * | Timestamp |
47 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
48 * | SGT |P| FT | Hw ID |D|Gra|O|
49 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
50 *
51 * Platform Specific SubHeader (8 octets, optional)
52 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
53 * | Platf ID | Platform Specific Info |
54 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
55 * | Platform Specific Info |
56 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
57 *
58 * GRE proto ERSPAN type I/II = 0x88BE, type III = 0x22EB
59 */
60
61#include <uapi/linux/erspan.h>
62
63#define ERSPAN_VERSION 0x1 /* ERSPAN type II */
64#define VER_MASK 0xf000
65#define VLAN_MASK 0x0fff
66#define COS_MASK 0xe000
67#define EN_MASK 0x1800
68#define T_MASK 0x0400
69#define ID_MASK 0x03ff
70#define INDEX_MASK 0xfffff
71
72#define ERSPAN_VERSION2 0x2 /* ERSPAN type III*/
73#define BSO_MASK EN_MASK
74#define SGT_MASK 0xffff0000
75#define P_MASK 0x8000
76#define FT_MASK 0x7c00
77#define HWID_MASK 0x03f0
78#define DIR_MASK 0x0008
79#define GRA_MASK 0x0006
80#define O_MASK 0x0001
81
82#define HWID_OFFSET 4
83#define DIR_OFFSET 3
84
85enum erspan_encap_type {
86 ERSPAN_ENCAP_NOVLAN = 0x0, /* originally without VLAN tag */
87 ERSPAN_ENCAP_ISL = 0x1, /* originally ISL encapsulated */
88 ERSPAN_ENCAP_8021Q = 0x2, /* originally 802.1Q encapsulated */
89 ERSPAN_ENCAP_INFRAME = 0x3, /* VLAN tag perserved in frame */
90};
91
92#define ERSPAN_V1_MDSIZE 4
93#define ERSPAN_V2_MDSIZE 8
94
95struct erspan_base_hdr {
96#if defined(__LITTLE_ENDIAN_BITFIELD)
97 __u8 vlan_upper:4,
98 ver:4;
99 __u8 vlan:8;
100 __u8 session_id_upper:2,
101 t:1,
102 en:2,
103 cos:3;
104 __u8 session_id:8;
105#elif defined(__BIG_ENDIAN_BITFIELD)
106 __u8 ver: 4,
107 vlan_upper:4;
108 __u8 vlan:8;
109 __u8 cos:3,
110 en:2,
111 t:1,
112 session_id_upper:2;
113 __u8 session_id:8;
114#else
115#error "Please fix <asm/byteorder.h>"
116#endif
117};
118
119static inline void set_session_id(struct erspan_base_hdr *ershdr, u16 id)
120{
121 ershdr->session_id = id & 0xff;
122 ershdr->session_id_upper = (id >> 8) & 0x3;
123}
124
125static inline u16 get_session_id(const struct erspan_base_hdr *ershdr)
126{
127 return (ershdr->session_id_upper << 8) + ershdr->session_id;
128}
129
130static inline void set_vlan(struct erspan_base_hdr *ershdr, u16 vlan)
131{
132 ershdr->vlan = vlan & 0xff;
133 ershdr->vlan_upper = (vlan >> 8) & 0xf;
134}
135
136static inline u16 get_vlan(const struct erspan_base_hdr *ershdr)
137{
138 return (ershdr->vlan_upper << 8) + ershdr->vlan;
139}
140
141static inline void set_hwid(struct erspan_md2 *md2, u8 hwid)
142{
143 md2->hwid = hwid & 0xf;
144 md2->hwid_upper = (hwid >> 4) & 0x3;
145}
146
147static inline u8 get_hwid(const struct erspan_md2 *md2)
148{
149 return (md2->hwid_upper << 4) + md2->hwid;
150}
151
152static inline int erspan_hdr_len(int version)
153{
154 if (version == 0)
155 return 0;
156
157 return sizeof(struct erspan_base_hdr) +
158 (version == 1 ? ERSPAN_V1_MDSIZE : ERSPAN_V2_MDSIZE);
159}
160
161static inline u8 tos_to_cos(u8 tos)
162{
163 u8 dscp, cos;
164
165 dscp = tos >> 2;
166 cos = dscp >> 3;
167 return cos;
168}
169
170static inline void erspan_build_header(struct sk_buff *skb,
171 u32 id, u32 index,
172 bool truncate, bool is_ipv4)
173{
174 struct ethhdr *eth = (struct ethhdr *)skb->data;
175 enum erspan_encap_type enc_type;
176 struct erspan_base_hdr *ershdr;
177 struct qtag_prefix {
178 __be16 eth_type;
179 __be16 tci;
180 } *qp;
181 u16 vlan_tci = 0;
182 u8 tos;
183 __be32 *idx;
184
185 tos = is_ipv4 ? ip_hdr(skb)->tos :
186 (ipv6_hdr(skb)->priority << 4) +
187 (ipv6_hdr(skb)->flow_lbl[0] >> 4);
188
189 enc_type = ERSPAN_ENCAP_NOVLAN;
190
191 /* If mirrored packet has vlan tag, extract tci and
192 * perserve vlan header in the mirrored frame.
193 */
194 if (eth->h_proto == htons(ETH_P_8021Q)) {
195 qp = (struct qtag_prefix *)(skb->data + 2 * ETH_ALEN);
196 vlan_tci = ntohs(qp->tci);
197 enc_type = ERSPAN_ENCAP_INFRAME;
198 }
199
200 skb_push(skb, sizeof(*ershdr) + ERSPAN_V1_MDSIZE);
201 ershdr = (struct erspan_base_hdr *)skb->data;
202 memset(ershdr, 0, sizeof(*ershdr) + ERSPAN_V1_MDSIZE);
203
204 /* Build base header */
205 ershdr->ver = ERSPAN_VERSION;
206 ershdr->cos = tos_to_cos(tos);
207 ershdr->en = enc_type;
208 ershdr->t = truncate;
209 set_vlan(ershdr, vlan_tci);
210 set_session_id(ershdr, id);
211
212 /* Build metadata */
213 idx = (__be32 *)(ershdr + 1);
214 *idx = htonl(index & INDEX_MASK);
215}
216
217/* ERSPAN GRA: timestamp granularity
218 * 00b --> granularity = 100 microseconds
219 * 01b --> granularity = 100 nanoseconds
220 * 10b --> granularity = IEEE 1588
221 * Here we only support 100 microseconds.
222 */
223static inline __be32 erspan_get_timestamp(void)
224{
225 u64 h_usecs;
226 ktime_t kt;
227
228 kt = ktime_get_real();
229 h_usecs = ktime_divns(kt, 100 * NSEC_PER_USEC);
230
231 /* ERSPAN base header only has 32-bit,
232 * so it wraps around 4 days.
233 */
234 return htonl((u32)h_usecs);
235}
236
237/* ERSPAN BSO (Bad/Short/Oversized), see RFC1757
238 * 00b --> Good frame with no error, or unknown integrity
239 * 01b --> Payload is a Short Frame
240 * 10b --> Payload is an Oversized Frame
241 * 11b --> Payload is a Bad Frame with CRC or Alignment Error
242 */
243enum erspan_bso {
244 BSO_NOERROR = 0x0,
245 BSO_SHORT = 0x1,
246 BSO_OVERSIZED = 0x2,
247 BSO_BAD = 0x3,
248};
249
250static inline u8 erspan_detect_bso(struct sk_buff *skb)
251{
252 /* BSO_BAD is not handled because the frame CRC
253 * or alignment error information is in FCS.
254 */
255 if (skb->len < ETH_ZLEN)
256 return BSO_SHORT;
257
258 if (skb->len > ETH_FRAME_LEN)
259 return BSO_OVERSIZED;
260
261 return BSO_NOERROR;
262}
263
264static inline void erspan_build_header_v2(struct sk_buff *skb,
265 u32 id, u8 direction, u16 hwid,
266 bool truncate, bool is_ipv4)
267{
268 struct ethhdr *eth = (struct ethhdr *)skb->data;
269 struct erspan_base_hdr *ershdr;
270 struct erspan_md2 *md2;
271 struct qtag_prefix {
272 __be16 eth_type;
273 __be16 tci;
274 } *qp;
275 u16 vlan_tci = 0;
276 u8 gra = 0; /* 100 usec */
277 u8 bso = 0; /* Bad/Short/Oversized */
278 u8 sgt = 0;
279 u8 tos;
280
281 tos = is_ipv4 ? ip_hdr(skb)->tos :
282 (ipv6_hdr(skb)->priority << 4) +
283 (ipv6_hdr(skb)->flow_lbl[0] >> 4);
284
285 /* Unlike v1, v2 does not have En field,
286 * so only extract vlan tci field.
287 */
288 if (eth->h_proto == htons(ETH_P_8021Q)) {
289 qp = (struct qtag_prefix *)(skb->data + 2 * ETH_ALEN);
290 vlan_tci = ntohs(qp->tci);
291 }
292
293 bso = erspan_detect_bso(skb);
294 skb_push(skb, sizeof(*ershdr) + ERSPAN_V2_MDSIZE);
295 ershdr = (struct erspan_base_hdr *)skb->data;
296 memset(ershdr, 0, sizeof(*ershdr) + ERSPAN_V2_MDSIZE);
297
298 /* Build base header */
299 ershdr->ver = ERSPAN_VERSION2;
300 ershdr->cos = tos_to_cos(tos);
301 ershdr->en = bso;
302 ershdr->t = truncate;
303 set_vlan(ershdr, vlan_tci);
304 set_session_id(ershdr, id);
305
306 /* Build metadata */
307 md2 = (struct erspan_md2 *)(ershdr + 1);
308 md2->timestamp = erspan_get_timestamp();
309 md2->sgt = htons(sgt);
310 md2->p = 1;
311 md2->ft = 0;
312 md2->dir = direction;
313 md2->gra = gra;
314 md2->o = 0;
315 set_hwid(md2, hwid);
316}
317
318#endif