William Tu | 84e54fe | 2017-08-22 09:40:28 -0700 | [diff] [blame] | 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 | * |
William Tu | 1d7e2ed | 2017-12-13 16:38:55 -0800 | [diff] [blame] | 18 | * ERSPAN Version 1 (Type II) header (8 octets [42:49]) |
William Tu | 84e54fe | 2017-08-22 09:40:28 -0700 | [diff] [blame] | 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 | * |
William Tu | f551c91 | 2017-12-13 16:38:56 -0800 | [diff] [blame] | 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 | * |
William Tu | 84e54fe | 2017-08-22 09:40:28 -0700 | [diff] [blame] | 46 | * GRE proto ERSPAN type II = 0x88BE, type III = 0x22EB |
| 47 | */ |
| 48 | |
William Tu | d350a82 | 2018-01-25 13:20:10 -0800 | [diff] [blame] | 49 | #include <uapi/linux/erspan.h> |
| 50 | |
William Tu | 1d7e2ed | 2017-12-13 16:38:55 -0800 | [diff] [blame] | 51 | #define ERSPAN_VERSION 0x1 /* ERSPAN type II */ |
William Tu | 84e54fe | 2017-08-22 09:40:28 -0700 | [diff] [blame] | 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 | |
William Tu | f551c91 | 2017-12-13 16:38:56 -0800 | [diff] [blame] | 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 | |
William Tu | c69de58 | 2018-01-25 13:20:09 -0800 | [diff] [blame] | 70 | #define HWID_OFFSET 4 |
| 71 | #define DIR_OFFSET 3 |
| 72 | |
William Tu | 84e54fe | 2017-08-22 09:40:28 -0700 | [diff] [blame] | 73 | enum 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 | |
William Tu | 1d7e2ed | 2017-12-13 16:38:55 -0800 | [diff] [blame] | 80 | #define ERSPAN_V1_MDSIZE 4 |
| 81 | #define ERSPAN_V2_MDSIZE 8 |
William Tu | 84e54fe | 2017-08-22 09:40:28 -0700 | [diff] [blame] | 82 | |
William Tu | 1d7e2ed | 2017-12-13 16:38:55 -0800 | [diff] [blame] | 83 | struct erspan_base_hdr { |
William Tu | c69de58 | 2018-01-25 13:20:09 -0800 | [diff] [blame] | 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 |
William Tu | 84e54fe | 2017-08-22 09:40:28 -0700 | [diff] [blame] | 105 | }; |
| 106 | |
William Tu | c69de58 | 2018-01-25 13:20:09 -0800 | [diff] [blame] | 107 | static 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 | |
| 113 | static inline u16 get_session_id(const struct erspan_base_hdr *ershdr) |
| 114 | { |
| 115 | return (ershdr->session_id_upper << 8) + ershdr->session_id; |
| 116 | } |
| 117 | |
| 118 | static 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 | |
| 124 | static inline u16 get_vlan(const struct erspan_base_hdr *ershdr) |
| 125 | { |
| 126 | return (ershdr->vlan_upper << 8) + ershdr->vlan; |
| 127 | } |
| 128 | |
| 129 | static 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 | |
| 135 | static inline u8 get_hwid(const struct erspan_md2 *md2) |
| 136 | { |
| 137 | return (md2->hwid_upper << 4) + md2->hwid; |
| 138 | } |
| 139 | |
William Tu | 1d7e2ed | 2017-12-13 16:38:55 -0800 | [diff] [blame] | 140 | static 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 | |
William Tu | a3222dc | 2017-11-30 11:51:27 -0800 | [diff] [blame] | 146 | static 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 | |
| 155 | static inline void erspan_build_header(struct sk_buff *skb, |
William Tu | c69de58 | 2018-01-25 13:20:09 -0800 | [diff] [blame] | 156 | u32 id, u32 index, |
William Tu | a3222dc | 2017-11-30 11:51:27 -0800 | [diff] [blame] | 157 | bool truncate, bool is_ipv4) |
| 158 | { |
William Tu | b423d13 | 2018-01-23 17:01:29 -0800 | [diff] [blame] | 159 | struct ethhdr *eth = (struct ethhdr *)skb->data; |
William Tu | a3222dc | 2017-11-30 11:51:27 -0800 | [diff] [blame] | 160 | enum erspan_encap_type enc_type; |
William Tu | 1d7e2ed | 2017-12-13 16:38:55 -0800 | [diff] [blame] | 161 | struct erspan_base_hdr *ershdr; |
William Tu | a3222dc | 2017-11-30 11:51:27 -0800 | [diff] [blame] | 162 | struct qtag_prefix { |
| 163 | __be16 eth_type; |
| 164 | __be16 tci; |
| 165 | } *qp; |
| 166 | u16 vlan_tci = 0; |
| 167 | u8 tos; |
William Tu | 3df19283 | 2018-02-05 13:35:34 -0800 | [diff] [blame] | 168 | __be32 *idx; |
William Tu | a3222dc | 2017-11-30 11:51:27 -0800 | [diff] [blame] | 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 | |
William Tu | 1d7e2ed | 2017-12-13 16:38:55 -0800 | [diff] [blame] | 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); |
William Tu | a3222dc | 2017-11-30 11:51:27 -0800 | [diff] [blame] | 188 | |
William Tu | 1d7e2ed | 2017-12-13 16:38:55 -0800 | [diff] [blame] | 189 | /* Build base header */ |
William Tu | c69de58 | 2018-01-25 13:20:09 -0800 | [diff] [blame] | 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); |
William Tu | 1d7e2ed | 2017-12-13 16:38:55 -0800 | [diff] [blame] | 196 | |
| 197 | /* Build metadata */ |
William Tu | 3df19283 | 2018-02-05 13:35:34 -0800 | [diff] [blame] | 198 | idx = (__be32 *)(ershdr + 1); |
| 199 | *idx = htonl(index & INDEX_MASK); |
William Tu | a3222dc | 2017-11-30 11:51:27 -0800 | [diff] [blame] | 200 | } |
| 201 | |
William Tu | f551c91 | 2017-12-13 16:38:56 -0800 | [diff] [blame] | 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 | */ |
| 208 | static 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 | static inline void erspan_build_header_v2(struct sk_buff *skb, |
William Tu | c69de58 | 2018-01-25 13:20:09 -0800 | [diff] [blame] | 223 | u32 id, u8 direction, u16 hwid, |
William Tu | f551c91 | 2017-12-13 16:38:56 -0800 | [diff] [blame] | 224 | bool truncate, bool is_ipv4) |
| 225 | { |
William Tu | b423d13 | 2018-01-23 17:01:29 -0800 | [diff] [blame] | 226 | struct ethhdr *eth = (struct ethhdr *)skb->data; |
William Tu | f551c91 | 2017-12-13 16:38:56 -0800 | [diff] [blame] | 227 | struct erspan_base_hdr *ershdr; |
William Tu | 3df19283 | 2018-02-05 13:35:34 -0800 | [diff] [blame] | 228 | struct erspan_md2 *md2; |
William Tu | f551c91 | 2017-12-13 16:38:56 -0800 | [diff] [blame] | 229 | struct qtag_prefix { |
| 230 | __be16 eth_type; |
| 231 | __be16 tci; |
| 232 | } *qp; |
| 233 | u16 vlan_tci = 0; |
William Tu | f551c91 | 2017-12-13 16:38:56 -0800 | [diff] [blame] | 234 | u8 gra = 0; /* 100 usec */ |
| 235 | u8 bso = 0; /* Bad/Short/Oversized */ |
| 236 | u8 sgt = 0; |
| 237 | u8 tos; |
| 238 | |
| 239 | tos = is_ipv4 ? ip_hdr(skb)->tos : |
| 240 | (ipv6_hdr(skb)->priority << 4) + |
| 241 | (ipv6_hdr(skb)->flow_lbl[0] >> 4); |
| 242 | |
| 243 | /* Unlike v1, v2 does not have En field, |
| 244 | * so only extract vlan tci field. |
| 245 | */ |
| 246 | if (eth->h_proto == htons(ETH_P_8021Q)) { |
| 247 | qp = (struct qtag_prefix *)(skb->data + 2 * ETH_ALEN); |
| 248 | vlan_tci = ntohs(qp->tci); |
| 249 | } |
| 250 | |
| 251 | skb_push(skb, sizeof(*ershdr) + ERSPAN_V2_MDSIZE); |
| 252 | ershdr = (struct erspan_base_hdr *)skb->data; |
| 253 | memset(ershdr, 0, sizeof(*ershdr) + ERSPAN_V2_MDSIZE); |
| 254 | |
| 255 | /* Build base header */ |
William Tu | c69de58 | 2018-01-25 13:20:09 -0800 | [diff] [blame] | 256 | ershdr->ver = ERSPAN_VERSION2; |
| 257 | ershdr->cos = tos_to_cos(tos); |
| 258 | ershdr->en = bso; |
| 259 | ershdr->t = truncate; |
| 260 | set_vlan(ershdr, vlan_tci); |
| 261 | set_session_id(ershdr, id); |
William Tu | f551c91 | 2017-12-13 16:38:56 -0800 | [diff] [blame] | 262 | |
| 263 | /* Build metadata */ |
William Tu | 3df19283 | 2018-02-05 13:35:34 -0800 | [diff] [blame] | 264 | md2 = (struct erspan_md2 *)(ershdr + 1); |
| 265 | md2->timestamp = erspan_get_timestamp(); |
| 266 | md2->sgt = htons(sgt); |
| 267 | md2->p = 1; |
| 268 | md2->ft = 0; |
| 269 | md2->dir = direction; |
| 270 | md2->gra = gra; |
| 271 | md2->o = 0; |
| 272 | set_hwid(md2, hwid); |
William Tu | f551c91 | 2017-12-13 16:38:56 -0800 | [diff] [blame] | 273 | } |
| 274 | |
William Tu | 84e54fe | 2017-08-22 09:40:28 -0700 | [diff] [blame] | 275 | #endif |