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