blob: 70c40c7c75b26c186c5a170c3a3105bdaeddcda2 [file] [log] [blame]
William Tu84e54fe2017-08-22 09:40:28 -07001#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 *
William Tu1d7e2ed2017-12-13 16:38:55 -080018 * ERSPAN Version 1 (Type II) header (8 octets [42:49])
William Tu84e54fe2017-08-22 09:40:28 -070019 * 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 * GRE proto ERSPAN type II = 0x88BE, type III = 0x22EB
28 */
29
William Tu1d7e2ed2017-12-13 16:38:55 -080030#define ERSPAN_VERSION 0x1 /* ERSPAN type II */
William Tu84e54fe2017-08-22 09:40:28 -070031
32#define VER_MASK 0xf000
33#define VLAN_MASK 0x0fff
34#define COS_MASK 0xe000
35#define EN_MASK 0x1800
36#define T_MASK 0x0400
37#define ID_MASK 0x03ff
38#define INDEX_MASK 0xfffff
39
40enum erspan_encap_type {
41 ERSPAN_ENCAP_NOVLAN = 0x0, /* originally without VLAN tag */
42 ERSPAN_ENCAP_ISL = 0x1, /* originally ISL encapsulated */
43 ERSPAN_ENCAP_8021Q = 0x2, /* originally 802.1Q encapsulated */
44 ERSPAN_ENCAP_INFRAME = 0x3, /* VLAN tag perserved in frame */
45};
46
William Tu1d7e2ed2017-12-13 16:38:55 -080047#define ERSPAN_V1_MDSIZE 4
48#define ERSPAN_V2_MDSIZE 8
William Tu84e54fe2017-08-22 09:40:28 -070049struct erspan_metadata {
William Tu1d7e2ed2017-12-13 16:38:55 -080050 union {
51 __be32 index; /* Version 1 (type II)*/
52 } u;
William Tu84e54fe2017-08-22 09:40:28 -070053};
54
William Tu1d7e2ed2017-12-13 16:38:55 -080055struct erspan_base_hdr {
William Tu84e54fe2017-08-22 09:40:28 -070056 __be16 ver_vlan;
57#define VER_OFFSET 12
58 __be16 session_id;
59#define COS_OFFSET 13
60#define EN_OFFSET 11
61#define T_OFFSET 10
William Tu84e54fe2017-08-22 09:40:28 -070062};
63
William Tu1d7e2ed2017-12-13 16:38:55 -080064static inline int erspan_hdr_len(int version)
65{
66 return sizeof(struct erspan_base_hdr) +
67 (version == 1 ? ERSPAN_V1_MDSIZE : ERSPAN_V2_MDSIZE);
68}
69
William Tua3222dc2017-11-30 11:51:27 -080070static inline u8 tos_to_cos(u8 tos)
71{
72 u8 dscp, cos;
73
74 dscp = tos >> 2;
75 cos = dscp >> 3;
76 return cos;
77}
78
79static inline void erspan_build_header(struct sk_buff *skb,
80 __be32 id, u32 index,
81 bool truncate, bool is_ipv4)
82{
83 struct ethhdr *eth = eth_hdr(skb);
84 enum erspan_encap_type enc_type;
William Tu1d7e2ed2017-12-13 16:38:55 -080085 struct erspan_base_hdr *ershdr;
86 struct erspan_metadata *ersmd;
William Tua3222dc2017-11-30 11:51:27 -080087 struct qtag_prefix {
88 __be16 eth_type;
89 __be16 tci;
90 } *qp;
91 u16 vlan_tci = 0;
92 u8 tos;
93
94 tos = is_ipv4 ? ip_hdr(skb)->tos :
95 (ipv6_hdr(skb)->priority << 4) +
96 (ipv6_hdr(skb)->flow_lbl[0] >> 4);
97
98 enc_type = ERSPAN_ENCAP_NOVLAN;
99
100 /* If mirrored packet has vlan tag, extract tci and
101 * perserve vlan header in the mirrored frame.
102 */
103 if (eth->h_proto == htons(ETH_P_8021Q)) {
104 qp = (struct qtag_prefix *)(skb->data + 2 * ETH_ALEN);
105 vlan_tci = ntohs(qp->tci);
106 enc_type = ERSPAN_ENCAP_INFRAME;
107 }
108
William Tu1d7e2ed2017-12-13 16:38:55 -0800109 skb_push(skb, sizeof(*ershdr) + ERSPAN_V1_MDSIZE);
110 ershdr = (struct erspan_base_hdr *)skb->data;
111 memset(ershdr, 0, sizeof(*ershdr) + ERSPAN_V1_MDSIZE);
William Tua3222dc2017-11-30 11:51:27 -0800112
William Tu1d7e2ed2017-12-13 16:38:55 -0800113 /* Build base header */
William Tua3222dc2017-11-30 11:51:27 -0800114 ershdr->ver_vlan = htons((vlan_tci & VLAN_MASK) |
115 (ERSPAN_VERSION << VER_OFFSET));
116 ershdr->session_id = htons((u16)(ntohl(id) & ID_MASK) |
117 ((tos_to_cos(tos) << COS_OFFSET) & COS_MASK) |
118 (enc_type << EN_OFFSET & EN_MASK) |
119 ((truncate << T_OFFSET) & T_MASK));
William Tu1d7e2ed2017-12-13 16:38:55 -0800120
121 /* Build metadata */
122 ersmd = (struct erspan_metadata *)(ershdr + 1);
123 ersmd->u.index = htonl(index & INDEX_MASK);
William Tua3222dc2017-11-30 11:51:27 -0800124}
125
William Tu84e54fe2017-08-22 09:40:28 -0700126#endif