18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (c) 2007-2017 Nicira, Inc.
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#ifndef FLOW_H
78c2ecf20Sopenharmony_ci#define FLOW_H 1
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include <linux/cache.h>
108c2ecf20Sopenharmony_ci#include <linux/kernel.h>
118c2ecf20Sopenharmony_ci#include <linux/netlink.h>
128c2ecf20Sopenharmony_ci#include <linux/openvswitch.h>
138c2ecf20Sopenharmony_ci#include <linux/spinlock.h>
148c2ecf20Sopenharmony_ci#include <linux/types.h>
158c2ecf20Sopenharmony_ci#include <linux/rcupdate.h>
168c2ecf20Sopenharmony_ci#include <linux/if_ether.h>
178c2ecf20Sopenharmony_ci#include <linux/in6.h>
188c2ecf20Sopenharmony_ci#include <linux/jiffies.h>
198c2ecf20Sopenharmony_ci#include <linux/time.h>
208c2ecf20Sopenharmony_ci#include <linux/cpumask.h>
218c2ecf20Sopenharmony_ci#include <net/inet_ecn.h>
228c2ecf20Sopenharmony_ci#include <net/ip_tunnels.h>
238c2ecf20Sopenharmony_ci#include <net/dst_metadata.h>
248c2ecf20Sopenharmony_ci#include <net/nsh.h>
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_cistruct sk_buff;
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_cienum sw_flow_mac_proto {
298c2ecf20Sopenharmony_ci	MAC_PROTO_NONE = 0,
308c2ecf20Sopenharmony_ci	MAC_PROTO_ETHERNET,
318c2ecf20Sopenharmony_ci};
328c2ecf20Sopenharmony_ci#define SW_FLOW_KEY_INVALID	0x80
338c2ecf20Sopenharmony_ci#define MPLS_LABEL_DEPTH       3
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci/* Store options at the end of the array if they are less than the
368c2ecf20Sopenharmony_ci * maximum size. This allows us to get the benefits of variable length
378c2ecf20Sopenharmony_ci * matching for small options.
388c2ecf20Sopenharmony_ci */
398c2ecf20Sopenharmony_ci#define TUN_METADATA_OFFSET(opt_len) \
408c2ecf20Sopenharmony_ci	(sizeof_field(struct sw_flow_key, tun_opts) - opt_len)
418c2ecf20Sopenharmony_ci#define TUN_METADATA_OPTS(flow_key, opt_len) \
428c2ecf20Sopenharmony_ci	((void *)((flow_key)->tun_opts + TUN_METADATA_OFFSET(opt_len)))
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_cistruct ovs_tunnel_info {
458c2ecf20Sopenharmony_ci	struct metadata_dst	*tun_dst;
468c2ecf20Sopenharmony_ci};
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_cistruct vlan_head {
498c2ecf20Sopenharmony_ci	__be16 tpid; /* Vlan type. Generally 802.1q or 802.1ad.*/
508c2ecf20Sopenharmony_ci	__be16 tci;  /* 0 if no VLAN, VLAN_CFI_MASK set otherwise. */
518c2ecf20Sopenharmony_ci};
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci#define OVS_SW_FLOW_KEY_METADATA_SIZE			\
548c2ecf20Sopenharmony_ci	(offsetof(struct sw_flow_key, recirc_id) +	\
558c2ecf20Sopenharmony_ci	sizeof_field(struct sw_flow_key, recirc_id))
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_cistruct ovs_key_nsh {
588c2ecf20Sopenharmony_ci	struct ovs_nsh_key_base base;
598c2ecf20Sopenharmony_ci	__be32 context[NSH_MD1_CONTEXT_SIZE];
608c2ecf20Sopenharmony_ci};
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_cistruct sw_flow_key {
638c2ecf20Sopenharmony_ci	u8 tun_opts[IP_TUNNEL_OPTS_MAX];
648c2ecf20Sopenharmony_ci	u8 tun_opts_len;
658c2ecf20Sopenharmony_ci	struct ip_tunnel_key tun_key;	/* Encapsulating tunnel key. */
668c2ecf20Sopenharmony_ci	struct {
678c2ecf20Sopenharmony_ci		u32	priority;	/* Packet QoS priority. */
688c2ecf20Sopenharmony_ci		u32	skb_mark;	/* SKB mark. */
698c2ecf20Sopenharmony_ci		u16	in_port;	/* Input switch port (or DP_MAX_PORTS). */
708c2ecf20Sopenharmony_ci	} __packed phy; /* Safe when right after 'tun_key'. */
718c2ecf20Sopenharmony_ci	u8 mac_proto;			/* MAC layer protocol (e.g. Ethernet). */
728c2ecf20Sopenharmony_ci	u8 tun_proto;			/* Protocol of encapsulating tunnel. */
738c2ecf20Sopenharmony_ci	u32 ovs_flow_hash;		/* Datapath computed hash value.  */
748c2ecf20Sopenharmony_ci	u32 recirc_id;			/* Recirculation ID.  */
758c2ecf20Sopenharmony_ci	struct {
768c2ecf20Sopenharmony_ci		u8     src[ETH_ALEN];	/* Ethernet source address. */
778c2ecf20Sopenharmony_ci		u8     dst[ETH_ALEN];	/* Ethernet destination address. */
788c2ecf20Sopenharmony_ci		struct vlan_head vlan;
798c2ecf20Sopenharmony_ci		struct vlan_head cvlan;
808c2ecf20Sopenharmony_ci		__be16 type;		/* Ethernet frame type. */
818c2ecf20Sopenharmony_ci	} eth;
828c2ecf20Sopenharmony_ci	/* Filling a hole of two bytes. */
838c2ecf20Sopenharmony_ci	u8 ct_state;
848c2ecf20Sopenharmony_ci	u8 ct_orig_proto;		/* CT original direction tuple IP
858c2ecf20Sopenharmony_ci					 * protocol.
868c2ecf20Sopenharmony_ci					 */
878c2ecf20Sopenharmony_ci	union {
888c2ecf20Sopenharmony_ci		struct {
898c2ecf20Sopenharmony_ci			u8     proto;	/* IP protocol or lower 8 bits of ARP opcode. */
908c2ecf20Sopenharmony_ci			u8     tos;	    /* IP ToS. */
918c2ecf20Sopenharmony_ci			u8     ttl;	    /* IP TTL/hop limit. */
928c2ecf20Sopenharmony_ci			u8     frag;	/* One of OVS_FRAG_TYPE_*. */
938c2ecf20Sopenharmony_ci		} ip;
948c2ecf20Sopenharmony_ci	};
958c2ecf20Sopenharmony_ci	u16 ct_zone;			/* Conntrack zone. */
968c2ecf20Sopenharmony_ci	struct {
978c2ecf20Sopenharmony_ci		__be16 src;		/* TCP/UDP/SCTP source port. */
988c2ecf20Sopenharmony_ci		__be16 dst;		/* TCP/UDP/SCTP destination port. */
998c2ecf20Sopenharmony_ci		__be16 flags;		/* TCP flags. */
1008c2ecf20Sopenharmony_ci	} tp;
1018c2ecf20Sopenharmony_ci	union {
1028c2ecf20Sopenharmony_ci		struct {
1038c2ecf20Sopenharmony_ci			struct {
1048c2ecf20Sopenharmony_ci				__be32 src;	/* IP source address. */
1058c2ecf20Sopenharmony_ci				__be32 dst;	/* IP destination address. */
1068c2ecf20Sopenharmony_ci			} addr;
1078c2ecf20Sopenharmony_ci			union {
1088c2ecf20Sopenharmony_ci				struct {
1098c2ecf20Sopenharmony_ci					__be32 src;
1108c2ecf20Sopenharmony_ci					__be32 dst;
1118c2ecf20Sopenharmony_ci				} ct_orig;	/* Conntrack original direction fields. */
1128c2ecf20Sopenharmony_ci				struct {
1138c2ecf20Sopenharmony_ci					u8 sha[ETH_ALEN];	/* ARP source hardware address. */
1148c2ecf20Sopenharmony_ci					u8 tha[ETH_ALEN];	/* ARP target hardware address. */
1158c2ecf20Sopenharmony_ci				} arp;
1168c2ecf20Sopenharmony_ci			};
1178c2ecf20Sopenharmony_ci		} ipv4;
1188c2ecf20Sopenharmony_ci		struct {
1198c2ecf20Sopenharmony_ci			struct {
1208c2ecf20Sopenharmony_ci				struct in6_addr src;	/* IPv6 source address. */
1218c2ecf20Sopenharmony_ci				struct in6_addr dst;	/* IPv6 destination address. */
1228c2ecf20Sopenharmony_ci			} addr;
1238c2ecf20Sopenharmony_ci			__be32 label;			/* IPv6 flow label. */
1248c2ecf20Sopenharmony_ci			union {
1258c2ecf20Sopenharmony_ci				struct {
1268c2ecf20Sopenharmony_ci					struct in6_addr src;
1278c2ecf20Sopenharmony_ci					struct in6_addr dst;
1288c2ecf20Sopenharmony_ci				} ct_orig;	/* Conntrack original direction fields. */
1298c2ecf20Sopenharmony_ci				struct {
1308c2ecf20Sopenharmony_ci					struct in6_addr target;	/* ND target address. */
1318c2ecf20Sopenharmony_ci					u8 sll[ETH_ALEN];	/* ND source link layer address. */
1328c2ecf20Sopenharmony_ci					u8 tll[ETH_ALEN];	/* ND target link layer address. */
1338c2ecf20Sopenharmony_ci				} nd;
1348c2ecf20Sopenharmony_ci			};
1358c2ecf20Sopenharmony_ci		} ipv6;
1368c2ecf20Sopenharmony_ci		struct {
1378c2ecf20Sopenharmony_ci			u32 num_labels_mask;    /* labels present bitmap of effective length MPLS_LABEL_DEPTH */
1388c2ecf20Sopenharmony_ci			__be32 lse[MPLS_LABEL_DEPTH];     /* label stack entry  */
1398c2ecf20Sopenharmony_ci		} mpls;
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci		struct ovs_key_nsh nsh;         /* network service header */
1428c2ecf20Sopenharmony_ci	};
1438c2ecf20Sopenharmony_ci	struct {
1448c2ecf20Sopenharmony_ci		/* Connection tracking fields not packed above. */
1458c2ecf20Sopenharmony_ci		struct {
1468c2ecf20Sopenharmony_ci			__be16 src;	/* CT orig tuple tp src port. */
1478c2ecf20Sopenharmony_ci			__be16 dst;	/* CT orig tuple tp dst port. */
1488c2ecf20Sopenharmony_ci		} orig_tp;
1498c2ecf20Sopenharmony_ci		u32 mark;
1508c2ecf20Sopenharmony_ci		struct ovs_key_ct_labels labels;
1518c2ecf20Sopenharmony_ci	} ct;
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci} __aligned(BITS_PER_LONG/8); /* Ensure that we can do comparisons as longs. */
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_cistatic inline bool sw_flow_key_is_nd(const struct sw_flow_key *key)
1568c2ecf20Sopenharmony_ci{
1578c2ecf20Sopenharmony_ci	return key->eth.type == htons(ETH_P_IPV6) &&
1588c2ecf20Sopenharmony_ci		key->ip.proto == NEXTHDR_ICMP &&
1598c2ecf20Sopenharmony_ci		key->tp.dst == 0 &&
1608c2ecf20Sopenharmony_ci		(key->tp.src == htons(NDISC_NEIGHBOUR_SOLICITATION) ||
1618c2ecf20Sopenharmony_ci		 key->tp.src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT));
1628c2ecf20Sopenharmony_ci}
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_cistruct sw_flow_key_range {
1658c2ecf20Sopenharmony_ci	unsigned short int start;
1668c2ecf20Sopenharmony_ci	unsigned short int end;
1678c2ecf20Sopenharmony_ci};
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_cistruct sw_flow_mask {
1708c2ecf20Sopenharmony_ci	int ref_count;
1718c2ecf20Sopenharmony_ci	struct rcu_head rcu;
1728c2ecf20Sopenharmony_ci	struct sw_flow_key_range range;
1738c2ecf20Sopenharmony_ci	struct sw_flow_key key;
1748c2ecf20Sopenharmony_ci};
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_cistruct sw_flow_match {
1778c2ecf20Sopenharmony_ci	struct sw_flow_key *key;
1788c2ecf20Sopenharmony_ci	struct sw_flow_key_range range;
1798c2ecf20Sopenharmony_ci	struct sw_flow_mask *mask;
1808c2ecf20Sopenharmony_ci};
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci#define MAX_UFID_LENGTH 16 /* 128 bits */
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_cistruct sw_flow_id {
1858c2ecf20Sopenharmony_ci	u32 ufid_len;
1868c2ecf20Sopenharmony_ci	union {
1878c2ecf20Sopenharmony_ci		u32 ufid[MAX_UFID_LENGTH / 4];
1888c2ecf20Sopenharmony_ci		struct sw_flow_key *unmasked_key;
1898c2ecf20Sopenharmony_ci	};
1908c2ecf20Sopenharmony_ci};
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_cistruct sw_flow_actions {
1938c2ecf20Sopenharmony_ci	struct rcu_head rcu;
1948c2ecf20Sopenharmony_ci	size_t orig_len;	/* From flow_cmd_new netlink actions size */
1958c2ecf20Sopenharmony_ci	u32 actions_len;
1968c2ecf20Sopenharmony_ci	struct nlattr actions[];
1978c2ecf20Sopenharmony_ci};
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_cistruct sw_flow_stats {
2008c2ecf20Sopenharmony_ci	u64 packet_count;		/* Number of packets matched. */
2018c2ecf20Sopenharmony_ci	u64 byte_count;			/* Number of bytes matched. */
2028c2ecf20Sopenharmony_ci	unsigned long used;		/* Last used time (in jiffies). */
2038c2ecf20Sopenharmony_ci	spinlock_t lock;		/* Lock for atomic stats update. */
2048c2ecf20Sopenharmony_ci	__be16 tcp_flags;		/* Union of seen TCP flags. */
2058c2ecf20Sopenharmony_ci};
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_cistruct sw_flow {
2088c2ecf20Sopenharmony_ci	struct rcu_head rcu;
2098c2ecf20Sopenharmony_ci	struct {
2108c2ecf20Sopenharmony_ci		struct hlist_node node[2];
2118c2ecf20Sopenharmony_ci		u32 hash;
2128c2ecf20Sopenharmony_ci	} flow_table, ufid_table;
2138c2ecf20Sopenharmony_ci	int stats_last_writer;		/* CPU id of the last writer on
2148c2ecf20Sopenharmony_ci					 * 'stats[0]'.
2158c2ecf20Sopenharmony_ci					 */
2168c2ecf20Sopenharmony_ci	struct sw_flow_key key;
2178c2ecf20Sopenharmony_ci	struct sw_flow_id id;
2188c2ecf20Sopenharmony_ci	struct cpumask cpu_used_mask;
2198c2ecf20Sopenharmony_ci	struct sw_flow_mask *mask;
2208c2ecf20Sopenharmony_ci	struct sw_flow_actions __rcu *sf_acts;
2218c2ecf20Sopenharmony_ci	struct sw_flow_stats __rcu *stats[]; /* One for each CPU.  First one
2228c2ecf20Sopenharmony_ci					   * is allocated at flow creation time,
2238c2ecf20Sopenharmony_ci					   * the rest are allocated on demand
2248c2ecf20Sopenharmony_ci					   * while holding the 'stats[0].lock'.
2258c2ecf20Sopenharmony_ci					   */
2268c2ecf20Sopenharmony_ci};
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_cistruct arp_eth_header {
2298c2ecf20Sopenharmony_ci	__be16      ar_hrd;	/* format of hardware address   */
2308c2ecf20Sopenharmony_ci	__be16      ar_pro;	/* format of protocol address   */
2318c2ecf20Sopenharmony_ci	unsigned char   ar_hln;	/* length of hardware address   */
2328c2ecf20Sopenharmony_ci	unsigned char   ar_pln;	/* length of protocol address   */
2338c2ecf20Sopenharmony_ci	__be16      ar_op;	/* ARP opcode (command)     */
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_ci	/* Ethernet+IPv4 specific members. */
2368c2ecf20Sopenharmony_ci	unsigned char       ar_sha[ETH_ALEN];	/* sender hardware address  */
2378c2ecf20Sopenharmony_ci	unsigned char       ar_sip[4];		/* sender IP address        */
2388c2ecf20Sopenharmony_ci	unsigned char       ar_tha[ETH_ALEN];	/* target hardware address  */
2398c2ecf20Sopenharmony_ci	unsigned char       ar_tip[4];		/* target IP address        */
2408c2ecf20Sopenharmony_ci} __packed;
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_cistatic inline u8 ovs_key_mac_proto(const struct sw_flow_key *key)
2438c2ecf20Sopenharmony_ci{
2448c2ecf20Sopenharmony_ci	return key->mac_proto & ~SW_FLOW_KEY_INVALID;
2458c2ecf20Sopenharmony_ci}
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_cistatic inline u16 __ovs_mac_header_len(u8 mac_proto)
2488c2ecf20Sopenharmony_ci{
2498c2ecf20Sopenharmony_ci	return mac_proto == MAC_PROTO_ETHERNET ? ETH_HLEN : 0;
2508c2ecf20Sopenharmony_ci}
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_cistatic inline u16 ovs_mac_header_len(const struct sw_flow_key *key)
2538c2ecf20Sopenharmony_ci{
2548c2ecf20Sopenharmony_ci	return __ovs_mac_header_len(ovs_key_mac_proto(key));
2558c2ecf20Sopenharmony_ci}
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_cistatic inline bool ovs_identifier_is_ufid(const struct sw_flow_id *sfid)
2588c2ecf20Sopenharmony_ci{
2598c2ecf20Sopenharmony_ci	return sfid->ufid_len;
2608c2ecf20Sopenharmony_ci}
2618c2ecf20Sopenharmony_ci
2628c2ecf20Sopenharmony_cistatic inline bool ovs_identifier_is_key(const struct sw_flow_id *sfid)
2638c2ecf20Sopenharmony_ci{
2648c2ecf20Sopenharmony_ci	return !ovs_identifier_is_ufid(sfid);
2658c2ecf20Sopenharmony_ci}
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_civoid ovs_flow_stats_update(struct sw_flow *, __be16 tcp_flags,
2688c2ecf20Sopenharmony_ci			   const struct sk_buff *);
2698c2ecf20Sopenharmony_civoid ovs_flow_stats_get(const struct sw_flow *, struct ovs_flow_stats *,
2708c2ecf20Sopenharmony_ci			unsigned long *used, __be16 *tcp_flags);
2718c2ecf20Sopenharmony_civoid ovs_flow_stats_clear(struct sw_flow *);
2728c2ecf20Sopenharmony_ciu64 ovs_flow_used_time(unsigned long flow_jiffies);
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_ciint ovs_flow_key_update(struct sk_buff *skb, struct sw_flow_key *key);
2758c2ecf20Sopenharmony_ciint ovs_flow_key_update_l3l4(struct sk_buff *skb, struct sw_flow_key *key);
2768c2ecf20Sopenharmony_ciint ovs_flow_key_extract(const struct ip_tunnel_info *tun_info,
2778c2ecf20Sopenharmony_ci			 struct sk_buff *skb,
2788c2ecf20Sopenharmony_ci			 struct sw_flow_key *key);
2798c2ecf20Sopenharmony_ci/* Extract key from packet coming from userspace. */
2808c2ecf20Sopenharmony_ciint ovs_flow_key_extract_userspace(struct net *net, const struct nlattr *attr,
2818c2ecf20Sopenharmony_ci				   struct sk_buff *skb,
2828c2ecf20Sopenharmony_ci				   struct sw_flow_key *key, bool log);
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_ci#endif /* flow.h */
285