xref: /openbmc/linux/include/net/flow_dissector.h (revision 3a1214e8b06317b4e71cd3a36344df87b7858e19)
11bd758ebSJiri Pirko #ifndef _NET_FLOW_DISSECTOR_H
21bd758ebSJiri Pirko #define _NET_FLOW_DISSECTOR_H
31bd758ebSJiri Pirko 
4c3f8eaebSJiri Pirko #include <linux/types.h>
5b924933cSJiri Pirko #include <linux/in6.h>
667a900ccSJiri Pirko #include <uapi/linux/if_ether.h>
7c3f8eaebSJiri Pirko 
8fbff949eSJiri Pirko /**
942aecaa9STom Herbert  * struct flow_dissector_key_control:
1042aecaa9STom Herbert  * @thoff: Transport header offset
1142aecaa9STom Herbert  */
1242aecaa9STom Herbert struct flow_dissector_key_control {
1342aecaa9STom Herbert 	u16	thoff;
14c3f83241STom Herbert 	u16	addr_type;
154b36993dSDavid S. Miller 	u32	flags;
1642aecaa9STom Herbert };
1742aecaa9STom Herbert 
184b36993dSDavid S. Miller #define FLOW_DIS_IS_FRAGMENT	BIT(0)
194b36993dSDavid S. Miller #define FLOW_DIS_FIRST_FRAG	BIT(1)
204b36993dSDavid S. Miller #define FLOW_DIS_ENCAPSULATION	BIT(2)
214b36993dSDavid S. Miller 
22*3a1214e8STom Herbert enum flow_dissect_ret {
23*3a1214e8STom Herbert 	FLOW_DISSECT_RET_OUT_GOOD,
24*3a1214e8STom Herbert 	FLOW_DISSECT_RET_OUT_BAD,
25*3a1214e8STom Herbert 	FLOW_DISSECT_RET_PROTO_AGAIN,
26*3a1214e8STom Herbert 	FLOW_DISSECT_RET_IPPROTO_AGAIN,
27*3a1214e8STom Herbert 	FLOW_DISSECT_RET_CONTINUE,
28*3a1214e8STom Herbert };
29*3a1214e8STom Herbert 
3042aecaa9STom Herbert /**
31fbff949eSJiri Pirko  * struct flow_dissector_key_basic:
32fbff949eSJiri Pirko  * @thoff: Transport header offset
33fbff949eSJiri Pirko  * @n_proto: Network header protocol (eg. IPv4/IPv6)
34fbff949eSJiri Pirko  * @ip_proto: Transport header protocol (eg. TCP/UDP)
35fbff949eSJiri Pirko  */
36fbff949eSJiri Pirko struct flow_dissector_key_basic {
37fbff949eSJiri Pirko 	__be16	n_proto;
38fbff949eSJiri Pirko 	u8	ip_proto;
3942aecaa9STom Herbert 	u8	padding;
40fbff949eSJiri Pirko };
41fbff949eSJiri Pirko 
42d34af823STom Herbert struct flow_dissector_key_tags {
43f6a66927SHadar Hen Zion 	u32	flow_label;
44f6a66927SHadar Hen Zion };
45f6a66927SHadar Hen Zion 
46f6a66927SHadar Hen Zion struct flow_dissector_key_vlan {
47f6a66927SHadar Hen Zion 	u16	vlan_id:12,
48f6a66927SHadar Hen Zion 		vlan_priority:3;
49f6a66927SHadar Hen Zion 	u16	padding;
50d34af823STom Herbert };
51d34af823STom Herbert 
52029c1ecbSBenjamin LaHaise struct flow_dissector_key_mpls {
53029c1ecbSBenjamin LaHaise 	u32	mpls_ttl:8,
54029c1ecbSBenjamin LaHaise 		mpls_bos:1,
55029c1ecbSBenjamin LaHaise 		mpls_tc:3,
56029c1ecbSBenjamin LaHaise 		mpls_label:20;
57029c1ecbSBenjamin LaHaise };
58029c1ecbSBenjamin LaHaise 
591fdd512cSTom Herbert struct flow_dissector_key_keyid {
601fdd512cSTom Herbert 	__be32	keyid;
611fdd512cSTom Herbert };
621fdd512cSTom Herbert 
63fbff949eSJiri Pirko /**
64c3f83241STom Herbert  * struct flow_dissector_key_ipv4_addrs:
65c3f83241STom Herbert  * @src: source ip address
66c3f83241STom Herbert  * @dst: destination ip address
67fbff949eSJiri Pirko  */
68c3f83241STom Herbert struct flow_dissector_key_ipv4_addrs {
69fbff949eSJiri Pirko 	/* (src,dst) must be grouped, in the same way than in IP header */
70fbff949eSJiri Pirko 	__be32 src;
71fbff949eSJiri Pirko 	__be32 dst;
72fbff949eSJiri Pirko };
73fbff949eSJiri Pirko 
74fbff949eSJiri Pirko /**
75c3f83241STom Herbert  * struct flow_dissector_key_ipv6_addrs:
76c3f83241STom Herbert  * @src: source ip address
77c3f83241STom Herbert  * @dst: destination ip address
78c3f83241STom Herbert  */
79c3f83241STom Herbert struct flow_dissector_key_ipv6_addrs {
80c3f83241STom Herbert 	/* (src,dst) must be grouped, in the same way than in IP header */
81c3f83241STom Herbert 	struct in6_addr src;
82c3f83241STom Herbert 	struct in6_addr dst;
83c3f83241STom Herbert };
84c3f83241STom Herbert 
85c3f83241STom Herbert /**
869f249089STom Herbert  * struct flow_dissector_key_tipc_addrs:
879f249089STom Herbert  * @srcnode: source node address
889f249089STom Herbert  */
899f249089STom Herbert struct flow_dissector_key_tipc_addrs {
909f249089STom Herbert 	__be32 srcnode;
919f249089STom Herbert };
929f249089STom Herbert 
939f249089STom Herbert /**
94c3f83241STom Herbert  * struct flow_dissector_key_addrs:
95c3f83241STom Herbert  * @v4addrs: IPv4 addresses
96c3f83241STom Herbert  * @v6addrs: IPv6 addresses
97c3f83241STom Herbert  */
98c3f83241STom Herbert struct flow_dissector_key_addrs {
99c3f83241STom Herbert 	union {
100c3f83241STom Herbert 		struct flow_dissector_key_ipv4_addrs v4addrs;
101c3f83241STom Herbert 		struct flow_dissector_key_ipv6_addrs v6addrs;
1029f249089STom Herbert 		struct flow_dissector_key_tipc_addrs tipcaddrs;
103c3f83241STom Herbert 	};
104c3f83241STom Herbert };
105c3f83241STom Herbert 
106c3f83241STom Herbert /**
10755733350SSimon Horman  * flow_dissector_key_arp:
10855733350SSimon Horman  *	@ports: Operation, source and target addresses for an ARP header
10955733350SSimon Horman  *              for Ethernet hardware addresses and IPv4 protocol addresses
11055733350SSimon Horman  *		sip: Sender IP address
11155733350SSimon Horman  *		tip: Target IP address
11255733350SSimon Horman  *		op:  Operation
11355733350SSimon Horman  *		sha: Sender hardware address
11455733350SSimon Horman  *		tpa: Target hardware address
11555733350SSimon Horman  */
11655733350SSimon Horman struct flow_dissector_key_arp {
11755733350SSimon Horman 	__u32 sip;
11855733350SSimon Horman 	__u32 tip;
11955733350SSimon Horman 	__u8 op;
12055733350SSimon Horman 	unsigned char sha[ETH_ALEN];
12155733350SSimon Horman 	unsigned char tha[ETH_ALEN];
12255733350SSimon Horman };
12355733350SSimon Horman 
12455733350SSimon Horman /**
125fbff949eSJiri Pirko  * flow_dissector_key_tp_ports:
126fbff949eSJiri Pirko  *	@ports: port numbers of Transport header
12759346afeSJiri Pirko  *		src: source port number
12859346afeSJiri Pirko  *		dst: destination port number
129fbff949eSJiri Pirko  */
130fbff949eSJiri Pirko struct flow_dissector_key_ports {
131fbff949eSJiri Pirko 	union {
132fbff949eSJiri Pirko 		__be32 ports;
13359346afeSJiri Pirko 		struct {
13459346afeSJiri Pirko 			__be16 src;
13559346afeSJiri Pirko 			__be16 dst;
13659346afeSJiri Pirko 		};
137fbff949eSJiri Pirko 	};
138fbff949eSJiri Pirko };
139fbff949eSJiri Pirko 
140972d3876SSimon Horman /**
141972d3876SSimon Horman  * flow_dissector_key_icmp:
142972d3876SSimon Horman  *	@ports: type and code of ICMP header
143972d3876SSimon Horman  *		icmp: ICMP type (high) and code (low)
144972d3876SSimon Horman  *		type: ICMP type
145972d3876SSimon Horman  *		code: ICMP code
146972d3876SSimon Horman  */
147972d3876SSimon Horman struct flow_dissector_key_icmp {
148972d3876SSimon Horman 	union {
149972d3876SSimon Horman 		__be16 icmp;
150972d3876SSimon Horman 		struct {
151972d3876SSimon Horman 			u8 type;
152972d3876SSimon Horman 			u8 code;
153972d3876SSimon Horman 		};
154972d3876SSimon Horman 	};
155972d3876SSimon Horman };
156b924933cSJiri Pirko 
15767a900ccSJiri Pirko /**
15867a900ccSJiri Pirko  * struct flow_dissector_key_eth_addrs:
15967a900ccSJiri Pirko  * @src: source Ethernet address
16067a900ccSJiri Pirko  * @dst: destination Ethernet address
16167a900ccSJiri Pirko  */
16267a900ccSJiri Pirko struct flow_dissector_key_eth_addrs {
16367a900ccSJiri Pirko 	/* (dst,src) must be grouped, in the same way than in ETH header */
16467a900ccSJiri Pirko 	unsigned char dst[ETH_ALEN];
16567a900ccSJiri Pirko 	unsigned char src[ETH_ALEN];
16667a900ccSJiri Pirko };
16767a900ccSJiri Pirko 
168ac4bb5deSJiri Pirko /**
169ac4bb5deSJiri Pirko  * struct flow_dissector_key_tcp:
170ac4bb5deSJiri Pirko  * @flags: flags
171ac4bb5deSJiri Pirko  */
172ac4bb5deSJiri Pirko struct flow_dissector_key_tcp {
173ac4bb5deSJiri Pirko 	__be16 flags;
174ac4bb5deSJiri Pirko };
175ac4bb5deSJiri Pirko 
176518d8a2eSOr Gerlitz /**
177518d8a2eSOr Gerlitz  * struct flow_dissector_key_ip:
178518d8a2eSOr Gerlitz  * @tos: tos
179518d8a2eSOr Gerlitz  * @ttl: ttl
180518d8a2eSOr Gerlitz  */
181518d8a2eSOr Gerlitz struct flow_dissector_key_ip {
182518d8a2eSOr Gerlitz 	__u8	tos;
183518d8a2eSOr Gerlitz 	__u8	ttl;
184518d8a2eSOr Gerlitz };
185518d8a2eSOr Gerlitz 
186fbff949eSJiri Pirko enum flow_dissector_key_id {
18742aecaa9STom Herbert 	FLOW_DISSECTOR_KEY_CONTROL, /* struct flow_dissector_key_control */
188fbff949eSJiri Pirko 	FLOW_DISSECTOR_KEY_BASIC, /* struct flow_dissector_key_basic */
189c3f83241STom Herbert 	FLOW_DISSECTOR_KEY_IPV4_ADDRS, /* struct flow_dissector_key_ipv4_addrs */
190c3f83241STom Herbert 	FLOW_DISSECTOR_KEY_IPV6_ADDRS, /* struct flow_dissector_key_ipv6_addrs */
191fbff949eSJiri Pirko 	FLOW_DISSECTOR_KEY_PORTS, /* struct flow_dissector_key_ports */
192972d3876SSimon Horman 	FLOW_DISSECTOR_KEY_ICMP, /* struct flow_dissector_key_icmp */
19367a900ccSJiri Pirko 	FLOW_DISSECTOR_KEY_ETH_ADDRS, /* struct flow_dissector_key_eth_addrs */
1949f249089STom Herbert 	FLOW_DISSECTOR_KEY_TIPC_ADDRS, /* struct flow_dissector_key_tipc_addrs */
19555733350SSimon Horman 	FLOW_DISSECTOR_KEY_ARP, /* struct flow_dissector_key_arp */
196f6a66927SHadar Hen Zion 	FLOW_DISSECTOR_KEY_VLAN, /* struct flow_dissector_key_flow_vlan */
19787ee9e52STom Herbert 	FLOW_DISSECTOR_KEY_FLOW_LABEL, /* struct flow_dissector_key_flow_tags */
1981fdd512cSTom Herbert 	FLOW_DISSECTOR_KEY_GRE_KEYID, /* struct flow_dissector_key_keyid */
199b3baa0fbSTom Herbert 	FLOW_DISSECTOR_KEY_MPLS_ENTROPY, /* struct flow_dissector_key_keyid */
2009ba6a9a9SHadar Hen Zion 	FLOW_DISSECTOR_KEY_ENC_KEYID, /* struct flow_dissector_key_keyid */
2019ba6a9a9SHadar Hen Zion 	FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS, /* struct flow_dissector_key_ipv4_addrs */
2029ba6a9a9SHadar Hen Zion 	FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS, /* struct flow_dissector_key_ipv6_addrs */
2039ba6a9a9SHadar Hen Zion 	FLOW_DISSECTOR_KEY_ENC_CONTROL, /* struct flow_dissector_key_control */
204f4d997fdSHadar Hen Zion 	FLOW_DISSECTOR_KEY_ENC_PORTS, /* struct flow_dissector_key_ports */
205029c1ecbSBenjamin LaHaise 	FLOW_DISSECTOR_KEY_MPLS, /* struct flow_dissector_key_mpls */
206ac4bb5deSJiri Pirko 	FLOW_DISSECTOR_KEY_TCP, /* struct flow_dissector_key_tcp */
207518d8a2eSOr Gerlitz 	FLOW_DISSECTOR_KEY_IP, /* struct flow_dissector_key_ip */
208fbff949eSJiri Pirko 
209fbff949eSJiri Pirko 	FLOW_DISSECTOR_KEY_MAX,
210fbff949eSJiri Pirko };
211fbff949eSJiri Pirko 
212807e165dSTom Herbert #define FLOW_DISSECTOR_F_PARSE_1ST_FRAG		BIT(0)
2138306b688STom Herbert #define FLOW_DISSECTOR_F_STOP_AT_L3		BIT(1)
214872b1abbSTom Herbert #define FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL	BIT(2)
215823b9693STom Herbert #define FLOW_DISSECTOR_F_STOP_AT_ENCAP		BIT(3)
216807e165dSTom Herbert 
217fbff949eSJiri Pirko struct flow_dissector_key {
218fbff949eSJiri Pirko 	enum flow_dissector_key_id key_id;
219fbff949eSJiri Pirko 	size_t offset; /* offset of struct flow_dissector_key_*
220fbff949eSJiri Pirko 			  in target the struct */
221fbff949eSJiri Pirko };
222fbff949eSJiri Pirko 
223fbff949eSJiri Pirko struct flow_dissector {
224fbff949eSJiri Pirko 	unsigned int used_keys; /* each bit repesents presence of one key id */
225fbff949eSJiri Pirko 	unsigned short int offset[FLOW_DISSECTOR_KEY_MAX];
226fbff949eSJiri Pirko };
227fbff949eSJiri Pirko 
22806635a35SJiri Pirko struct flow_keys {
22942aecaa9STom Herbert 	struct flow_dissector_key_control control;
23042aecaa9STom Herbert #define FLOW_KEYS_HASH_START_FIELD basic
23106635a35SJiri Pirko 	struct flow_dissector_key_basic basic;
232d34af823STom Herbert 	struct flow_dissector_key_tags tags;
233f6a66927SHadar Hen Zion 	struct flow_dissector_key_vlan vlan;
2341fdd512cSTom Herbert 	struct flow_dissector_key_keyid keyid;
23542aecaa9STom Herbert 	struct flow_dissector_key_ports ports;
23642aecaa9STom Herbert 	struct flow_dissector_key_addrs addrs;
23706635a35SJiri Pirko };
23806635a35SJiri Pirko 
23942aecaa9STom Herbert #define FLOW_KEYS_HASH_OFFSET		\
24042aecaa9STom Herbert 	offsetof(struct flow_keys, FLOW_KEYS_HASH_START_FIELD)
24142aecaa9STom Herbert 
242c3f83241STom Herbert __be32 flow_get_u32_src(const struct flow_keys *flow);
243c3f83241STom Herbert __be32 flow_get_u32_dst(const struct flow_keys *flow);
244c3f83241STom Herbert 
24506635a35SJiri Pirko extern struct flow_dissector flow_keys_dissector;
24606635a35SJiri Pirko extern struct flow_dissector flow_keys_buf_dissector;
24706635a35SJiri Pirko 
2481bd758ebSJiri Pirko /* struct flow_keys_digest:
2491bd758ebSJiri Pirko  *
2501bd758ebSJiri Pirko  * This structure is used to hold a digest of the full flow keys. This is a
2511bd758ebSJiri Pirko  * larger "hash" of a flow to allow definitively matching specific flows where
2521bd758ebSJiri Pirko  * the 32 bit skb->hash is not large enough. The size is limited to 16 bytes so
2531bd758ebSJiri Pirko  * that it can by used in CB of skb (see sch_choke for an example).
2541bd758ebSJiri Pirko  */
2551bd758ebSJiri Pirko #define FLOW_KEYS_DIGEST_LEN	16
2561bd758ebSJiri Pirko struct flow_keys_digest {
2571bd758ebSJiri Pirko 	u8	data[FLOW_KEYS_DIGEST_LEN];
2581bd758ebSJiri Pirko };
2591bd758ebSJiri Pirko 
2601bd758ebSJiri Pirko void make_flow_keys_digest(struct flow_keys_digest *digest,
2611bd758ebSJiri Pirko 			   const struct flow_keys *flow);
2621bd758ebSJiri Pirko 
26366fdd05eSGao Feng static inline bool flow_keys_have_l4(const struct flow_keys *keys)
264bcc83839STom Herbert {
265bcc83839STom Herbert 	return (keys->ports.ports || keys->tags.flow_label);
266bcc83839STom Herbert }
267bcc83839STom Herbert 
268c6cc1ca7STom Herbert u32 flow_hash_from_keys(struct flow_keys *keys);
269c6cc1ca7STom Herbert 
2708de2d793SAmir Vadai static inline bool dissector_uses_key(const struct flow_dissector *flow_dissector,
2718de2d793SAmir Vadai 				      enum flow_dissector_key_id key_id)
2728de2d793SAmir Vadai {
2738de2d793SAmir Vadai 	return flow_dissector->used_keys & (1 << key_id);
2748de2d793SAmir Vadai }
2758de2d793SAmir Vadai 
2768de2d793SAmir Vadai static inline void *skb_flow_dissector_target(struct flow_dissector *flow_dissector,
2778de2d793SAmir Vadai 					      enum flow_dissector_key_id key_id,
2788de2d793SAmir Vadai 					      void *target_container)
2798de2d793SAmir Vadai {
2808de2d793SAmir Vadai 	return ((char *)target_container) + flow_dissector->offset[key_id];
2818de2d793SAmir Vadai }
2828de2d793SAmir Vadai 
2831bd758ebSJiri Pirko #endif
284