Jiri Pirko | 1bd758e | 2015-05-12 14:56:07 +0200 | [diff] [blame] | 1 | #ifndef _NET_FLOW_DISSECTOR_H |
| 2 | #define _NET_FLOW_DISSECTOR_H |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 3 | |
Jiri Pirko | c3f8eae | 2015-05-12 14:56:17 +0200 | [diff] [blame] | 4 | #include <linux/types.h> |
| 5 | #include <linux/skbuff.h> |
Jiri Pirko | b924933 | 2015-05-12 14:56:18 +0200 | [diff] [blame] | 6 | #include <linux/in6.h> |
Jiri Pirko | 67a900c | 2015-05-12 14:56:19 +0200 | [diff] [blame] | 7 | #include <uapi/linux/if_ether.h> |
Jiri Pirko | c3f8eae | 2015-05-12 14:56:17 +0200 | [diff] [blame] | 8 | |
Jiri Pirko | fbff949 | 2015-05-12 14:56:15 +0200 | [diff] [blame] | 9 | /** |
Tom Herbert | 42aecaa | 2015-06-04 09:16:39 -0700 | [diff] [blame] | 10 | * struct flow_dissector_key_control: |
| 11 | * @thoff: Transport header offset |
| 12 | */ |
| 13 | struct flow_dissector_key_control { |
| 14 | u16 thoff; |
Tom Herbert | c3f8324 | 2015-06-04 09:16:40 -0700 | [diff] [blame] | 15 | u16 addr_type; |
Tom Herbert | 42aecaa | 2015-06-04 09:16:39 -0700 | [diff] [blame] | 16 | }; |
| 17 | |
| 18 | /** |
Jiri Pirko | fbff949 | 2015-05-12 14:56:15 +0200 | [diff] [blame] | 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 { |
Jiri Pirko | fbff949 | 2015-05-12 14:56:15 +0200 | [diff] [blame] | 25 | __be16 n_proto; |
| 26 | u8 ip_proto; |
Tom Herbert | 42aecaa | 2015-06-04 09:16:39 -0700 | [diff] [blame] | 27 | u8 padding; |
Jiri Pirko | fbff949 | 2015-05-12 14:56:15 +0200 | [diff] [blame] | 28 | }; |
| 29 | |
Tom Herbert | d34af82 | 2015-06-04 09:16:43 -0700 | [diff] [blame] | 30 | struct flow_dissector_key_tags { |
Tom Herbert | 87ee9e5 | 2015-06-04 09:16:44 -0700 | [diff] [blame] | 31 | u32 vlan_id:12, |
| 32 | flow_label:20; |
Tom Herbert | d34af82 | 2015-06-04 09:16:43 -0700 | [diff] [blame] | 33 | }; |
| 34 | |
Tom Herbert | 1fdd512 | 2015-06-04 09:16:45 -0700 | [diff] [blame^] | 35 | struct flow_dissector_key_keyid { |
| 36 | __be32 keyid; |
| 37 | }; |
| 38 | |
Jiri Pirko | fbff949 | 2015-05-12 14:56:15 +0200 | [diff] [blame] | 39 | /** |
Tom Herbert | c3f8324 | 2015-06-04 09:16:40 -0700 | [diff] [blame] | 40 | * struct flow_dissector_key_ipv4_addrs: |
| 41 | * @src: source ip address |
| 42 | * @dst: destination ip address |
Jiri Pirko | fbff949 | 2015-05-12 14:56:15 +0200 | [diff] [blame] | 43 | */ |
Tom Herbert | c3f8324 | 2015-06-04 09:16:40 -0700 | [diff] [blame] | 44 | struct flow_dissector_key_ipv4_addrs { |
Jiri Pirko | fbff949 | 2015-05-12 14:56:15 +0200 | [diff] [blame] | 45 | /* (src,dst) must be grouped, in the same way than in IP header */ |
| 46 | __be32 src; |
| 47 | __be32 dst; |
| 48 | }; |
| 49 | |
| 50 | /** |
Tom Herbert | c3f8324 | 2015-06-04 09:16:40 -0700 | [diff] [blame] | 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 | /** |
Tom Herbert | 9f24908 | 2015-06-04 09:16:41 -0700 | [diff] [blame] | 62 | * struct flow_dissector_key_tipc_addrs: |
| 63 | * @srcnode: source node address |
| 64 | */ |
| 65 | struct flow_dissector_key_tipc_addrs { |
| 66 | __be32 srcnode; |
| 67 | }; |
| 68 | |
| 69 | /** |
Tom Herbert | c3f8324 | 2015-06-04 09:16:40 -0700 | [diff] [blame] | 70 | * struct flow_dissector_key_addrs: |
| 71 | * @v4addrs: IPv4 addresses |
| 72 | * @v6addrs: IPv6 addresses |
| 73 | */ |
| 74 | struct flow_dissector_key_addrs { |
| 75 | union { |
| 76 | struct flow_dissector_key_ipv4_addrs v4addrs; |
| 77 | struct flow_dissector_key_ipv6_addrs v6addrs; |
Tom Herbert | 9f24908 | 2015-06-04 09:16:41 -0700 | [diff] [blame] | 78 | struct flow_dissector_key_tipc_addrs tipcaddrs; |
Tom Herbert | c3f8324 | 2015-06-04 09:16:40 -0700 | [diff] [blame] | 79 | }; |
| 80 | }; |
| 81 | |
| 82 | /** |
Jiri Pirko | fbff949 | 2015-05-12 14:56:15 +0200 | [diff] [blame] | 83 | * flow_dissector_key_tp_ports: |
| 84 | * @ports: port numbers of Transport header |
Jiri Pirko | 59346af | 2015-05-12 14:56:20 +0200 | [diff] [blame] | 85 | * src: source port number |
| 86 | * dst: destination port number |
Jiri Pirko | fbff949 | 2015-05-12 14:56:15 +0200 | [diff] [blame] | 87 | */ |
| 88 | struct flow_dissector_key_ports { |
| 89 | union { |
| 90 | __be32 ports; |
Jiri Pirko | 59346af | 2015-05-12 14:56:20 +0200 | [diff] [blame] | 91 | struct { |
| 92 | __be16 src; |
| 93 | __be16 dst; |
| 94 | }; |
Jiri Pirko | fbff949 | 2015-05-12 14:56:15 +0200 | [diff] [blame] | 95 | }; |
| 96 | }; |
| 97 | |
Jiri Pirko | b924933 | 2015-05-12 14:56:18 +0200 | [diff] [blame] | 98 | |
Jiri Pirko | 67a900c | 2015-05-12 14:56:19 +0200 | [diff] [blame] | 99 | /** |
| 100 | * struct flow_dissector_key_eth_addrs: |
| 101 | * @src: source Ethernet address |
| 102 | * @dst: destination Ethernet address |
| 103 | */ |
| 104 | struct flow_dissector_key_eth_addrs { |
| 105 | /* (dst,src) must be grouped, in the same way than in ETH header */ |
| 106 | unsigned char dst[ETH_ALEN]; |
| 107 | unsigned char src[ETH_ALEN]; |
| 108 | }; |
| 109 | |
Jiri Pirko | fbff949 | 2015-05-12 14:56:15 +0200 | [diff] [blame] | 110 | enum flow_dissector_key_id { |
Tom Herbert | 42aecaa | 2015-06-04 09:16:39 -0700 | [diff] [blame] | 111 | FLOW_DISSECTOR_KEY_CONTROL, /* struct flow_dissector_key_control */ |
Jiri Pirko | fbff949 | 2015-05-12 14:56:15 +0200 | [diff] [blame] | 112 | FLOW_DISSECTOR_KEY_BASIC, /* struct flow_dissector_key_basic */ |
Tom Herbert | c3f8324 | 2015-06-04 09:16:40 -0700 | [diff] [blame] | 113 | FLOW_DISSECTOR_KEY_IPV4_ADDRS, /* struct flow_dissector_key_ipv4_addrs */ |
| 114 | FLOW_DISSECTOR_KEY_IPV6_ADDRS, /* struct flow_dissector_key_ipv6_addrs */ |
Jiri Pirko | fbff949 | 2015-05-12 14:56:15 +0200 | [diff] [blame] | 115 | FLOW_DISSECTOR_KEY_PORTS, /* struct flow_dissector_key_ports */ |
Jiri Pirko | 67a900c | 2015-05-12 14:56:19 +0200 | [diff] [blame] | 116 | FLOW_DISSECTOR_KEY_ETH_ADDRS, /* struct flow_dissector_key_eth_addrs */ |
Tom Herbert | 9f24908 | 2015-06-04 09:16:41 -0700 | [diff] [blame] | 117 | FLOW_DISSECTOR_KEY_TIPC_ADDRS, /* struct flow_dissector_key_tipc_addrs */ |
Tom Herbert | d34af82 | 2015-06-04 09:16:43 -0700 | [diff] [blame] | 118 | FLOW_DISSECTOR_KEY_VLANID, /* struct flow_dissector_key_flow_tags */ |
Tom Herbert | 87ee9e5 | 2015-06-04 09:16:44 -0700 | [diff] [blame] | 119 | FLOW_DISSECTOR_KEY_FLOW_LABEL, /* struct flow_dissector_key_flow_tags */ |
Tom Herbert | 1fdd512 | 2015-06-04 09:16:45 -0700 | [diff] [blame^] | 120 | FLOW_DISSECTOR_KEY_GRE_KEYID, /* struct flow_dissector_key_keyid */ |
Jiri Pirko | fbff949 | 2015-05-12 14:56:15 +0200 | [diff] [blame] | 121 | |
| 122 | FLOW_DISSECTOR_KEY_MAX, |
| 123 | }; |
| 124 | |
| 125 | struct flow_dissector_key { |
| 126 | enum flow_dissector_key_id key_id; |
| 127 | size_t offset; /* offset of struct flow_dissector_key_* |
| 128 | in target the struct */ |
| 129 | }; |
| 130 | |
| 131 | struct flow_dissector { |
| 132 | unsigned int used_keys; /* each bit repesents presence of one key id */ |
| 133 | unsigned short int offset[FLOW_DISSECTOR_KEY_MAX]; |
| 134 | }; |
| 135 | |
Jiri Pirko | fbff949 | 2015-05-12 14:56:15 +0200 | [diff] [blame] | 136 | void skb_flow_dissector_init(struct flow_dissector *flow_dissector, |
| 137 | const struct flow_dissector_key *key, |
| 138 | unsigned int key_count); |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 139 | |
| 140 | bool __skb_flow_dissect(const struct sk_buff *skb, |
| 141 | struct flow_dissector *flow_dissector, |
| 142 | void *target_container, |
WANG Cong | 453a940 | 2014-08-25 17:03:47 -0700 | [diff] [blame] | 143 | void *data, __be16 proto, int nhoff, int hlen); |
Jiri Pirko | 1bd758e | 2015-05-12 14:56:07 +0200 | [diff] [blame] | 144 | |
| 145 | static inline bool skb_flow_dissect(const struct sk_buff *skb, |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 146 | struct flow_dissector *flow_dissector, |
| 147 | void *target_container) |
David S. Miller | 690e36e | 2014-08-23 12:13:41 -0700 | [diff] [blame] | 148 | { |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 149 | return __skb_flow_dissect(skb, flow_dissector, target_container, |
| 150 | NULL, 0, 0, 0); |
| 151 | } |
| 152 | |
| 153 | struct flow_keys { |
Tom Herbert | 42aecaa | 2015-06-04 09:16:39 -0700 | [diff] [blame] | 154 | struct flow_dissector_key_control control; |
| 155 | #define FLOW_KEYS_HASH_START_FIELD basic |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 156 | struct flow_dissector_key_basic basic; |
Tom Herbert | d34af82 | 2015-06-04 09:16:43 -0700 | [diff] [blame] | 157 | struct flow_dissector_key_tags tags; |
Tom Herbert | 1fdd512 | 2015-06-04 09:16:45 -0700 | [diff] [blame^] | 158 | struct flow_dissector_key_keyid keyid; |
Tom Herbert | 42aecaa | 2015-06-04 09:16:39 -0700 | [diff] [blame] | 159 | struct flow_dissector_key_ports ports; |
| 160 | struct flow_dissector_key_addrs addrs; |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 161 | }; |
| 162 | |
Tom Herbert | 42aecaa | 2015-06-04 09:16:39 -0700 | [diff] [blame] | 163 | #define FLOW_KEYS_HASH_OFFSET \ |
| 164 | offsetof(struct flow_keys, FLOW_KEYS_HASH_START_FIELD) |
| 165 | |
Tom Herbert | c3f8324 | 2015-06-04 09:16:40 -0700 | [diff] [blame] | 166 | __be32 flow_get_u32_src(const struct flow_keys *flow); |
| 167 | __be32 flow_get_u32_dst(const struct flow_keys *flow); |
| 168 | |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 169 | extern struct flow_dissector flow_keys_dissector; |
| 170 | extern struct flow_dissector flow_keys_buf_dissector; |
| 171 | |
| 172 | static inline bool skb_flow_dissect_flow_keys(const struct sk_buff *skb, |
| 173 | struct flow_keys *flow) |
| 174 | { |
| 175 | memset(flow, 0, sizeof(*flow)); |
| 176 | return __skb_flow_dissect(skb, &flow_keys_dissector, flow, |
| 177 | NULL, 0, 0, 0); |
| 178 | } |
| 179 | |
| 180 | static inline bool skb_flow_dissect_flow_keys_buf(struct flow_keys *flow, |
| 181 | void *data, __be16 proto, |
| 182 | int nhoff, int hlen) |
| 183 | { |
| 184 | memset(flow, 0, sizeof(*flow)); |
| 185 | return __skb_flow_dissect(NULL, &flow_keys_buf_dissector, flow, |
| 186 | data, proto, nhoff, hlen); |
David S. Miller | 690e36e | 2014-08-23 12:13:41 -0700 | [diff] [blame] | 187 | } |
Jiri Pirko | 1bd758e | 2015-05-12 14:56:07 +0200 | [diff] [blame] | 188 | |
David S. Miller | 690e36e | 2014-08-23 12:13:41 -0700 | [diff] [blame] | 189 | __be32 __skb_flow_get_ports(const struct sk_buff *skb, int thoff, u8 ip_proto, |
| 190 | void *data, int hlen_proto); |
Jiri Pirko | 1bd758e | 2015-05-12 14:56:07 +0200 | [diff] [blame] | 191 | |
| 192 | static inline __be32 skb_flow_get_ports(const struct sk_buff *skb, |
| 193 | int thoff, u8 ip_proto) |
David S. Miller | 690e36e | 2014-08-23 12:13:41 -0700 | [diff] [blame] | 194 | { |
| 195 | return __skb_flow_get_ports(skb, thoff, ip_proto, NULL, 0); |
| 196 | } |
Jiri Pirko | 1bd758e | 2015-05-12 14:56:07 +0200 | [diff] [blame] | 197 | |
Tom Herbert | 5ed20a6 | 2014-07-01 21:32:05 -0700 | [diff] [blame] | 198 | u32 flow_hash_from_keys(struct flow_keys *keys); |
Jiri Pirko | 9c684b5 | 2015-05-12 14:56:11 +0200 | [diff] [blame] | 199 | void __skb_get_hash(struct sk_buff *skb); |
Jiri Pirko | 10b89ee4 | 2015-05-12 14:56:09 +0200 | [diff] [blame] | 200 | u32 skb_get_poff(const struct sk_buff *skb); |
| 201 | u32 __skb_get_poff(const struct sk_buff *skb, void *data, |
| 202 | const struct flow_keys *keys, int hlen); |
Jiri Pirko | 1bd758e | 2015-05-12 14:56:07 +0200 | [diff] [blame] | 203 | |
Tom Herbert | 2f59e1e | 2015-05-01 11:30:17 -0700 | [diff] [blame] | 204 | /* struct flow_keys_digest: |
| 205 | * |
| 206 | * This structure is used to hold a digest of the full flow keys. This is a |
| 207 | * larger "hash" of a flow to allow definitively matching specific flows where |
| 208 | * the 32 bit skb->hash is not large enough. The size is limited to 16 bytes so |
| 209 | * that it can by used in CB of skb (see sch_choke for an example). |
| 210 | */ |
| 211 | #define FLOW_KEYS_DIGEST_LEN 16 |
| 212 | struct flow_keys_digest { |
| 213 | u8 data[FLOW_KEYS_DIGEST_LEN]; |
| 214 | }; |
| 215 | |
| 216 | void make_flow_keys_digest(struct flow_keys_digest *digest, |
| 217 | const struct flow_keys *flow); |
| 218 | |
Eric Dumazet | 0744dd0 | 2011-11-28 05:22:18 +0000 | [diff] [blame] | 219 | #endif |