flow_dissector.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. #ifndef _NET_FLOW_DISSECTOR_H
  2. #define _NET_FLOW_DISSECTOR_H
  3. #include <linux/types.h>
  4. #include <linux/in6.h>
  5. #include <uapi/linux/if_ether.h>
  6. /**
  7. * struct flow_dissector_key_control:
  8. * @thoff: Transport header offset
  9. */
  10. struct flow_dissector_key_control {
  11. u16 thoff;
  12. u16 addr_type;
  13. u32 flags;
  14. };
  15. #define FLOW_DIS_IS_FRAGMENT BIT(0)
  16. #define FLOW_DIS_FIRST_FRAG BIT(1)
  17. #define FLOW_DIS_ENCAPSULATION BIT(2)
  18. /**
  19. * struct flow_dissector_key_basic:
  20. * @thoff: Transport header offset
  21. * @n_proto: Network header protocol (eg. IPv4/IPv6)
  22. * @ip_proto: Transport header protocol (eg. TCP/UDP)
  23. */
  24. struct flow_dissector_key_basic {
  25. __be16 n_proto;
  26. u8 ip_proto;
  27. u8 padding;
  28. };
  29. struct flow_dissector_key_tags {
  30. u32 flow_label;
  31. };
  32. struct flow_dissector_key_vlan {
  33. u16 vlan_id:12,
  34. vlan_priority:3;
  35. u16 padding;
  36. };
  37. struct flow_dissector_key_keyid {
  38. __be32 keyid;
  39. };
  40. /**
  41. * struct flow_dissector_key_ipv4_addrs:
  42. * @src: source ip address
  43. * @dst: destination ip address
  44. */
  45. struct flow_dissector_key_ipv4_addrs {
  46. /* (src,dst) must be grouped, in the same way than in IP header */
  47. __be32 src;
  48. __be32 dst;
  49. };
  50. /**
  51. * struct flow_dissector_key_ipv6_addrs:
  52. * @src: source ip address
  53. * @dst: destination ip address
  54. */
  55. struct flow_dissector_key_ipv6_addrs {
  56. /* (src,dst) must be grouped, in the same way than in IP header */
  57. struct in6_addr src;
  58. struct in6_addr dst;
  59. };
  60. /**
  61. * struct flow_dissector_key_tipc_addrs:
  62. * @srcnode: source node address
  63. */
  64. struct flow_dissector_key_tipc_addrs {
  65. __be32 srcnode;
  66. };
  67. /**
  68. * struct flow_dissector_key_addrs:
  69. * @v4addrs: IPv4 addresses
  70. * @v6addrs: IPv6 addresses
  71. */
  72. struct flow_dissector_key_addrs {
  73. union {
  74. struct flow_dissector_key_ipv4_addrs v4addrs;
  75. struct flow_dissector_key_ipv6_addrs v6addrs;
  76. struct flow_dissector_key_tipc_addrs tipcaddrs;
  77. };
  78. };
  79. /**
  80. * flow_dissector_key_tp_ports:
  81. * @ports: port numbers of Transport header
  82. * src: source port number
  83. * dst: destination port number
  84. */
  85. struct flow_dissector_key_ports {
  86. union {
  87. __be32 ports;
  88. struct {
  89. __be16 src;
  90. __be16 dst;
  91. };
  92. };
  93. };
  94. /**
  95. * struct flow_dissector_key_eth_addrs:
  96. * @src: source Ethernet address
  97. * @dst: destination Ethernet address
  98. */
  99. struct flow_dissector_key_eth_addrs {
  100. /* (dst,src) must be grouped, in the same way than in ETH header */
  101. unsigned char dst[ETH_ALEN];
  102. unsigned char src[ETH_ALEN];
  103. };
  104. enum flow_dissector_key_id {
  105. FLOW_DISSECTOR_KEY_CONTROL, /* struct flow_dissector_key_control */
  106. FLOW_DISSECTOR_KEY_BASIC, /* struct flow_dissector_key_basic */
  107. FLOW_DISSECTOR_KEY_IPV4_ADDRS, /* struct flow_dissector_key_ipv4_addrs */
  108. FLOW_DISSECTOR_KEY_IPV6_ADDRS, /* struct flow_dissector_key_ipv6_addrs */
  109. FLOW_DISSECTOR_KEY_PORTS, /* struct flow_dissector_key_ports */
  110. FLOW_DISSECTOR_KEY_ETH_ADDRS, /* struct flow_dissector_key_eth_addrs */
  111. FLOW_DISSECTOR_KEY_TIPC_ADDRS, /* struct flow_dissector_key_tipc_addrs */
  112. FLOW_DISSECTOR_KEY_VLAN, /* struct flow_dissector_key_flow_vlan */
  113. FLOW_DISSECTOR_KEY_FLOW_LABEL, /* struct flow_dissector_key_flow_tags */
  114. FLOW_DISSECTOR_KEY_GRE_KEYID, /* struct flow_dissector_key_keyid */
  115. FLOW_DISSECTOR_KEY_MPLS_ENTROPY, /* struct flow_dissector_key_keyid */
  116. FLOW_DISSECTOR_KEY_MAX,
  117. };
  118. #define FLOW_DISSECTOR_F_PARSE_1ST_FRAG BIT(0)
  119. #define FLOW_DISSECTOR_F_STOP_AT_L3 BIT(1)
  120. #define FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL BIT(2)
  121. #define FLOW_DISSECTOR_F_STOP_AT_ENCAP BIT(3)
  122. struct flow_dissector_key {
  123. enum flow_dissector_key_id key_id;
  124. size_t offset; /* offset of struct flow_dissector_key_*
  125. in target the struct */
  126. };
  127. struct flow_dissector {
  128. unsigned int used_keys; /* each bit repesents presence of one key id */
  129. unsigned short int offset[FLOW_DISSECTOR_KEY_MAX];
  130. };
  131. struct flow_keys {
  132. struct flow_dissector_key_control control;
  133. #define FLOW_KEYS_HASH_START_FIELD basic
  134. struct flow_dissector_key_basic basic;
  135. struct flow_dissector_key_tags tags;
  136. struct flow_dissector_key_vlan vlan;
  137. struct flow_dissector_key_keyid keyid;
  138. struct flow_dissector_key_ports ports;
  139. struct flow_dissector_key_addrs addrs;
  140. };
  141. #define FLOW_KEYS_HASH_OFFSET \
  142. offsetof(struct flow_keys, FLOW_KEYS_HASH_START_FIELD)
  143. __be32 flow_get_u32_src(const struct flow_keys *flow);
  144. __be32 flow_get_u32_dst(const struct flow_keys *flow);
  145. extern struct flow_dissector flow_keys_dissector;
  146. extern struct flow_dissector flow_keys_buf_dissector;
  147. /* struct flow_keys_digest:
  148. *
  149. * This structure is used to hold a digest of the full flow keys. This is a
  150. * larger "hash" of a flow to allow definitively matching specific flows where
  151. * the 32 bit skb->hash is not large enough. The size is limited to 16 bytes so
  152. * that it can by used in CB of skb (see sch_choke for an example).
  153. */
  154. #define FLOW_KEYS_DIGEST_LEN 16
  155. struct flow_keys_digest {
  156. u8 data[FLOW_KEYS_DIGEST_LEN];
  157. };
  158. void make_flow_keys_digest(struct flow_keys_digest *digest,
  159. const struct flow_keys *flow);
  160. static inline bool flow_keys_have_l4(const struct flow_keys *keys)
  161. {
  162. return (keys->ports.ports || keys->tags.flow_label);
  163. }
  164. u32 flow_hash_from_keys(struct flow_keys *keys);
  165. static inline bool dissector_uses_key(const struct flow_dissector *flow_dissector,
  166. enum flow_dissector_key_id key_id)
  167. {
  168. return flow_dissector->used_keys & (1 << key_id);
  169. }
  170. static inline void *skb_flow_dissector_target(struct flow_dissector *flow_dissector,
  171. enum flow_dissector_key_id key_id,
  172. void *target_container)
  173. {
  174. return ((char *)target_container) + flow_dissector->offset[key_id];
  175. }
  176. #endif