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