1 #ifndef __LINUX_ERSPAN_H
2 #define __LINUX_ERSPAN_H
5 * GRE header for ERSPAN type I encapsulation (4 octets [34:37])
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 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
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.
17 * GRE header for ERSPAN type II and II encapsulation (8 octets [34:41])
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 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
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.
30 * ERSPAN Version 1 (Type II) header (8 octets [42:49])
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 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
40 * ERSPAN Version 2 (Type III) header (12 octets [42:49])
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 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
47 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
48 * | SGT |P| FT | Hw ID |D|Gra|O|
49 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
51 * Platform Specific SubHeader (8 octets, optional)
52 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
53 * | Platf ID | Platform Specific Info |
54 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
55 * | Platform Specific Info |
56 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
58 * GRE proto ERSPAN type I/II = 0x88BE, type III = 0x22EB
61 #include <uapi/linux/erspan.h>
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
69 #define ID_MASK 0x03ff
70 #define INDEX_MASK 0xfffff
72 #define ERSPAN_VERSION2 0x2 /* ERSPAN type III*/
73 #define BSO_MASK EN_MASK
74 #define SGT_MASK 0xffff0000
76 #define FT_MASK 0x7c00
77 #define HWID_MASK 0x03f0
78 #define DIR_MASK 0x0008
79 #define GRA_MASK 0x0006
85 enum 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 */
92 #define ERSPAN_V1_MDSIZE 4
93 #define ERSPAN_V2_MDSIZE 8
95 struct erspan_base_hdr
{
96 #if defined(__LITTLE_ENDIAN_BITFIELD)
100 __u8 session_id_upper
:2,
105 #elif defined(__BIG_ENDIAN_BITFIELD)
115 #error "Please fix <asm/byteorder.h>"
119 static inline void set_session_id(struct erspan_base_hdr
*ershdr
, u16 id
)
121 ershdr
->session_id
= id
& 0xff;
122 ershdr
->session_id_upper
= (id
>> 8) & 0x3;
125 static inline u16
get_session_id(const struct erspan_base_hdr
*ershdr
)
127 return (ershdr
->session_id_upper
<< 8) + ershdr
->session_id
;
130 static inline void set_vlan(struct erspan_base_hdr
*ershdr
, u16 vlan
)
132 ershdr
->vlan
= vlan
& 0xff;
133 ershdr
->vlan_upper
= (vlan
>> 8) & 0xf;
136 static inline u16
get_vlan(const struct erspan_base_hdr
*ershdr
)
138 return (ershdr
->vlan_upper
<< 8) + ershdr
->vlan
;
141 static inline void set_hwid(struct erspan_md2
*md2
, u8 hwid
)
143 md2
->hwid
= hwid
& 0xf;
144 md2
->hwid_upper
= (hwid
>> 4) & 0x3;
147 static inline u8
get_hwid(const struct erspan_md2
*md2
)
149 return (md2
->hwid_upper
<< 4) + md2
->hwid
;
152 static inline int erspan_hdr_len(int version
)
157 return sizeof(struct erspan_base_hdr
) +
158 (version
== 1 ? ERSPAN_V1_MDSIZE
: ERSPAN_V2_MDSIZE
);
161 static inline u8
tos_to_cos(u8 tos
)
170 static inline void erspan_build_header(struct sk_buff
*skb
,
172 bool truncate
, bool is_ipv4
)
174 struct ethhdr
*eth
= (struct ethhdr
*)skb
->data
;
175 enum erspan_encap_type enc_type
;
176 struct erspan_base_hdr
*ershdr
;
185 tos
= is_ipv4
? ip_hdr(skb
)->tos
:
186 (ipv6_hdr(skb
)->priority
<< 4) +
187 (ipv6_hdr(skb
)->flow_lbl
[0] >> 4);
189 enc_type
= ERSPAN_ENCAP_NOVLAN
;
191 /* If mirrored packet has vlan tag, extract tci and
192 * perserve vlan header in the mirrored frame.
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
;
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
);
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
);
213 idx
= (__be32
*)(ershdr
+ 1);
214 *idx
= htonl(index
& INDEX_MASK
);
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.
223 static inline __be32
erspan_get_timestamp(void)
228 kt
= ktime_get_real();
229 h_usecs
= ktime_divns(kt
, 100 * NSEC_PER_USEC
);
231 /* ERSPAN base header only has 32-bit,
232 * so it wraps around 4 days.
234 return htonl((u32
)h_usecs
);
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
250 static inline u8
erspan_detect_bso(struct sk_buff
*skb
)
252 /* BSO_BAD is not handled because the frame CRC
253 * or alignment error information is in FCS.
255 if (skb
->len
< ETH_ZLEN
)
258 if (skb
->len
> ETH_FRAME_LEN
)
259 return BSO_OVERSIZED
;
264 static inline void erspan_build_header_v2(struct sk_buff
*skb
,
265 u32 id
, u8 direction
, u16 hwid
,
266 bool truncate
, bool is_ipv4
)
268 struct ethhdr
*eth
= (struct ethhdr
*)skb
->data
;
269 struct erspan_base_hdr
*ershdr
;
270 struct erspan_md2
*md2
;
276 u8 gra
= 0; /* 100 usec */
277 u8 bso
= 0; /* Bad/Short/Oversized */
281 tos
= is_ipv4
? ip_hdr(skb
)->tos
:
282 (ipv6_hdr(skb
)->priority
<< 4) +
283 (ipv6_hdr(skb
)->flow_lbl
[0] >> 4);
285 /* Unlike v1, v2 does not have En field,
286 * so only extract vlan tci field.
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
);
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
);
298 /* Build base header */
299 ershdr
->ver
= ERSPAN_VERSION2
;
300 ershdr
->cos
= tos_to_cos(tos
);
302 ershdr
->t
= truncate
;
303 set_vlan(ershdr
, vlan_tci
);
304 set_session_id(ershdr
, id
);
307 md2
= (struct erspan_md2
*)(ershdr
+ 1);
308 md2
->timestamp
= erspan_get_timestamp();
309 md2
->sgt
= htons(sgt
);
312 md2
->dir
= direction
;