xref: /openbmc/linux/include/net/flow_dissector.h (revision 4b36993d3df0834eff3b4172962de0343a4d9123)
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;
15*4b36993dSDavid S. Miller 	u32	flags;
1642aecaa9STom Herbert };
1742aecaa9STom Herbert 
18*4b36993dSDavid S. Miller #define FLOW_DIS_IS_FRAGMENT	BIT(0)
19*4b36993dSDavid S. Miller #define FLOW_DIS_FIRST_FRAG	BIT(1)
20*4b36993dSDavid S. Miller #define FLOW_DIS_ENCAPSULATION	BIT(2)
21*4b36993dSDavid 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 {
3587ee9e52STom Herbert 	u32	vlan_id:12,
3687ee9e52STom Herbert 		flow_label:20;
37d34af823STom Herbert };
38d34af823STom Herbert 
391fdd512cSTom Herbert struct flow_dissector_key_keyid {
401fdd512cSTom Herbert 	__be32	keyid;
411fdd512cSTom Herbert };
421fdd512cSTom Herbert 
43fbff949eSJiri Pirko /**
44c3f83241STom Herbert  * struct flow_dissector_key_ipv4_addrs:
45c3f83241STom Herbert  * @src: source ip address
46c3f83241STom Herbert  * @dst: destination ip address
47fbff949eSJiri Pirko  */
48c3f83241STom Herbert struct flow_dissector_key_ipv4_addrs {
49fbff949eSJiri Pirko 	/* (src,dst) must be grouped, in the same way than in IP header */
50fbff949eSJiri Pirko 	__be32 src;
51fbff949eSJiri Pirko 	__be32 dst;
52fbff949eSJiri Pirko };
53fbff949eSJiri Pirko 
54fbff949eSJiri Pirko /**
55c3f83241STom Herbert  * struct flow_dissector_key_ipv6_addrs:
56c3f83241STom Herbert  * @src: source ip address
57c3f83241STom Herbert  * @dst: destination ip address
58c3f83241STom Herbert  */
59c3f83241STom Herbert struct flow_dissector_key_ipv6_addrs {
60c3f83241STom Herbert 	/* (src,dst) must be grouped, in the same way than in IP header */
61c3f83241STom Herbert 	struct in6_addr src;
62c3f83241STom Herbert 	struct in6_addr dst;
63c3f83241STom Herbert };
64c3f83241STom Herbert 
65c3f83241STom Herbert /**
669f249089STom Herbert  * struct flow_dissector_key_tipc_addrs:
679f249089STom Herbert  * @srcnode: source node address
689f249089STom Herbert  */
699f249089STom Herbert struct flow_dissector_key_tipc_addrs {
709f249089STom Herbert 	__be32 srcnode;
719f249089STom Herbert };
729f249089STom Herbert 
739f249089STom Herbert /**
74c3f83241STom Herbert  * struct flow_dissector_key_addrs:
75c3f83241STom Herbert  * @v4addrs: IPv4 addresses
76c3f83241STom Herbert  * @v6addrs: IPv6 addresses
77c3f83241STom Herbert  */
78c3f83241STom Herbert struct flow_dissector_key_addrs {
79c3f83241STom Herbert 	union {
80c3f83241STom Herbert 		struct flow_dissector_key_ipv4_addrs v4addrs;
81c3f83241STom Herbert 		struct flow_dissector_key_ipv6_addrs v6addrs;
829f249089STom Herbert 		struct flow_dissector_key_tipc_addrs tipcaddrs;
83c3f83241STom Herbert 	};
84c3f83241STom Herbert };
85c3f83241STom Herbert 
86c3f83241STom Herbert /**
87fbff949eSJiri Pirko  * flow_dissector_key_tp_ports:
88fbff949eSJiri Pirko  *	@ports: port numbers of Transport header
8959346afeSJiri Pirko  *		src: source port number
9059346afeSJiri Pirko  *		dst: destination port number
91fbff949eSJiri Pirko  */
92fbff949eSJiri Pirko struct flow_dissector_key_ports {
93fbff949eSJiri Pirko 	union {
94fbff949eSJiri Pirko 		__be32 ports;
9559346afeSJiri Pirko 		struct {
9659346afeSJiri Pirko 			__be16 src;
9759346afeSJiri Pirko 			__be16 dst;
9859346afeSJiri Pirko 		};
99fbff949eSJiri Pirko 	};
100fbff949eSJiri Pirko };
101fbff949eSJiri Pirko 
102b924933cSJiri Pirko 
10367a900ccSJiri Pirko /**
10467a900ccSJiri Pirko  * struct flow_dissector_key_eth_addrs:
10567a900ccSJiri Pirko  * @src: source Ethernet address
10667a900ccSJiri Pirko  * @dst: destination Ethernet address
10767a900ccSJiri Pirko  */
10867a900ccSJiri Pirko struct flow_dissector_key_eth_addrs {
10967a900ccSJiri Pirko 	/* (dst,src) must be grouped, in the same way than in ETH header */
11067a900ccSJiri Pirko 	unsigned char dst[ETH_ALEN];
11167a900ccSJiri Pirko 	unsigned char src[ETH_ALEN];
11267a900ccSJiri Pirko };
11367a900ccSJiri Pirko 
114fbff949eSJiri Pirko enum flow_dissector_key_id {
11542aecaa9STom Herbert 	FLOW_DISSECTOR_KEY_CONTROL, /* struct flow_dissector_key_control */
116fbff949eSJiri Pirko 	FLOW_DISSECTOR_KEY_BASIC, /* struct flow_dissector_key_basic */
117c3f83241STom Herbert 	FLOW_DISSECTOR_KEY_IPV4_ADDRS, /* struct flow_dissector_key_ipv4_addrs */
118c3f83241STom Herbert 	FLOW_DISSECTOR_KEY_IPV6_ADDRS, /* struct flow_dissector_key_ipv6_addrs */
119fbff949eSJiri Pirko 	FLOW_DISSECTOR_KEY_PORTS, /* struct flow_dissector_key_ports */
12067a900ccSJiri Pirko 	FLOW_DISSECTOR_KEY_ETH_ADDRS, /* struct flow_dissector_key_eth_addrs */
1219f249089STom Herbert 	FLOW_DISSECTOR_KEY_TIPC_ADDRS, /* struct flow_dissector_key_tipc_addrs */
122d34af823STom Herbert 	FLOW_DISSECTOR_KEY_VLANID, /* struct flow_dissector_key_flow_tags */
12387ee9e52STom Herbert 	FLOW_DISSECTOR_KEY_FLOW_LABEL, /* struct flow_dissector_key_flow_tags */
1241fdd512cSTom Herbert 	FLOW_DISSECTOR_KEY_GRE_KEYID, /* struct flow_dissector_key_keyid */
125b3baa0fbSTom Herbert 	FLOW_DISSECTOR_KEY_MPLS_ENTROPY, /* struct flow_dissector_key_keyid */
126fbff949eSJiri Pirko 
127fbff949eSJiri Pirko 	FLOW_DISSECTOR_KEY_MAX,
128fbff949eSJiri Pirko };
129fbff949eSJiri Pirko 
130807e165dSTom Herbert #define FLOW_DISSECTOR_F_PARSE_1ST_FRAG		BIT(0)
1318306b688STom Herbert #define FLOW_DISSECTOR_F_STOP_AT_L3		BIT(1)
132872b1abbSTom Herbert #define FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL	BIT(2)
133823b9693STom Herbert #define FLOW_DISSECTOR_F_STOP_AT_ENCAP		BIT(3)
134807e165dSTom Herbert 
135fbff949eSJiri Pirko struct flow_dissector_key {
136fbff949eSJiri Pirko 	enum flow_dissector_key_id key_id;
137fbff949eSJiri Pirko 	size_t offset; /* offset of struct flow_dissector_key_*
138fbff949eSJiri Pirko 			  in target the struct */
139fbff949eSJiri Pirko };
140fbff949eSJiri Pirko 
141fbff949eSJiri Pirko struct flow_dissector {
142fbff949eSJiri Pirko 	unsigned int used_keys; /* each bit repesents presence of one key id */
143fbff949eSJiri Pirko 	unsigned short int offset[FLOW_DISSECTOR_KEY_MAX];
144fbff949eSJiri Pirko };
145fbff949eSJiri Pirko 
14606635a35SJiri Pirko struct flow_keys {
14742aecaa9STom Herbert 	struct flow_dissector_key_control control;
14842aecaa9STom Herbert #define FLOW_KEYS_HASH_START_FIELD basic
14906635a35SJiri Pirko 	struct flow_dissector_key_basic basic;
150d34af823STom Herbert 	struct flow_dissector_key_tags tags;
1511fdd512cSTom Herbert 	struct flow_dissector_key_keyid keyid;
15242aecaa9STom Herbert 	struct flow_dissector_key_ports ports;
15342aecaa9STom Herbert 	struct flow_dissector_key_addrs addrs;
15406635a35SJiri Pirko };
15506635a35SJiri Pirko 
15642aecaa9STom Herbert #define FLOW_KEYS_HASH_OFFSET		\
15742aecaa9STom Herbert 	offsetof(struct flow_keys, FLOW_KEYS_HASH_START_FIELD)
15842aecaa9STom Herbert 
159c3f83241STom Herbert __be32 flow_get_u32_src(const struct flow_keys *flow);
160c3f83241STom Herbert __be32 flow_get_u32_dst(const struct flow_keys *flow);
161c3f83241STom Herbert 
16206635a35SJiri Pirko extern struct flow_dissector flow_keys_dissector;
16306635a35SJiri Pirko extern struct flow_dissector flow_keys_buf_dissector;
16406635a35SJiri Pirko 
1651bd758ebSJiri Pirko /* struct flow_keys_digest:
1661bd758ebSJiri Pirko  *
1671bd758ebSJiri Pirko  * This structure is used to hold a digest of the full flow keys. This is a
1681bd758ebSJiri Pirko  * larger "hash" of a flow to allow definitively matching specific flows where
1691bd758ebSJiri Pirko  * the 32 bit skb->hash is not large enough. The size is limited to 16 bytes so
1701bd758ebSJiri Pirko  * that it can by used in CB of skb (see sch_choke for an example).
1711bd758ebSJiri Pirko  */
1721bd758ebSJiri Pirko #define FLOW_KEYS_DIGEST_LEN	16
1731bd758ebSJiri Pirko struct flow_keys_digest {
1741bd758ebSJiri Pirko 	u8	data[FLOW_KEYS_DIGEST_LEN];
1751bd758ebSJiri Pirko };
1761bd758ebSJiri Pirko 
1771bd758ebSJiri Pirko void make_flow_keys_digest(struct flow_keys_digest *digest,
1781bd758ebSJiri Pirko 			   const struct flow_keys *flow);
1791bd758ebSJiri Pirko 
180bcc83839STom Herbert static inline bool flow_keys_have_l4(struct flow_keys *keys)
181bcc83839STom Herbert {
182bcc83839STom Herbert 	return (keys->ports.ports || keys->tags.flow_label);
183bcc83839STom Herbert }
184bcc83839STom Herbert 
185c6cc1ca7STom Herbert u32 flow_hash_from_keys(struct flow_keys *keys);
186c6cc1ca7STom Herbert 
1871bd758ebSJiri Pirko #endif
188