xref: /openbmc/linux/include/net/flow_dissector.h (revision 823b96939578eae67b9d6c0e33a39d6a7b6401e7)
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;
15807e165dSTom Herbert 	u32	is_fragment:1;
16807e165dSTom Herbert 	u32	first_frag:1;
17*823b9693STom Herbert 	u32	encapsulation:1;
1842aecaa9STom Herbert };
1942aecaa9STom Herbert 
2042aecaa9STom Herbert /**
21fbff949eSJiri Pirko  * struct flow_dissector_key_basic:
22fbff949eSJiri Pirko  * @thoff: Transport header offset
23fbff949eSJiri Pirko  * @n_proto: Network header protocol (eg. IPv4/IPv6)
24fbff949eSJiri Pirko  * @ip_proto: Transport header protocol (eg. TCP/UDP)
25fbff949eSJiri Pirko  */
26fbff949eSJiri Pirko struct flow_dissector_key_basic {
27fbff949eSJiri Pirko 	__be16	n_proto;
28fbff949eSJiri Pirko 	u8	ip_proto;
2942aecaa9STom Herbert 	u8	padding;
30fbff949eSJiri Pirko };
31fbff949eSJiri Pirko 
32d34af823STom Herbert struct flow_dissector_key_tags {
3387ee9e52STom Herbert 	u32	vlan_id:12,
3487ee9e52STom Herbert 		flow_label:20;
35d34af823STom Herbert };
36d34af823STom Herbert 
371fdd512cSTom Herbert struct flow_dissector_key_keyid {
381fdd512cSTom Herbert 	__be32	keyid;
391fdd512cSTom Herbert };
401fdd512cSTom Herbert 
41fbff949eSJiri Pirko /**
42c3f83241STom Herbert  * struct flow_dissector_key_ipv4_addrs:
43c3f83241STom Herbert  * @src: source ip address
44c3f83241STom Herbert  * @dst: destination ip address
45fbff949eSJiri Pirko  */
46c3f83241STom Herbert struct flow_dissector_key_ipv4_addrs {
47fbff949eSJiri Pirko 	/* (src,dst) must be grouped, in the same way than in IP header */
48fbff949eSJiri Pirko 	__be32 src;
49fbff949eSJiri Pirko 	__be32 dst;
50fbff949eSJiri Pirko };
51fbff949eSJiri Pirko 
52fbff949eSJiri Pirko /**
53c3f83241STom Herbert  * struct flow_dissector_key_ipv6_addrs:
54c3f83241STom Herbert  * @src: source ip address
55c3f83241STom Herbert  * @dst: destination ip address
56c3f83241STom Herbert  */
57c3f83241STom Herbert struct flow_dissector_key_ipv6_addrs {
58c3f83241STom Herbert 	/* (src,dst) must be grouped, in the same way than in IP header */
59c3f83241STom Herbert 	struct in6_addr src;
60c3f83241STom Herbert 	struct in6_addr dst;
61c3f83241STom Herbert };
62c3f83241STom Herbert 
63c3f83241STom Herbert /**
649f249089STom Herbert  * struct flow_dissector_key_tipc_addrs:
659f249089STom Herbert  * @srcnode: source node address
669f249089STom Herbert  */
679f249089STom Herbert struct flow_dissector_key_tipc_addrs {
689f249089STom Herbert 	__be32 srcnode;
699f249089STom Herbert };
709f249089STom Herbert 
719f249089STom Herbert /**
72c3f83241STom Herbert  * struct flow_dissector_key_addrs:
73c3f83241STom Herbert  * @v4addrs: IPv4 addresses
74c3f83241STom Herbert  * @v6addrs: IPv6 addresses
75c3f83241STom Herbert  */
76c3f83241STom Herbert struct flow_dissector_key_addrs {
77c3f83241STom Herbert 	union {
78c3f83241STom Herbert 		struct flow_dissector_key_ipv4_addrs v4addrs;
79c3f83241STom Herbert 		struct flow_dissector_key_ipv6_addrs v6addrs;
809f249089STom Herbert 		struct flow_dissector_key_tipc_addrs tipcaddrs;
81c3f83241STom Herbert 	};
82c3f83241STom Herbert };
83c3f83241STom Herbert 
84c3f83241STom Herbert /**
85fbff949eSJiri Pirko  * flow_dissector_key_tp_ports:
86fbff949eSJiri Pirko  *	@ports: port numbers of Transport header
8759346afeSJiri Pirko  *		src: source port number
8859346afeSJiri Pirko  *		dst: destination port number
89fbff949eSJiri Pirko  */
90fbff949eSJiri Pirko struct flow_dissector_key_ports {
91fbff949eSJiri Pirko 	union {
92fbff949eSJiri Pirko 		__be32 ports;
9359346afeSJiri Pirko 		struct {
9459346afeSJiri Pirko 			__be16 src;
9559346afeSJiri Pirko 			__be16 dst;
9659346afeSJiri Pirko 		};
97fbff949eSJiri Pirko 	};
98fbff949eSJiri Pirko };
99fbff949eSJiri Pirko 
100b924933cSJiri Pirko 
10167a900ccSJiri Pirko /**
10267a900ccSJiri Pirko  * struct flow_dissector_key_eth_addrs:
10367a900ccSJiri Pirko  * @src: source Ethernet address
10467a900ccSJiri Pirko  * @dst: destination Ethernet address
10567a900ccSJiri Pirko  */
10667a900ccSJiri Pirko struct flow_dissector_key_eth_addrs {
10767a900ccSJiri Pirko 	/* (dst,src) must be grouped, in the same way than in ETH header */
10867a900ccSJiri Pirko 	unsigned char dst[ETH_ALEN];
10967a900ccSJiri Pirko 	unsigned char src[ETH_ALEN];
11067a900ccSJiri Pirko };
11167a900ccSJiri Pirko 
112fbff949eSJiri Pirko enum flow_dissector_key_id {
11342aecaa9STom Herbert 	FLOW_DISSECTOR_KEY_CONTROL, /* struct flow_dissector_key_control */
114fbff949eSJiri Pirko 	FLOW_DISSECTOR_KEY_BASIC, /* struct flow_dissector_key_basic */
115c3f83241STom Herbert 	FLOW_DISSECTOR_KEY_IPV4_ADDRS, /* struct flow_dissector_key_ipv4_addrs */
116c3f83241STom Herbert 	FLOW_DISSECTOR_KEY_IPV6_ADDRS, /* struct flow_dissector_key_ipv6_addrs */
117fbff949eSJiri Pirko 	FLOW_DISSECTOR_KEY_PORTS, /* struct flow_dissector_key_ports */
11867a900ccSJiri Pirko 	FLOW_DISSECTOR_KEY_ETH_ADDRS, /* struct flow_dissector_key_eth_addrs */
1199f249089STom Herbert 	FLOW_DISSECTOR_KEY_TIPC_ADDRS, /* struct flow_dissector_key_tipc_addrs */
120d34af823STom Herbert 	FLOW_DISSECTOR_KEY_VLANID, /* struct flow_dissector_key_flow_tags */
12187ee9e52STom Herbert 	FLOW_DISSECTOR_KEY_FLOW_LABEL, /* struct flow_dissector_key_flow_tags */
1221fdd512cSTom Herbert 	FLOW_DISSECTOR_KEY_GRE_KEYID, /* struct flow_dissector_key_keyid */
123b3baa0fbSTom Herbert 	FLOW_DISSECTOR_KEY_MPLS_ENTROPY, /* struct flow_dissector_key_keyid */
124fbff949eSJiri Pirko 
125fbff949eSJiri Pirko 	FLOW_DISSECTOR_KEY_MAX,
126fbff949eSJiri Pirko };
127fbff949eSJiri Pirko 
128807e165dSTom Herbert #define FLOW_DISSECTOR_F_PARSE_1ST_FRAG		BIT(0)
1298306b688STom Herbert #define FLOW_DISSECTOR_F_STOP_AT_L3		BIT(1)
130872b1abbSTom Herbert #define FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL	BIT(2)
131*823b9693STom Herbert #define FLOW_DISSECTOR_F_STOP_AT_ENCAP		BIT(3)
132807e165dSTom Herbert 
133fbff949eSJiri Pirko struct flow_dissector_key {
134fbff949eSJiri Pirko 	enum flow_dissector_key_id key_id;
135fbff949eSJiri Pirko 	size_t offset; /* offset of struct flow_dissector_key_*
136fbff949eSJiri Pirko 			  in target the struct */
137fbff949eSJiri Pirko };
138fbff949eSJiri Pirko 
139fbff949eSJiri Pirko struct flow_dissector {
140fbff949eSJiri Pirko 	unsigned int used_keys; /* each bit repesents presence of one key id */
141fbff949eSJiri Pirko 	unsigned short int offset[FLOW_DISSECTOR_KEY_MAX];
142fbff949eSJiri Pirko };
143fbff949eSJiri Pirko 
14406635a35SJiri Pirko struct flow_keys {
14542aecaa9STom Herbert 	struct flow_dissector_key_control control;
14642aecaa9STom Herbert #define FLOW_KEYS_HASH_START_FIELD basic
14706635a35SJiri Pirko 	struct flow_dissector_key_basic basic;
148d34af823STom Herbert 	struct flow_dissector_key_tags tags;
1491fdd512cSTom Herbert 	struct flow_dissector_key_keyid keyid;
15042aecaa9STom Herbert 	struct flow_dissector_key_ports ports;
15142aecaa9STom Herbert 	struct flow_dissector_key_addrs addrs;
15206635a35SJiri Pirko };
15306635a35SJiri Pirko 
15442aecaa9STom Herbert #define FLOW_KEYS_HASH_OFFSET		\
15542aecaa9STom Herbert 	offsetof(struct flow_keys, FLOW_KEYS_HASH_START_FIELD)
15642aecaa9STom Herbert 
157c3f83241STom Herbert __be32 flow_get_u32_src(const struct flow_keys *flow);
158c3f83241STom Herbert __be32 flow_get_u32_dst(const struct flow_keys *flow);
159c3f83241STom Herbert 
16006635a35SJiri Pirko extern struct flow_dissector flow_keys_dissector;
16106635a35SJiri Pirko extern struct flow_dissector flow_keys_buf_dissector;
16206635a35SJiri Pirko 
1631bd758ebSJiri Pirko /* struct flow_keys_digest:
1641bd758ebSJiri Pirko  *
1651bd758ebSJiri Pirko  * This structure is used to hold a digest of the full flow keys. This is a
1661bd758ebSJiri Pirko  * larger "hash" of a flow to allow definitively matching specific flows where
1671bd758ebSJiri Pirko  * the 32 bit skb->hash is not large enough. The size is limited to 16 bytes so
1681bd758ebSJiri Pirko  * that it can by used in CB of skb (see sch_choke for an example).
1691bd758ebSJiri Pirko  */
1701bd758ebSJiri Pirko #define FLOW_KEYS_DIGEST_LEN	16
1711bd758ebSJiri Pirko struct flow_keys_digest {
1721bd758ebSJiri Pirko 	u8	data[FLOW_KEYS_DIGEST_LEN];
1731bd758ebSJiri Pirko };
1741bd758ebSJiri Pirko 
1751bd758ebSJiri Pirko void make_flow_keys_digest(struct flow_keys_digest *digest,
1761bd758ebSJiri Pirko 			   const struct flow_keys *flow);
1771bd758ebSJiri Pirko 
178bcc83839STom Herbert static inline bool flow_keys_have_l4(struct flow_keys *keys)
179bcc83839STom Herbert {
180bcc83839STom Herbert 	return (keys->ports.ports || keys->tags.flow_label);
181bcc83839STom Herbert }
182bcc83839STom Herbert 
183c6cc1ca7STom Herbert u32 flow_hash_from_keys(struct flow_keys *keys);
184c6cc1ca7STom Herbert 
1851bd758ebSJiri Pirko #endif
186