18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (c) 2007-2014 Nicira, Inc.
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#ifndef DATAPATH_H
78c2ecf20Sopenharmony_ci#define DATAPATH_H 1
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include <asm/page.h>
108c2ecf20Sopenharmony_ci#include <linux/kernel.h>
118c2ecf20Sopenharmony_ci#include <linux/mutex.h>
128c2ecf20Sopenharmony_ci#include <linux/netdevice.h>
138c2ecf20Sopenharmony_ci#include <linux/skbuff.h>
148c2ecf20Sopenharmony_ci#include <linux/u64_stats_sync.h>
158c2ecf20Sopenharmony_ci#include <net/ip_tunnels.h>
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#include "conntrack.h"
188c2ecf20Sopenharmony_ci#include "flow.h"
198c2ecf20Sopenharmony_ci#include "flow_table.h"
208c2ecf20Sopenharmony_ci#include "meter.h"
218c2ecf20Sopenharmony_ci#include "vport-internal_dev.h"
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci#define DP_MAX_PORTS                USHRT_MAX
248c2ecf20Sopenharmony_ci#define DP_VPORT_HASH_BUCKETS       1024
258c2ecf20Sopenharmony_ci#define DP_MASKS_REBALANCE_INTERVAL 4000
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci/**
288c2ecf20Sopenharmony_ci * struct dp_stats_percpu - per-cpu packet processing statistics for a given
298c2ecf20Sopenharmony_ci * datapath.
308c2ecf20Sopenharmony_ci * @n_hit: Number of received packets for which a matching flow was found in
318c2ecf20Sopenharmony_ci * the flow table.
328c2ecf20Sopenharmony_ci * @n_miss: Number of received packets that had no matching flow in the flow
338c2ecf20Sopenharmony_ci * table.  The sum of @n_hit and @n_miss is the number of packets that have
348c2ecf20Sopenharmony_ci * been received by the datapath.
358c2ecf20Sopenharmony_ci * @n_lost: Number of received packets that had no matching flow in the flow
368c2ecf20Sopenharmony_ci * table that could not be sent to userspace (normally due to an overflow in
378c2ecf20Sopenharmony_ci * one of the datapath's queues).
388c2ecf20Sopenharmony_ci * @n_mask_hit: Number of masks looked up for flow match.
398c2ecf20Sopenharmony_ci *   @n_mask_hit / (@n_hit + @n_missed)  will be the average masks looked
408c2ecf20Sopenharmony_ci *   up per packet.
418c2ecf20Sopenharmony_ci * @n_cache_hit: The number of received packets that had their mask found using
428c2ecf20Sopenharmony_ci * the mask cache.
438c2ecf20Sopenharmony_ci */
448c2ecf20Sopenharmony_cistruct dp_stats_percpu {
458c2ecf20Sopenharmony_ci	u64 n_hit;
468c2ecf20Sopenharmony_ci	u64 n_missed;
478c2ecf20Sopenharmony_ci	u64 n_lost;
488c2ecf20Sopenharmony_ci	u64 n_mask_hit;
498c2ecf20Sopenharmony_ci	u64 n_cache_hit;
508c2ecf20Sopenharmony_ci	struct u64_stats_sync syncp;
518c2ecf20Sopenharmony_ci};
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci/**
548c2ecf20Sopenharmony_ci * struct datapath - datapath for flow-based packet switching
558c2ecf20Sopenharmony_ci * @rcu: RCU callback head for deferred destruction.
568c2ecf20Sopenharmony_ci * @list_node: Element in global 'dps' list.
578c2ecf20Sopenharmony_ci * @table: flow table.
588c2ecf20Sopenharmony_ci * @ports: Hash table for ports.  %OVSP_LOCAL port always exists.  Protected by
598c2ecf20Sopenharmony_ci * ovs_mutex and RCU.
608c2ecf20Sopenharmony_ci * @stats_percpu: Per-CPU datapath statistics.
618c2ecf20Sopenharmony_ci * @net: Reference to net namespace.
628c2ecf20Sopenharmony_ci * @max_headroom: the maximum headroom of all vports in this datapath; it will
638c2ecf20Sopenharmony_ci * be used by all the internal vports in this dp.
648c2ecf20Sopenharmony_ci *
658c2ecf20Sopenharmony_ci * Context: See the comment on locking at the top of datapath.c for additional
668c2ecf20Sopenharmony_ci * locking information.
678c2ecf20Sopenharmony_ci */
688c2ecf20Sopenharmony_cistruct datapath {
698c2ecf20Sopenharmony_ci	struct rcu_head rcu;
708c2ecf20Sopenharmony_ci	struct list_head list_node;
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci	/* Flow table. */
738c2ecf20Sopenharmony_ci	struct flow_table table;
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci	/* Switch ports. */
768c2ecf20Sopenharmony_ci	struct hlist_head *ports;
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci	/* Stats. */
798c2ecf20Sopenharmony_ci	struct dp_stats_percpu __percpu *stats_percpu;
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci	/* Network namespace ref. */
828c2ecf20Sopenharmony_ci	possible_net_t net;
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci	u32 user_features;
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci	u32 max_headroom;
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci	/* Switch meters. */
898c2ecf20Sopenharmony_ci	struct dp_meter_table meter_tbl;
908c2ecf20Sopenharmony_ci};
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci/**
938c2ecf20Sopenharmony_ci * struct ovs_skb_cb - OVS data in skb CB
948c2ecf20Sopenharmony_ci * @input_vport: The original vport packet came in on. This value is cached
958c2ecf20Sopenharmony_ci * when a packet is received by OVS.
968c2ecf20Sopenharmony_ci * @mru: The maximum received fragement size; 0 if the packet is not
978c2ecf20Sopenharmony_ci * fragmented.
988c2ecf20Sopenharmony_ci * @acts_origlen: The netlink size of the flow actions applied to this skb.
998c2ecf20Sopenharmony_ci * @cutlen: The number of bytes from the packet end to be removed.
1008c2ecf20Sopenharmony_ci */
1018c2ecf20Sopenharmony_cistruct ovs_skb_cb {
1028c2ecf20Sopenharmony_ci	struct vport		*input_vport;
1038c2ecf20Sopenharmony_ci	u16			mru;
1048c2ecf20Sopenharmony_ci	u16			acts_origlen;
1058c2ecf20Sopenharmony_ci	u32			cutlen;
1068c2ecf20Sopenharmony_ci};
1078c2ecf20Sopenharmony_ci#define OVS_CB(skb) ((struct ovs_skb_cb *)(skb)->cb)
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci/**
1108c2ecf20Sopenharmony_ci * struct dp_upcall - metadata to include with a packet to send to userspace
1118c2ecf20Sopenharmony_ci * @cmd: One of %OVS_PACKET_CMD_*.
1128c2ecf20Sopenharmony_ci * @userdata: If nonnull, its variable-length value is passed to userspace as
1138c2ecf20Sopenharmony_ci * %OVS_PACKET_ATTR_USERDATA.
1148c2ecf20Sopenharmony_ci * @portid: Netlink portid to which packet should be sent.  If @portid is 0
1158c2ecf20Sopenharmony_ci * then no packet is sent and the packet is accounted in the datapath's @n_lost
1168c2ecf20Sopenharmony_ci * counter.
1178c2ecf20Sopenharmony_ci * @egress_tun_info: If nonnull, becomes %OVS_PACKET_ATTR_EGRESS_TUN_KEY.
1188c2ecf20Sopenharmony_ci * @mru: If not zero, Maximum received IP fragment size.
1198c2ecf20Sopenharmony_ci */
1208c2ecf20Sopenharmony_cistruct dp_upcall_info {
1218c2ecf20Sopenharmony_ci	struct ip_tunnel_info *egress_tun_info;
1228c2ecf20Sopenharmony_ci	const struct nlattr *userdata;
1238c2ecf20Sopenharmony_ci	const struct nlattr *actions;
1248c2ecf20Sopenharmony_ci	int actions_len;
1258c2ecf20Sopenharmony_ci	u32 portid;
1268c2ecf20Sopenharmony_ci	u8 cmd;
1278c2ecf20Sopenharmony_ci	u16 mru;
1288c2ecf20Sopenharmony_ci};
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci/**
1318c2ecf20Sopenharmony_ci * struct ovs_net - Per net-namespace data for ovs.
1328c2ecf20Sopenharmony_ci * @dps: List of datapaths to enable dumping them all out.
1338c2ecf20Sopenharmony_ci * Protected by genl_mutex.
1348c2ecf20Sopenharmony_ci */
1358c2ecf20Sopenharmony_cistruct ovs_net {
1368c2ecf20Sopenharmony_ci	struct list_head dps;
1378c2ecf20Sopenharmony_ci	struct work_struct dp_notify_work;
1388c2ecf20Sopenharmony_ci	struct delayed_work masks_rebalance;
1398c2ecf20Sopenharmony_ci#if	IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
1408c2ecf20Sopenharmony_ci	struct ovs_ct_limit_info *ct_limit_info;
1418c2ecf20Sopenharmony_ci#endif
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci	/* Module reference for configuring conntrack. */
1448c2ecf20Sopenharmony_ci	bool xt_label;
1458c2ecf20Sopenharmony_ci};
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci/**
1488c2ecf20Sopenharmony_ci * enum ovs_pkt_hash_types - hash info to include with a packet
1498c2ecf20Sopenharmony_ci * to send to userspace.
1508c2ecf20Sopenharmony_ci * @OVS_PACKET_HASH_SW_BIT: indicates hash was computed in software stack.
1518c2ecf20Sopenharmony_ci * @OVS_PACKET_HASH_L4_BIT: indicates hash is a canonical 4-tuple hash
1528c2ecf20Sopenharmony_ci * over transport ports.
1538c2ecf20Sopenharmony_ci */
1548c2ecf20Sopenharmony_cienum ovs_pkt_hash_types {
1558c2ecf20Sopenharmony_ci	OVS_PACKET_HASH_SW_BIT = (1ULL << 32),
1568c2ecf20Sopenharmony_ci	OVS_PACKET_HASH_L4_BIT = (1ULL << 33),
1578c2ecf20Sopenharmony_ci};
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ciextern unsigned int ovs_net_id;
1608c2ecf20Sopenharmony_civoid ovs_lock(void);
1618c2ecf20Sopenharmony_civoid ovs_unlock(void);
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci#ifdef CONFIG_LOCKDEP
1648c2ecf20Sopenharmony_ciint lockdep_ovsl_is_held(void);
1658c2ecf20Sopenharmony_ci#else
1668c2ecf20Sopenharmony_ci#define lockdep_ovsl_is_held()	1
1678c2ecf20Sopenharmony_ci#endif
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci#define ASSERT_OVSL()		WARN_ON(!lockdep_ovsl_is_held())
1708c2ecf20Sopenharmony_ci#define ovsl_dereference(p)					\
1718c2ecf20Sopenharmony_ci	rcu_dereference_protected(p, lockdep_ovsl_is_held())
1728c2ecf20Sopenharmony_ci#define rcu_dereference_ovsl(p)					\
1738c2ecf20Sopenharmony_ci	rcu_dereference_check(p, lockdep_ovsl_is_held())
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_cistatic inline struct net *ovs_dp_get_net(const struct datapath *dp)
1768c2ecf20Sopenharmony_ci{
1778c2ecf20Sopenharmony_ci	return read_pnet(&dp->net);
1788c2ecf20Sopenharmony_ci}
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_cistatic inline void ovs_dp_set_net(struct datapath *dp, struct net *net)
1818c2ecf20Sopenharmony_ci{
1828c2ecf20Sopenharmony_ci	write_pnet(&dp->net, net);
1838c2ecf20Sopenharmony_ci}
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_cistruct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no);
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_cistatic inline struct vport *ovs_vport_rcu(const struct datapath *dp, int port_no)
1888c2ecf20Sopenharmony_ci{
1898c2ecf20Sopenharmony_ci	WARN_ON_ONCE(!rcu_read_lock_held());
1908c2ecf20Sopenharmony_ci	return ovs_lookup_vport(dp, port_no);
1918c2ecf20Sopenharmony_ci}
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_cistatic inline struct vport *ovs_vport_ovsl_rcu(const struct datapath *dp, int port_no)
1948c2ecf20Sopenharmony_ci{
1958c2ecf20Sopenharmony_ci	WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_ovsl_is_held());
1968c2ecf20Sopenharmony_ci	return ovs_lookup_vport(dp, port_no);
1978c2ecf20Sopenharmony_ci}
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_cistatic inline struct vport *ovs_vport_ovsl(const struct datapath *dp, int port_no)
2008c2ecf20Sopenharmony_ci{
2018c2ecf20Sopenharmony_ci	ASSERT_OVSL();
2028c2ecf20Sopenharmony_ci	return ovs_lookup_vport(dp, port_no);
2038c2ecf20Sopenharmony_ci}
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_ci/* Must be called with rcu_read_lock. */
2068c2ecf20Sopenharmony_cistatic inline struct datapath *get_dp_rcu(struct net *net, int dp_ifindex)
2078c2ecf20Sopenharmony_ci{
2088c2ecf20Sopenharmony_ci	struct net_device *dev = dev_get_by_index_rcu(net, dp_ifindex);
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ci	if (dev) {
2118c2ecf20Sopenharmony_ci		struct vport *vport = ovs_internal_dev_get_vport(dev);
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci		if (vport)
2148c2ecf20Sopenharmony_ci			return vport->dp;
2158c2ecf20Sopenharmony_ci	}
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci	return NULL;
2188c2ecf20Sopenharmony_ci}
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci/* The caller must hold either ovs_mutex or rcu_read_lock to keep the
2218c2ecf20Sopenharmony_ci * returned dp pointer valid.
2228c2ecf20Sopenharmony_ci */
2238c2ecf20Sopenharmony_cistatic inline struct datapath *get_dp(struct net *net, int dp_ifindex)
2248c2ecf20Sopenharmony_ci{
2258c2ecf20Sopenharmony_ci	struct datapath *dp;
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_ci	WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_ovsl_is_held());
2288c2ecf20Sopenharmony_ci	rcu_read_lock();
2298c2ecf20Sopenharmony_ci	dp = get_dp_rcu(net, dp_ifindex);
2308c2ecf20Sopenharmony_ci	rcu_read_unlock();
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ci	return dp;
2338c2ecf20Sopenharmony_ci}
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_ciextern struct notifier_block ovs_dp_device_notifier;
2368c2ecf20Sopenharmony_ciextern struct genl_family dp_vport_genl_family;
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_ciDECLARE_STATIC_KEY_FALSE(tc_recirc_sharing_support);
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_civoid ovs_dp_process_packet(struct sk_buff *skb, struct sw_flow_key *key);
2418c2ecf20Sopenharmony_civoid ovs_dp_detach_port(struct vport *);
2428c2ecf20Sopenharmony_ciint ovs_dp_upcall(struct datapath *, struct sk_buff *,
2438c2ecf20Sopenharmony_ci		  const struct sw_flow_key *, const struct dp_upcall_info *,
2448c2ecf20Sopenharmony_ci		  uint32_t cutlen);
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ciconst char *ovs_dp_name(const struct datapath *dp);
2478c2ecf20Sopenharmony_cistruct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, struct net *net,
2488c2ecf20Sopenharmony_ci					 u32 portid, u32 seq, u8 cmd);
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_ciint ovs_execute_actions(struct datapath *dp, struct sk_buff *skb,
2518c2ecf20Sopenharmony_ci			const struct sw_flow_actions *, struct sw_flow_key *);
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_civoid ovs_dp_notify_wq(struct work_struct *work);
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_ciint action_fifos_init(void);
2568c2ecf20Sopenharmony_civoid action_fifos_exit(void);
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_ci/* 'KEY' must not have any bits set outside of the 'MASK' */
2598c2ecf20Sopenharmony_ci#define OVS_MASKED(OLD, KEY, MASK) ((KEY) | ((OLD) & ~(MASK)))
2608c2ecf20Sopenharmony_ci#define OVS_SET_MASKED(OLD, KEY, MASK) ((OLD) = OVS_MASKED(OLD, KEY, MASK))
2618c2ecf20Sopenharmony_ci
2628c2ecf20Sopenharmony_ci#define OVS_NLERR(logging_allowed, fmt, ...)			\
2638c2ecf20Sopenharmony_cido {								\
2648c2ecf20Sopenharmony_ci	if (logging_allowed && net_ratelimit())			\
2658c2ecf20Sopenharmony_ci		pr_info("netlink: " fmt "\n", ##__VA_ARGS__);	\
2668c2ecf20Sopenharmony_ci} while (0)
2678c2ecf20Sopenharmony_ci#endif /* datapath.h */
268