xref: /openbmc/linux/include/net/flow_dissector.h (revision c6cc1ca7f4d70cbb3ea3a5ca163c5dabaf155cdb)
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;
1542aecaa9STom Herbert };
1642aecaa9STom Herbert 
1742aecaa9STom Herbert /**
18fbff949eSJiri Pirko  * struct flow_dissector_key_basic:
19fbff949eSJiri Pirko  * @thoff: Transport header offset
20fbff949eSJiri Pirko  * @n_proto: Network header protocol (eg. IPv4/IPv6)
21fbff949eSJiri Pirko  * @ip_proto: Transport header protocol (eg. TCP/UDP)
22fbff949eSJiri Pirko  */
23fbff949eSJiri Pirko struct flow_dissector_key_basic {
24fbff949eSJiri Pirko 	__be16	n_proto;
25fbff949eSJiri Pirko 	u8	ip_proto;
2642aecaa9STom Herbert 	u8	padding;
27fbff949eSJiri Pirko };
28fbff949eSJiri Pirko 
29d34af823STom Herbert struct flow_dissector_key_tags {
3087ee9e52STom Herbert 	u32	vlan_id:12,
3187ee9e52STom Herbert 		flow_label:20;
32d34af823STom Herbert };
33d34af823STom Herbert 
341fdd512cSTom Herbert struct flow_dissector_key_keyid {
351fdd512cSTom Herbert 	__be32	keyid;
361fdd512cSTom Herbert };
371fdd512cSTom Herbert 
38fbff949eSJiri Pirko /**
39c3f83241STom Herbert  * struct flow_dissector_key_ipv4_addrs:
40c3f83241STom Herbert  * @src: source ip address
41c3f83241STom Herbert  * @dst: destination ip address
42fbff949eSJiri Pirko  */
43c3f83241STom Herbert struct flow_dissector_key_ipv4_addrs {
44fbff949eSJiri Pirko 	/* (src,dst) must be grouped, in the same way than in IP header */
45fbff949eSJiri Pirko 	__be32 src;
46fbff949eSJiri Pirko 	__be32 dst;
47fbff949eSJiri Pirko };
48fbff949eSJiri Pirko 
49fbff949eSJiri Pirko /**
50c3f83241STom Herbert  * struct flow_dissector_key_ipv6_addrs:
51c3f83241STom Herbert  * @src: source ip address
52c3f83241STom Herbert  * @dst: destination ip address
53c3f83241STom Herbert  */
54c3f83241STom Herbert struct flow_dissector_key_ipv6_addrs {
55c3f83241STom Herbert 	/* (src,dst) must be grouped, in the same way than in IP header */
56c3f83241STom Herbert 	struct in6_addr src;
57c3f83241STom Herbert 	struct in6_addr dst;
58c3f83241STom Herbert };
59c3f83241STom Herbert 
60c3f83241STom Herbert /**
619f249089STom Herbert  * struct flow_dissector_key_tipc_addrs:
629f249089STom Herbert  * @srcnode: source node address
639f249089STom Herbert  */
649f249089STom Herbert struct flow_dissector_key_tipc_addrs {
659f249089STom Herbert 	__be32 srcnode;
669f249089STom Herbert };
679f249089STom Herbert 
689f249089STom Herbert /**
69c3f83241STom Herbert  * struct flow_dissector_key_addrs:
70c3f83241STom Herbert  * @v4addrs: IPv4 addresses
71c3f83241STom Herbert  * @v6addrs: IPv6 addresses
72c3f83241STom Herbert  */
73c3f83241STom Herbert struct flow_dissector_key_addrs {
74c3f83241STom Herbert 	union {
75c3f83241STom Herbert 		struct flow_dissector_key_ipv4_addrs v4addrs;
76c3f83241STom Herbert 		struct flow_dissector_key_ipv6_addrs v6addrs;
779f249089STom Herbert 		struct flow_dissector_key_tipc_addrs tipcaddrs;
78c3f83241STom Herbert 	};
79c3f83241STom Herbert };
80c3f83241STom Herbert 
81c3f83241STom Herbert /**
82fbff949eSJiri Pirko  * flow_dissector_key_tp_ports:
83fbff949eSJiri Pirko  *	@ports: port numbers of Transport header
8459346afeSJiri Pirko  *		src: source port number
8559346afeSJiri Pirko  *		dst: destination port number
86fbff949eSJiri Pirko  */
87fbff949eSJiri Pirko struct flow_dissector_key_ports {
88fbff949eSJiri Pirko 	union {
89fbff949eSJiri Pirko 		__be32 ports;
9059346afeSJiri Pirko 		struct {
9159346afeSJiri Pirko 			__be16 src;
9259346afeSJiri Pirko 			__be16 dst;
9359346afeSJiri Pirko 		};
94fbff949eSJiri Pirko 	};
95fbff949eSJiri Pirko };
96fbff949eSJiri Pirko 
97b924933cSJiri Pirko 
9867a900ccSJiri Pirko /**
9967a900ccSJiri Pirko  * struct flow_dissector_key_eth_addrs:
10067a900ccSJiri Pirko  * @src: source Ethernet address
10167a900ccSJiri Pirko  * @dst: destination Ethernet address
10267a900ccSJiri Pirko  */
10367a900ccSJiri Pirko struct flow_dissector_key_eth_addrs {
10467a900ccSJiri Pirko 	/* (dst,src) must be grouped, in the same way than in ETH header */
10567a900ccSJiri Pirko 	unsigned char dst[ETH_ALEN];
10667a900ccSJiri Pirko 	unsigned char src[ETH_ALEN];
10767a900ccSJiri Pirko };
10867a900ccSJiri Pirko 
109fbff949eSJiri Pirko enum flow_dissector_key_id {
11042aecaa9STom Herbert 	FLOW_DISSECTOR_KEY_CONTROL, /* struct flow_dissector_key_control */
111fbff949eSJiri Pirko 	FLOW_DISSECTOR_KEY_BASIC, /* struct flow_dissector_key_basic */
112c3f83241STom Herbert 	FLOW_DISSECTOR_KEY_IPV4_ADDRS, /* struct flow_dissector_key_ipv4_addrs */
113c3f83241STom Herbert 	FLOW_DISSECTOR_KEY_IPV6_ADDRS, /* struct flow_dissector_key_ipv6_addrs */
114fbff949eSJiri Pirko 	FLOW_DISSECTOR_KEY_PORTS, /* struct flow_dissector_key_ports */
11567a900ccSJiri Pirko 	FLOW_DISSECTOR_KEY_ETH_ADDRS, /* struct flow_dissector_key_eth_addrs */
1169f249089STom Herbert 	FLOW_DISSECTOR_KEY_TIPC_ADDRS, /* struct flow_dissector_key_tipc_addrs */
117d34af823STom Herbert 	FLOW_DISSECTOR_KEY_VLANID, /* struct flow_dissector_key_flow_tags */
11887ee9e52STom Herbert 	FLOW_DISSECTOR_KEY_FLOW_LABEL, /* struct flow_dissector_key_flow_tags */
1191fdd512cSTom Herbert 	FLOW_DISSECTOR_KEY_GRE_KEYID, /* struct flow_dissector_key_keyid */
120b3baa0fbSTom Herbert 	FLOW_DISSECTOR_KEY_MPLS_ENTROPY, /* struct flow_dissector_key_keyid */
121fbff949eSJiri Pirko 
122fbff949eSJiri Pirko 	FLOW_DISSECTOR_KEY_MAX,
123fbff949eSJiri Pirko };
124fbff949eSJiri Pirko 
125fbff949eSJiri Pirko struct flow_dissector_key {
126fbff949eSJiri Pirko 	enum flow_dissector_key_id key_id;
127fbff949eSJiri Pirko 	size_t offset; /* offset of struct flow_dissector_key_*
128fbff949eSJiri Pirko 			  in target the struct */
129fbff949eSJiri Pirko };
130fbff949eSJiri Pirko 
131fbff949eSJiri Pirko struct flow_dissector {
132fbff949eSJiri Pirko 	unsigned int used_keys; /* each bit repesents presence of one key id */
133fbff949eSJiri Pirko 	unsigned short int offset[FLOW_DISSECTOR_KEY_MAX];
134fbff949eSJiri Pirko };
135fbff949eSJiri Pirko 
13606635a35SJiri Pirko struct flow_keys {
13742aecaa9STom Herbert 	struct flow_dissector_key_control control;
13842aecaa9STom Herbert #define FLOW_KEYS_HASH_START_FIELD basic
13906635a35SJiri Pirko 	struct flow_dissector_key_basic basic;
140d34af823STom Herbert 	struct flow_dissector_key_tags tags;
1411fdd512cSTom Herbert 	struct flow_dissector_key_keyid keyid;
14242aecaa9STom Herbert 	struct flow_dissector_key_ports ports;
14342aecaa9STom Herbert 	struct flow_dissector_key_addrs addrs;
14406635a35SJiri Pirko };
14506635a35SJiri Pirko 
14642aecaa9STom Herbert #define FLOW_KEYS_HASH_OFFSET		\
14742aecaa9STom Herbert 	offsetof(struct flow_keys, FLOW_KEYS_HASH_START_FIELD)
14842aecaa9STom Herbert 
149c3f83241STom Herbert __be32 flow_get_u32_src(const struct flow_keys *flow);
150c3f83241STom Herbert __be32 flow_get_u32_dst(const struct flow_keys *flow);
151c3f83241STom Herbert 
15206635a35SJiri Pirko extern struct flow_dissector flow_keys_dissector;
15306635a35SJiri Pirko extern struct flow_dissector flow_keys_buf_dissector;
15406635a35SJiri Pirko 
1551bd758ebSJiri Pirko /* struct flow_keys_digest:
1561bd758ebSJiri Pirko  *
1571bd758ebSJiri Pirko  * This structure is used to hold a digest of the full flow keys. This is a
1581bd758ebSJiri Pirko  * larger "hash" of a flow to allow definitively matching specific flows where
1591bd758ebSJiri Pirko  * the 32 bit skb->hash is not large enough. The size is limited to 16 bytes so
1601bd758ebSJiri Pirko  * that it can by used in CB of skb (see sch_choke for an example).
1611bd758ebSJiri Pirko  */
1621bd758ebSJiri Pirko #define FLOW_KEYS_DIGEST_LEN	16
1631bd758ebSJiri Pirko struct flow_keys_digest {
1641bd758ebSJiri Pirko 	u8	data[FLOW_KEYS_DIGEST_LEN];
1651bd758ebSJiri Pirko };
1661bd758ebSJiri Pirko 
1671bd758ebSJiri Pirko void make_flow_keys_digest(struct flow_keys_digest *digest,
1681bd758ebSJiri Pirko 			   const struct flow_keys *flow);
1691bd758ebSJiri Pirko 
170bcc83839STom Herbert static inline bool flow_keys_have_l4(struct flow_keys *keys)
171bcc83839STom Herbert {
172bcc83839STom Herbert 	return (keys->ports.ports || keys->tags.flow_label);
173bcc83839STom Herbert }
174bcc83839STom Herbert 
175*c6cc1ca7STom Herbert u32 flow_hash_from_keys(struct flow_keys *keys);
176*c6cc1ca7STom Herbert 
1771bd758ebSJiri Pirko #endif
178