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 | * |
| 27 | * GRE proto ERSPAN type II = 0x88BE, type III = 0x22EB |
| 28 | */ |
| 29 | |
William Tu | 1d7e2ed | 2017-12-13 16:38:55 -0800 | [diff] [blame^] | 30 | #define ERSPAN_VERSION 0x1 /* ERSPAN type II */ |
William Tu | 84e54fe | 2017-08-22 09:40:28 -0700 | [diff] [blame] | 31 | |
| 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 | |
| 40 | enum 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 Tu | 1d7e2ed | 2017-12-13 16:38:55 -0800 | [diff] [blame^] | 47 | #define ERSPAN_V1_MDSIZE 4 |
| 48 | #define ERSPAN_V2_MDSIZE 8 |
William Tu | 84e54fe | 2017-08-22 09:40:28 -0700 | [diff] [blame] | 49 | struct erspan_metadata { |
William Tu | 1d7e2ed | 2017-12-13 16:38:55 -0800 | [diff] [blame^] | 50 | union { |
| 51 | __be32 index; /* Version 1 (type II)*/ |
| 52 | } u; |
William Tu | 84e54fe | 2017-08-22 09:40:28 -0700 | [diff] [blame] | 53 | }; |
| 54 | |
William Tu | 1d7e2ed | 2017-12-13 16:38:55 -0800 | [diff] [blame^] | 55 | struct erspan_base_hdr { |
William Tu | 84e54fe | 2017-08-22 09:40:28 -0700 | [diff] [blame] | 56 | __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 Tu | 84e54fe | 2017-08-22 09:40:28 -0700 | [diff] [blame] | 62 | }; |
| 63 | |
William Tu | 1d7e2ed | 2017-12-13 16:38:55 -0800 | [diff] [blame^] | 64 | static 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 Tu | a3222dc | 2017-11-30 11:51:27 -0800 | [diff] [blame] | 70 | static 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 | |
| 79 | static 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 Tu | 1d7e2ed | 2017-12-13 16:38:55 -0800 | [diff] [blame^] | 85 | struct erspan_base_hdr *ershdr; |
| 86 | struct erspan_metadata *ersmd; |
William Tu | a3222dc | 2017-11-30 11:51:27 -0800 | [diff] [blame] | 87 | 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 Tu | 1d7e2ed | 2017-12-13 16:38:55 -0800 | [diff] [blame^] | 109 | 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 Tu | a3222dc | 2017-11-30 11:51:27 -0800 | [diff] [blame] | 112 | |
William Tu | 1d7e2ed | 2017-12-13 16:38:55 -0800 | [diff] [blame^] | 113 | /* Build base header */ |
William Tu | a3222dc | 2017-11-30 11:51:27 -0800 | [diff] [blame] | 114 | 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 Tu | 1d7e2ed | 2017-12-13 16:38:55 -0800 | [diff] [blame^] | 120 | |
| 121 | /* Build metadata */ |
| 122 | ersmd = (struct erspan_metadata *)(ershdr + 1); |
| 123 | ersmd->u.index = htonl(index & INDEX_MASK); |
William Tu | a3222dc | 2017-11-30 11:51:27 -0800 | [diff] [blame] | 124 | } |
| 125 | |
William Tu | 84e54fe | 2017-08-22 09:40:28 -0700 | [diff] [blame] | 126 | #endif |