erspan.h 8.3 KB

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