xref: /openbmc/linux/include/net/flow_dissector.h (revision 518d8a2e9bad83c6040eccebc3d1f7388fc034e7)
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 
2242aecaa9STom Herbert /**
23fbff949eSJiri Pirko  * struct flow_dissector_key_basic:
24fbff949eSJiri Pirko  * @thoff: Transport header offset
25fbff949eSJiri Pirko  * @n_proto: Network header protocol (eg. IPv4/IPv6)
26fbff949eSJiri Pirko  * @ip_proto: Transport header protocol (eg. TCP/UDP)
27fbff949eSJiri Pirko  */
28fbff949eSJiri Pirko struct flow_dissector_key_basic {
29fbff949eSJiri Pirko 	__be16	n_proto;
30fbff949eSJiri Pirko 	u8	ip_proto;
3142aecaa9STom Herbert 	u8	padding;
32fbff949eSJiri Pirko };
33fbff949eSJiri Pirko 
34d34af823STom Herbert struct flow_dissector_key_tags {
35f6a66927SHadar Hen Zion 	u32	flow_label;
36f6a66927SHadar Hen Zion };
37f6a66927SHadar Hen Zion 
38f6a66927SHadar Hen Zion struct flow_dissector_key_vlan {
39f6a66927SHadar Hen Zion 	u16	vlan_id:12,
40f6a66927SHadar Hen Zion 		vlan_priority:3;
41f6a66927SHadar Hen Zion 	u16	padding;
42d34af823STom Herbert };
43d34af823STom Herbert 
44029c1ecbSBenjamin LaHaise struct flow_dissector_key_mpls {
45029c1ecbSBenjamin LaHaise 	u32	mpls_ttl:8,
46029c1ecbSBenjamin LaHaise 		mpls_bos:1,
47029c1ecbSBenjamin LaHaise 		mpls_tc:3,
48029c1ecbSBenjamin LaHaise 		mpls_label:20;
49029c1ecbSBenjamin LaHaise };
50029c1ecbSBenjamin LaHaise 
511fdd512cSTom Herbert struct flow_dissector_key_keyid {
521fdd512cSTom Herbert 	__be32	keyid;
531fdd512cSTom Herbert };
541fdd512cSTom Herbert 
55fbff949eSJiri Pirko /**
56c3f83241STom Herbert  * struct flow_dissector_key_ipv4_addrs:
57c3f83241STom Herbert  * @src: source ip address
58c3f83241STom Herbert  * @dst: destination ip address
59fbff949eSJiri Pirko  */
60c3f83241STom Herbert struct flow_dissector_key_ipv4_addrs {
61fbff949eSJiri Pirko 	/* (src,dst) must be grouped, in the same way than in IP header */
62fbff949eSJiri Pirko 	__be32 src;
63fbff949eSJiri Pirko 	__be32 dst;
64fbff949eSJiri Pirko };
65fbff949eSJiri Pirko 
66fbff949eSJiri Pirko /**
67c3f83241STom Herbert  * struct flow_dissector_key_ipv6_addrs:
68c3f83241STom Herbert  * @src: source ip address
69c3f83241STom Herbert  * @dst: destination ip address
70c3f83241STom Herbert  */
71c3f83241STom Herbert struct flow_dissector_key_ipv6_addrs {
72c3f83241STom Herbert 	/* (src,dst) must be grouped, in the same way than in IP header */
73c3f83241STom Herbert 	struct in6_addr src;
74c3f83241STom Herbert 	struct in6_addr dst;
75c3f83241STom Herbert };
76c3f83241STom Herbert 
77c3f83241STom Herbert /**
789f249089STom Herbert  * struct flow_dissector_key_tipc_addrs:
799f249089STom Herbert  * @srcnode: source node address
809f249089STom Herbert  */
819f249089STom Herbert struct flow_dissector_key_tipc_addrs {
829f249089STom Herbert 	__be32 srcnode;
839f249089STom Herbert };
849f249089STom Herbert 
859f249089STom Herbert /**
86c3f83241STom Herbert  * struct flow_dissector_key_addrs:
87c3f83241STom Herbert  * @v4addrs: IPv4 addresses
88c3f83241STom Herbert  * @v6addrs: IPv6 addresses
89c3f83241STom Herbert  */
90c3f83241STom Herbert struct flow_dissector_key_addrs {
91c3f83241STom Herbert 	union {
92c3f83241STom Herbert 		struct flow_dissector_key_ipv4_addrs v4addrs;
93c3f83241STom Herbert 		struct flow_dissector_key_ipv6_addrs v6addrs;
949f249089STom Herbert 		struct flow_dissector_key_tipc_addrs tipcaddrs;
95c3f83241STom Herbert 	};
96c3f83241STom Herbert };
97c3f83241STom Herbert 
98c3f83241STom Herbert /**
9955733350SSimon Horman  * flow_dissector_key_arp:
10055733350SSimon Horman  *	@ports: Operation, source and target addresses for an ARP header
10155733350SSimon Horman  *              for Ethernet hardware addresses and IPv4 protocol addresses
10255733350SSimon Horman  *		sip: Sender IP address
10355733350SSimon Horman  *		tip: Target IP address
10455733350SSimon Horman  *		op:  Operation
10555733350SSimon Horman  *		sha: Sender hardware address
10655733350SSimon Horman  *		tpa: Target hardware address
10755733350SSimon Horman  */
10855733350SSimon Horman struct flow_dissector_key_arp {
10955733350SSimon Horman 	__u32 sip;
11055733350SSimon Horman 	__u32 tip;
11155733350SSimon Horman 	__u8 op;
11255733350SSimon Horman 	unsigned char sha[ETH_ALEN];
11355733350SSimon Horman 	unsigned char tha[ETH_ALEN];
11455733350SSimon Horman };
11555733350SSimon Horman 
11655733350SSimon Horman /**
117fbff949eSJiri Pirko  * flow_dissector_key_tp_ports:
118fbff949eSJiri Pirko  *	@ports: port numbers of Transport header
11959346afeSJiri Pirko  *		src: source port number
12059346afeSJiri Pirko  *		dst: destination port number
121fbff949eSJiri Pirko  */
122fbff949eSJiri Pirko struct flow_dissector_key_ports {
123fbff949eSJiri Pirko 	union {
124fbff949eSJiri Pirko 		__be32 ports;
12559346afeSJiri Pirko 		struct {
12659346afeSJiri Pirko 			__be16 src;
12759346afeSJiri Pirko 			__be16 dst;
12859346afeSJiri Pirko 		};
129fbff949eSJiri Pirko 	};
130fbff949eSJiri Pirko };
131fbff949eSJiri Pirko 
132972d3876SSimon Horman /**
133972d3876SSimon Horman  * flow_dissector_key_icmp:
134972d3876SSimon Horman  *	@ports: type and code of ICMP header
135972d3876SSimon Horman  *		icmp: ICMP type (high) and code (low)
136972d3876SSimon Horman  *		type: ICMP type
137972d3876SSimon Horman  *		code: ICMP code
138972d3876SSimon Horman  */
139972d3876SSimon Horman struct flow_dissector_key_icmp {
140972d3876SSimon Horman 	union {
141972d3876SSimon Horman 		__be16 icmp;
142972d3876SSimon Horman 		struct {
143972d3876SSimon Horman 			u8 type;
144972d3876SSimon Horman 			u8 code;
145972d3876SSimon Horman 		};
146972d3876SSimon Horman 	};
147972d3876SSimon Horman };
148b924933cSJiri Pirko 
14967a900ccSJiri Pirko /**
15067a900ccSJiri Pirko  * struct flow_dissector_key_eth_addrs:
15167a900ccSJiri Pirko  * @src: source Ethernet address
15267a900ccSJiri Pirko  * @dst: destination Ethernet address
15367a900ccSJiri Pirko  */
15467a900ccSJiri Pirko struct flow_dissector_key_eth_addrs {
15567a900ccSJiri Pirko 	/* (dst,src) must be grouped, in the same way than in ETH header */
15667a900ccSJiri Pirko 	unsigned char dst[ETH_ALEN];
15767a900ccSJiri Pirko 	unsigned char src[ETH_ALEN];
15867a900ccSJiri Pirko };
15967a900ccSJiri Pirko 
160ac4bb5deSJiri Pirko /**
161ac4bb5deSJiri Pirko  * struct flow_dissector_key_tcp:
162ac4bb5deSJiri Pirko  * @flags: flags
163ac4bb5deSJiri Pirko  */
164ac4bb5deSJiri Pirko struct flow_dissector_key_tcp {
165ac4bb5deSJiri Pirko 	__be16 flags;
166ac4bb5deSJiri Pirko };
167ac4bb5deSJiri Pirko 
168*518d8a2eSOr Gerlitz /**
169*518d8a2eSOr Gerlitz  * struct flow_dissector_key_ip:
170*518d8a2eSOr Gerlitz  * @tos: tos
171*518d8a2eSOr Gerlitz  * @ttl: ttl
172*518d8a2eSOr Gerlitz  */
173*518d8a2eSOr Gerlitz struct flow_dissector_key_ip {
174*518d8a2eSOr Gerlitz 	__u8	tos;
175*518d8a2eSOr Gerlitz 	__u8	ttl;
176*518d8a2eSOr Gerlitz };
177*518d8a2eSOr Gerlitz 
178fbff949eSJiri Pirko enum flow_dissector_key_id {
17942aecaa9STom Herbert 	FLOW_DISSECTOR_KEY_CONTROL, /* struct flow_dissector_key_control */
180fbff949eSJiri Pirko 	FLOW_DISSECTOR_KEY_BASIC, /* struct flow_dissector_key_basic */
181c3f83241STom Herbert 	FLOW_DISSECTOR_KEY_IPV4_ADDRS, /* struct flow_dissector_key_ipv4_addrs */
182c3f83241STom Herbert 	FLOW_DISSECTOR_KEY_IPV6_ADDRS, /* struct flow_dissector_key_ipv6_addrs */
183fbff949eSJiri Pirko 	FLOW_DISSECTOR_KEY_PORTS, /* struct flow_dissector_key_ports */
184972d3876SSimon Horman 	FLOW_DISSECTOR_KEY_ICMP, /* struct flow_dissector_key_icmp */
18567a900ccSJiri Pirko 	FLOW_DISSECTOR_KEY_ETH_ADDRS, /* struct flow_dissector_key_eth_addrs */
1869f249089STom Herbert 	FLOW_DISSECTOR_KEY_TIPC_ADDRS, /* struct flow_dissector_key_tipc_addrs */
18755733350SSimon Horman 	FLOW_DISSECTOR_KEY_ARP, /* struct flow_dissector_key_arp */
188f6a66927SHadar Hen Zion 	FLOW_DISSECTOR_KEY_VLAN, /* struct flow_dissector_key_flow_vlan */
18987ee9e52STom Herbert 	FLOW_DISSECTOR_KEY_FLOW_LABEL, /* struct flow_dissector_key_flow_tags */
1901fdd512cSTom Herbert 	FLOW_DISSECTOR_KEY_GRE_KEYID, /* struct flow_dissector_key_keyid */
191b3baa0fbSTom Herbert 	FLOW_DISSECTOR_KEY_MPLS_ENTROPY, /* struct flow_dissector_key_keyid */
1929ba6a9a9SHadar Hen Zion 	FLOW_DISSECTOR_KEY_ENC_KEYID, /* struct flow_dissector_key_keyid */
1939ba6a9a9SHadar Hen Zion 	FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS, /* struct flow_dissector_key_ipv4_addrs */
1949ba6a9a9SHadar Hen Zion 	FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS, /* struct flow_dissector_key_ipv6_addrs */
1959ba6a9a9SHadar Hen Zion 	FLOW_DISSECTOR_KEY_ENC_CONTROL, /* struct flow_dissector_key_control */
196f4d997fdSHadar Hen Zion 	FLOW_DISSECTOR_KEY_ENC_PORTS, /* struct flow_dissector_key_ports */
197029c1ecbSBenjamin LaHaise 	FLOW_DISSECTOR_KEY_MPLS, /* struct flow_dissector_key_mpls */
198ac4bb5deSJiri Pirko 	FLOW_DISSECTOR_KEY_TCP, /* struct flow_dissector_key_tcp */
199*518d8a2eSOr Gerlitz 	FLOW_DISSECTOR_KEY_IP, /* struct flow_dissector_key_ip */
200fbff949eSJiri Pirko 
201fbff949eSJiri Pirko 	FLOW_DISSECTOR_KEY_MAX,
202fbff949eSJiri Pirko };
203fbff949eSJiri Pirko 
204807e165dSTom Herbert #define FLOW_DISSECTOR_F_PARSE_1ST_FRAG		BIT(0)
2058306b688STom Herbert #define FLOW_DISSECTOR_F_STOP_AT_L3		BIT(1)
206872b1abbSTom Herbert #define FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL	BIT(2)
207823b9693STom Herbert #define FLOW_DISSECTOR_F_STOP_AT_ENCAP		BIT(3)
208807e165dSTom Herbert 
209fbff949eSJiri Pirko struct flow_dissector_key {
210fbff949eSJiri Pirko 	enum flow_dissector_key_id key_id;
211fbff949eSJiri Pirko 	size_t offset; /* offset of struct flow_dissector_key_*
212fbff949eSJiri Pirko 			  in target the struct */
213fbff949eSJiri Pirko };
214fbff949eSJiri Pirko 
215fbff949eSJiri Pirko struct flow_dissector {
216fbff949eSJiri Pirko 	unsigned int used_keys; /* each bit repesents presence of one key id */
217fbff949eSJiri Pirko 	unsigned short int offset[FLOW_DISSECTOR_KEY_MAX];
218fbff949eSJiri Pirko };
219fbff949eSJiri Pirko 
22006635a35SJiri Pirko struct flow_keys {
22142aecaa9STom Herbert 	struct flow_dissector_key_control control;
22242aecaa9STom Herbert #define FLOW_KEYS_HASH_START_FIELD basic
22306635a35SJiri Pirko 	struct flow_dissector_key_basic basic;
224d34af823STom Herbert 	struct flow_dissector_key_tags tags;
225f6a66927SHadar Hen Zion 	struct flow_dissector_key_vlan vlan;
2261fdd512cSTom Herbert 	struct flow_dissector_key_keyid keyid;
22742aecaa9STom Herbert 	struct flow_dissector_key_ports ports;
22842aecaa9STom Herbert 	struct flow_dissector_key_addrs addrs;
22906635a35SJiri Pirko };
23006635a35SJiri Pirko 
23142aecaa9STom Herbert #define FLOW_KEYS_HASH_OFFSET		\
23242aecaa9STom Herbert 	offsetof(struct flow_keys, FLOW_KEYS_HASH_START_FIELD)
23342aecaa9STom Herbert 
234c3f83241STom Herbert __be32 flow_get_u32_src(const struct flow_keys *flow);
235c3f83241STom Herbert __be32 flow_get_u32_dst(const struct flow_keys *flow);
236c3f83241STom Herbert 
23706635a35SJiri Pirko extern struct flow_dissector flow_keys_dissector;
23806635a35SJiri Pirko extern struct flow_dissector flow_keys_buf_dissector;
23906635a35SJiri Pirko 
2401bd758ebSJiri Pirko /* struct flow_keys_digest:
2411bd758ebSJiri Pirko  *
2421bd758ebSJiri Pirko  * This structure is used to hold a digest of the full flow keys. This is a
2431bd758ebSJiri Pirko  * larger "hash" of a flow to allow definitively matching specific flows where
2441bd758ebSJiri Pirko  * the 32 bit skb->hash is not large enough. The size is limited to 16 bytes so
2451bd758ebSJiri Pirko  * that it can by used in CB of skb (see sch_choke for an example).
2461bd758ebSJiri Pirko  */
2471bd758ebSJiri Pirko #define FLOW_KEYS_DIGEST_LEN	16
2481bd758ebSJiri Pirko struct flow_keys_digest {
2491bd758ebSJiri Pirko 	u8	data[FLOW_KEYS_DIGEST_LEN];
2501bd758ebSJiri Pirko };
2511bd758ebSJiri Pirko 
2521bd758ebSJiri Pirko void make_flow_keys_digest(struct flow_keys_digest *digest,
2531bd758ebSJiri Pirko 			   const struct flow_keys *flow);
2541bd758ebSJiri Pirko 
25566fdd05eSGao Feng static inline bool flow_keys_have_l4(const struct flow_keys *keys)
256bcc83839STom Herbert {
257bcc83839STom Herbert 	return (keys->ports.ports || keys->tags.flow_label);
258bcc83839STom Herbert }
259bcc83839STom Herbert 
260c6cc1ca7STom Herbert u32 flow_hash_from_keys(struct flow_keys *keys);
261c6cc1ca7STom Herbert 
2628de2d793SAmir Vadai static inline bool dissector_uses_key(const struct flow_dissector *flow_dissector,
2638de2d793SAmir Vadai 				      enum flow_dissector_key_id key_id)
2648de2d793SAmir Vadai {
2658de2d793SAmir Vadai 	return flow_dissector->used_keys & (1 << key_id);
2668de2d793SAmir Vadai }
2678de2d793SAmir Vadai 
2688de2d793SAmir Vadai static inline void *skb_flow_dissector_target(struct flow_dissector *flow_dissector,
2698de2d793SAmir Vadai 					      enum flow_dissector_key_id key_id,
2708de2d793SAmir Vadai 					      void *target_container)
2718de2d793SAmir Vadai {
2728de2d793SAmir Vadai 	return ((char *)target_container) + flow_dissector->offset[key_id];
2738de2d793SAmir Vadai }
2748de2d793SAmir Vadai 
2751bd758ebSJiri Pirko #endif
276