blob: c4f31666afd24695b9d83688f0800ebcc017114b [file] [log] [blame]
Jiri Pirko1bd758e2015-05-12 14:56:07 +02001#ifndef _NET_FLOW_DISSECTOR_H
2#define _NET_FLOW_DISSECTOR_H
Eric Dumazet0744dd02011-11-28 05:22:18 +00003
Jiri Pirkoc3f8eae2015-05-12 14:56:17 +02004#include <linux/types.h>
Jiri Pirkob9249332015-05-12 14:56:18 +02005#include <linux/in6.h>
Jiri Pirko67a900c2015-05-12 14:56:19 +02006#include <uapi/linux/if_ether.h>
Jiri Pirkoc3f8eae2015-05-12 14:56:17 +02007
Jiri Pirkofbff9492015-05-12 14:56:15 +02008/**
Tom Herbert42aecaa2015-06-04 09:16:39 -07009 * struct flow_dissector_key_control:
10 * @thoff: Transport header offset
11 */
12struct flow_dissector_key_control {
13 u16 thoff;
Tom Herbertc3f83242015-06-04 09:16:40 -070014 u16 addr_type;
David S. Miller4b369932015-09-01 16:46:08 -070015 u32 flags;
Tom Herbert42aecaa2015-06-04 09:16:39 -070016};
17
David S. Miller4b369932015-09-01 16:46:08 -070018#define FLOW_DIS_IS_FRAGMENT BIT(0)
19#define FLOW_DIS_FIRST_FRAG BIT(1)
20#define FLOW_DIS_ENCAPSULATION BIT(2)
21
Tom Herbert42aecaa2015-06-04 09:16:39 -070022/**
Jiri Pirkofbff9492015-05-12 14:56:15 +020023 * struct flow_dissector_key_basic:
24 * @thoff: Transport header offset
25 * @n_proto: Network header protocol (eg. IPv4/IPv6)
26 * @ip_proto: Transport header protocol (eg. TCP/UDP)
27 */
28struct flow_dissector_key_basic {
Jiri Pirkofbff9492015-05-12 14:56:15 +020029 __be16 n_proto;
30 u8 ip_proto;
Tom Herbert42aecaa2015-06-04 09:16:39 -070031 u8 padding;
Jiri Pirkofbff9492015-05-12 14:56:15 +020032};
33
Tom Herbertd34af822015-06-04 09:16:43 -070034struct flow_dissector_key_tags {
Hadar Hen Zionf6a66922016-08-17 13:36:11 +030035 u32 flow_label;
36};
37
38struct flow_dissector_key_vlan {
39 u16 vlan_id:12,
40 vlan_priority:3;
41 u16 padding;
Tom Herbertd34af822015-06-04 09:16:43 -070042};
43
Tom Herbert1fdd5122015-06-04 09:16:45 -070044struct flow_dissector_key_keyid {
45 __be32 keyid;
46};
47
Jiri Pirkofbff9492015-05-12 14:56:15 +020048/**
Tom Herbertc3f83242015-06-04 09:16:40 -070049 * struct flow_dissector_key_ipv4_addrs:
50 * @src: source ip address
51 * @dst: destination ip address
Jiri Pirkofbff9492015-05-12 14:56:15 +020052 */
Tom Herbertc3f83242015-06-04 09:16:40 -070053struct flow_dissector_key_ipv4_addrs {
Jiri Pirkofbff9492015-05-12 14:56:15 +020054 /* (src,dst) must be grouped, in the same way than in IP header */
55 __be32 src;
56 __be32 dst;
57};
58
59/**
Tom Herbertc3f83242015-06-04 09:16:40 -070060 * struct flow_dissector_key_ipv6_addrs:
61 * @src: source ip address
62 * @dst: destination ip address
63 */
64struct flow_dissector_key_ipv6_addrs {
65 /* (src,dst) must be grouped, in the same way than in IP header */
66 struct in6_addr src;
67 struct in6_addr dst;
68};
69
70/**
Tom Herbert9f249082015-06-04 09:16:41 -070071 * struct flow_dissector_key_tipc_addrs:
72 * @srcnode: source node address
73 */
74struct flow_dissector_key_tipc_addrs {
75 __be32 srcnode;
76};
77
78/**
Tom Herbertc3f83242015-06-04 09:16:40 -070079 * struct flow_dissector_key_addrs:
80 * @v4addrs: IPv4 addresses
81 * @v6addrs: IPv6 addresses
82 */
83struct flow_dissector_key_addrs {
84 union {
85 struct flow_dissector_key_ipv4_addrs v4addrs;
86 struct flow_dissector_key_ipv6_addrs v6addrs;
Tom Herbert9f249082015-06-04 09:16:41 -070087 struct flow_dissector_key_tipc_addrs tipcaddrs;
Tom Herbertc3f83242015-06-04 09:16:40 -070088 };
89};
90
91/**
Jiri Pirkofbff9492015-05-12 14:56:15 +020092 * flow_dissector_key_tp_ports:
93 * @ports: port numbers of Transport header
Jiri Pirko59346af2015-05-12 14:56:20 +020094 * src: source port number
95 * dst: destination port number
Jiri Pirkofbff9492015-05-12 14:56:15 +020096 */
97struct flow_dissector_key_ports {
98 union {
99 __be32 ports;
Jiri Pirko59346af2015-05-12 14:56:20 +0200100 struct {
101 __be16 src;
102 __be16 dst;
103 };
Jiri Pirkofbff9492015-05-12 14:56:15 +0200104 };
105};
106
Jiri Pirkob9249332015-05-12 14:56:18 +0200107
Jiri Pirko67a900c2015-05-12 14:56:19 +0200108/**
109 * struct flow_dissector_key_eth_addrs:
110 * @src: source Ethernet address
111 * @dst: destination Ethernet address
112 */
113struct flow_dissector_key_eth_addrs {
114 /* (dst,src) must be grouped, in the same way than in ETH header */
115 unsigned char dst[ETH_ALEN];
116 unsigned char src[ETH_ALEN];
117};
118
Jiri Pirkofbff9492015-05-12 14:56:15 +0200119enum flow_dissector_key_id {
Tom Herbert42aecaa2015-06-04 09:16:39 -0700120 FLOW_DISSECTOR_KEY_CONTROL, /* struct flow_dissector_key_control */
Jiri Pirkofbff9492015-05-12 14:56:15 +0200121 FLOW_DISSECTOR_KEY_BASIC, /* struct flow_dissector_key_basic */
Tom Herbertc3f83242015-06-04 09:16:40 -0700122 FLOW_DISSECTOR_KEY_IPV4_ADDRS, /* struct flow_dissector_key_ipv4_addrs */
123 FLOW_DISSECTOR_KEY_IPV6_ADDRS, /* struct flow_dissector_key_ipv6_addrs */
Jiri Pirkofbff9492015-05-12 14:56:15 +0200124 FLOW_DISSECTOR_KEY_PORTS, /* struct flow_dissector_key_ports */
Jiri Pirko67a900c2015-05-12 14:56:19 +0200125 FLOW_DISSECTOR_KEY_ETH_ADDRS, /* struct flow_dissector_key_eth_addrs */
Tom Herbert9f249082015-06-04 09:16:41 -0700126 FLOW_DISSECTOR_KEY_TIPC_ADDRS, /* struct flow_dissector_key_tipc_addrs */
Hadar Hen Zionf6a66922016-08-17 13:36:11 +0300127 FLOW_DISSECTOR_KEY_VLAN, /* struct flow_dissector_key_flow_vlan */
Tom Herbert87ee9e52015-06-04 09:16:44 -0700128 FLOW_DISSECTOR_KEY_FLOW_LABEL, /* struct flow_dissector_key_flow_tags */
Tom Herbert1fdd5122015-06-04 09:16:45 -0700129 FLOW_DISSECTOR_KEY_GRE_KEYID, /* struct flow_dissector_key_keyid */
Tom Herbertb3baa0f2015-06-04 09:16:46 -0700130 FLOW_DISSECTOR_KEY_MPLS_ENTROPY, /* struct flow_dissector_key_keyid */
Hadar Hen Zion9ba6a9a2016-11-07 15:14:37 +0200131 FLOW_DISSECTOR_KEY_ENC_KEYID, /* struct flow_dissector_key_keyid */
132 FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS, /* struct flow_dissector_key_ipv4_addrs */
133 FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS, /* struct flow_dissector_key_ipv6_addrs */
134 FLOW_DISSECTOR_KEY_ENC_CONTROL, /* struct flow_dissector_key_control */
Hadar Hen Zionf4d997f2016-11-07 15:14:39 +0200135 FLOW_DISSECTOR_KEY_ENC_PORTS, /* struct flow_dissector_key_ports */
Jiri Pirkofbff9492015-05-12 14:56:15 +0200136
137 FLOW_DISSECTOR_KEY_MAX,
138};
139
Tom Herbert807e1652015-09-01 09:24:28 -0700140#define FLOW_DISSECTOR_F_PARSE_1ST_FRAG BIT(0)
Tom Herbert8306b682015-09-01 09:24:30 -0700141#define FLOW_DISSECTOR_F_STOP_AT_L3 BIT(1)
Tom Herbert872b1ab2015-09-01 09:24:31 -0700142#define FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL BIT(2)
Tom Herbert823b9692015-09-01 09:24:32 -0700143#define FLOW_DISSECTOR_F_STOP_AT_ENCAP BIT(3)
Tom Herbert807e1652015-09-01 09:24:28 -0700144
Jiri Pirkofbff9492015-05-12 14:56:15 +0200145struct flow_dissector_key {
146 enum flow_dissector_key_id key_id;
147 size_t offset; /* offset of struct flow_dissector_key_*
148 in target the struct */
149};
150
151struct flow_dissector {
152 unsigned int used_keys; /* each bit repesents presence of one key id */
153 unsigned short int offset[FLOW_DISSECTOR_KEY_MAX];
154};
155
Jiri Pirko06635a32015-05-12 14:56:16 +0200156struct flow_keys {
Tom Herbert42aecaa2015-06-04 09:16:39 -0700157 struct flow_dissector_key_control control;
158#define FLOW_KEYS_HASH_START_FIELD basic
Jiri Pirko06635a32015-05-12 14:56:16 +0200159 struct flow_dissector_key_basic basic;
Tom Herbertd34af822015-06-04 09:16:43 -0700160 struct flow_dissector_key_tags tags;
Hadar Hen Zionf6a66922016-08-17 13:36:11 +0300161 struct flow_dissector_key_vlan vlan;
Tom Herbert1fdd5122015-06-04 09:16:45 -0700162 struct flow_dissector_key_keyid keyid;
Tom Herbert42aecaa2015-06-04 09:16:39 -0700163 struct flow_dissector_key_ports ports;
164 struct flow_dissector_key_addrs addrs;
Jiri Pirko06635a32015-05-12 14:56:16 +0200165};
166
Tom Herbert42aecaa2015-06-04 09:16:39 -0700167#define FLOW_KEYS_HASH_OFFSET \
168 offsetof(struct flow_keys, FLOW_KEYS_HASH_START_FIELD)
169
Tom Herbertc3f83242015-06-04 09:16:40 -0700170__be32 flow_get_u32_src(const struct flow_keys *flow);
171__be32 flow_get_u32_dst(const struct flow_keys *flow);
172
Jiri Pirko06635a32015-05-12 14:56:16 +0200173extern struct flow_dissector flow_keys_dissector;
174extern struct flow_dissector flow_keys_buf_dissector;
175
Tom Herbert2f59e1e2015-05-01 11:30:17 -0700176/* struct flow_keys_digest:
177 *
178 * This structure is used to hold a digest of the full flow keys. This is a
179 * larger "hash" of a flow to allow definitively matching specific flows where
180 * the 32 bit skb->hash is not large enough. The size is limited to 16 bytes so
181 * that it can by used in CB of skb (see sch_choke for an example).
182 */
183#define FLOW_KEYS_DIGEST_LEN 16
184struct flow_keys_digest {
185 u8 data[FLOW_KEYS_DIGEST_LEN];
186};
187
188void make_flow_keys_digest(struct flow_keys_digest *digest,
189 const struct flow_keys *flow);
190
Gao Feng66fdd052016-08-31 11:16:22 +0800191static inline bool flow_keys_have_l4(const struct flow_keys *keys)
Tom Herbertbcc83832015-09-01 09:24:24 -0700192{
193 return (keys->ports.ports || keys->tags.flow_label);
194}
195
Tom Herbertc6cc1ca2015-09-01 09:24:25 -0700196u32 flow_hash_from_keys(struct flow_keys *keys);
197
Amir Vadai8de2d792016-03-08 12:42:30 +0200198static inline bool dissector_uses_key(const struct flow_dissector *flow_dissector,
199 enum flow_dissector_key_id key_id)
200{
201 return flow_dissector->used_keys & (1 << key_id);
202}
203
204static inline void *skb_flow_dissector_target(struct flow_dissector *flow_dissector,
205 enum flow_dissector_key_id key_id,
206 void *target_container)
207{
208 return ((char *)target_container) + flow_dissector->offset[key_id];
209}
210
Eric Dumazet0744dd02011-11-28 05:22:18 +0000211#endif