162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright (c) 2007-2014 Nicira, Inc.
462306a36Sopenharmony_ci */
562306a36Sopenharmony_ci
662306a36Sopenharmony_ci#ifndef DATAPATH_H
762306a36Sopenharmony_ci#define DATAPATH_H 1
862306a36Sopenharmony_ci
962306a36Sopenharmony_ci#include <asm/page.h>
1062306a36Sopenharmony_ci#include <linux/kernel.h>
1162306a36Sopenharmony_ci#include <linux/mutex.h>
1262306a36Sopenharmony_ci#include <linux/netdevice.h>
1362306a36Sopenharmony_ci#include <linux/skbuff.h>
1462306a36Sopenharmony_ci#include <linux/u64_stats_sync.h>
1562306a36Sopenharmony_ci#include <net/ip_tunnels.h>
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_ci#include "conntrack.h"
1862306a36Sopenharmony_ci#include "flow.h"
1962306a36Sopenharmony_ci#include "flow_table.h"
2062306a36Sopenharmony_ci#include "meter.h"
2162306a36Sopenharmony_ci#include "vport-internal_dev.h"
2262306a36Sopenharmony_ci
2362306a36Sopenharmony_ci#define DP_MAX_PORTS                USHRT_MAX
2462306a36Sopenharmony_ci#define DP_VPORT_HASH_BUCKETS       1024
2562306a36Sopenharmony_ci#define DP_MASKS_REBALANCE_INTERVAL 4000
2662306a36Sopenharmony_ci
2762306a36Sopenharmony_ci/**
2862306a36Sopenharmony_ci * struct dp_stats_percpu - per-cpu packet processing statistics for a given
2962306a36Sopenharmony_ci * datapath.
3062306a36Sopenharmony_ci * @n_hit: Number of received packets for which a matching flow was found in
3162306a36Sopenharmony_ci * the flow table.
3262306a36Sopenharmony_ci * @n_miss: Number of received packets that had no matching flow in the flow
3362306a36Sopenharmony_ci * table.  The sum of @n_hit and @n_miss is the number of packets that have
3462306a36Sopenharmony_ci * been received by the datapath.
3562306a36Sopenharmony_ci * @n_lost: Number of received packets that had no matching flow in the flow
3662306a36Sopenharmony_ci * table that could not be sent to userspace (normally due to an overflow in
3762306a36Sopenharmony_ci * one of the datapath's queues).
3862306a36Sopenharmony_ci * @n_mask_hit: Number of masks looked up for flow match.
3962306a36Sopenharmony_ci *   @n_mask_hit / (@n_hit + @n_missed)  will be the average masks looked
4062306a36Sopenharmony_ci *   up per packet.
4162306a36Sopenharmony_ci * @n_cache_hit: The number of received packets that had their mask found using
4262306a36Sopenharmony_ci * the mask cache.
4362306a36Sopenharmony_ci */
4462306a36Sopenharmony_cistruct dp_stats_percpu {
4562306a36Sopenharmony_ci	u64 n_hit;
4662306a36Sopenharmony_ci	u64 n_missed;
4762306a36Sopenharmony_ci	u64 n_lost;
4862306a36Sopenharmony_ci	u64 n_mask_hit;
4962306a36Sopenharmony_ci	u64 n_cache_hit;
5062306a36Sopenharmony_ci	struct u64_stats_sync syncp;
5162306a36Sopenharmony_ci};
5262306a36Sopenharmony_ci
5362306a36Sopenharmony_ci/**
5462306a36Sopenharmony_ci * struct dp_nlsk_pids - array of netlink portids of for a datapath.
5562306a36Sopenharmony_ci *                       This is used when OVS_DP_F_DISPATCH_UPCALL_PER_CPU
5662306a36Sopenharmony_ci *                       is enabled and must be protected by rcu.
5762306a36Sopenharmony_ci * @rcu: RCU callback head for deferred destruction.
5862306a36Sopenharmony_ci * @n_pids: Size of @pids array.
5962306a36Sopenharmony_ci * @pids: Array storing the Netlink socket PIDs indexed by CPU ID for packets
6062306a36Sopenharmony_ci *       that miss the flow table.
6162306a36Sopenharmony_ci */
6262306a36Sopenharmony_cistruct dp_nlsk_pids {
6362306a36Sopenharmony_ci	struct rcu_head rcu;
6462306a36Sopenharmony_ci	u32 n_pids;
6562306a36Sopenharmony_ci	u32 pids[];
6662306a36Sopenharmony_ci};
6762306a36Sopenharmony_ci
6862306a36Sopenharmony_ci/**
6962306a36Sopenharmony_ci * struct datapath - datapath for flow-based packet switching
7062306a36Sopenharmony_ci * @rcu: RCU callback head for deferred destruction.
7162306a36Sopenharmony_ci * @list_node: Element in global 'dps' list.
7262306a36Sopenharmony_ci * @table: flow table.
7362306a36Sopenharmony_ci * @ports: Hash table for ports.  %OVSP_LOCAL port always exists.  Protected by
7462306a36Sopenharmony_ci * ovs_mutex and RCU.
7562306a36Sopenharmony_ci * @stats_percpu: Per-CPU datapath statistics.
7662306a36Sopenharmony_ci * @net: Reference to net namespace.
7762306a36Sopenharmony_ci * @max_headroom: the maximum headroom of all vports in this datapath; it will
7862306a36Sopenharmony_ci * be used by all the internal vports in this dp.
7962306a36Sopenharmony_ci * @upcall_portids: RCU protected 'struct dp_nlsk_pids'.
8062306a36Sopenharmony_ci *
8162306a36Sopenharmony_ci * Context: See the comment on locking at the top of datapath.c for additional
8262306a36Sopenharmony_ci * locking information.
8362306a36Sopenharmony_ci */
8462306a36Sopenharmony_cistruct datapath {
8562306a36Sopenharmony_ci	struct rcu_head rcu;
8662306a36Sopenharmony_ci	struct list_head list_node;
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_ci	/* Flow table. */
8962306a36Sopenharmony_ci	struct flow_table table;
9062306a36Sopenharmony_ci
9162306a36Sopenharmony_ci	/* Switch ports. */
9262306a36Sopenharmony_ci	struct hlist_head *ports;
9362306a36Sopenharmony_ci
9462306a36Sopenharmony_ci	/* Stats. */
9562306a36Sopenharmony_ci	struct dp_stats_percpu __percpu *stats_percpu;
9662306a36Sopenharmony_ci
9762306a36Sopenharmony_ci	/* Network namespace ref. */
9862306a36Sopenharmony_ci	possible_net_t net;
9962306a36Sopenharmony_ci
10062306a36Sopenharmony_ci	u32 user_features;
10162306a36Sopenharmony_ci
10262306a36Sopenharmony_ci	u32 max_headroom;
10362306a36Sopenharmony_ci
10462306a36Sopenharmony_ci	/* Switch meters. */
10562306a36Sopenharmony_ci	struct dp_meter_table meter_tbl;
10662306a36Sopenharmony_ci
10762306a36Sopenharmony_ci	struct dp_nlsk_pids __rcu *upcall_portids;
10862306a36Sopenharmony_ci};
10962306a36Sopenharmony_ci
11062306a36Sopenharmony_ci/**
11162306a36Sopenharmony_ci * struct ovs_skb_cb - OVS data in skb CB
11262306a36Sopenharmony_ci * @input_vport: The original vport packet came in on. This value is cached
11362306a36Sopenharmony_ci * when a packet is received by OVS.
11462306a36Sopenharmony_ci * @mru: The maximum received fragement size; 0 if the packet is not
11562306a36Sopenharmony_ci * fragmented.
11662306a36Sopenharmony_ci * @acts_origlen: The netlink size of the flow actions applied to this skb.
11762306a36Sopenharmony_ci * @cutlen: The number of bytes from the packet end to be removed.
11862306a36Sopenharmony_ci */
11962306a36Sopenharmony_cistruct ovs_skb_cb {
12062306a36Sopenharmony_ci	struct vport		*input_vport;
12162306a36Sopenharmony_ci	u16			mru;
12262306a36Sopenharmony_ci	u16			acts_origlen;
12362306a36Sopenharmony_ci	u32			cutlen;
12462306a36Sopenharmony_ci};
12562306a36Sopenharmony_ci#define OVS_CB(skb) ((struct ovs_skb_cb *)(skb)->cb)
12662306a36Sopenharmony_ci
12762306a36Sopenharmony_ci/**
12862306a36Sopenharmony_ci * struct dp_upcall - metadata to include with a packet to send to userspace
12962306a36Sopenharmony_ci * @cmd: One of %OVS_PACKET_CMD_*.
13062306a36Sopenharmony_ci * @userdata: If nonnull, its variable-length value is passed to userspace as
13162306a36Sopenharmony_ci * %OVS_PACKET_ATTR_USERDATA.
13262306a36Sopenharmony_ci * @portid: Netlink portid to which packet should be sent.  If @portid is 0
13362306a36Sopenharmony_ci * then no packet is sent and the packet is accounted in the datapath's @n_lost
13462306a36Sopenharmony_ci * counter.
13562306a36Sopenharmony_ci * @egress_tun_info: If nonnull, becomes %OVS_PACKET_ATTR_EGRESS_TUN_KEY.
13662306a36Sopenharmony_ci * @mru: If not zero, Maximum received IP fragment size.
13762306a36Sopenharmony_ci */
13862306a36Sopenharmony_cistruct dp_upcall_info {
13962306a36Sopenharmony_ci	struct ip_tunnel_info *egress_tun_info;
14062306a36Sopenharmony_ci	const struct nlattr *userdata;
14162306a36Sopenharmony_ci	const struct nlattr *actions;
14262306a36Sopenharmony_ci	int actions_len;
14362306a36Sopenharmony_ci	u32 portid;
14462306a36Sopenharmony_ci	u8 cmd;
14562306a36Sopenharmony_ci	u16 mru;
14662306a36Sopenharmony_ci};
14762306a36Sopenharmony_ci
14862306a36Sopenharmony_ci/**
14962306a36Sopenharmony_ci * struct ovs_net - Per net-namespace data for ovs.
15062306a36Sopenharmony_ci * @dps: List of datapaths to enable dumping them all out.
15162306a36Sopenharmony_ci * Protected by genl_mutex.
15262306a36Sopenharmony_ci */
15362306a36Sopenharmony_cistruct ovs_net {
15462306a36Sopenharmony_ci	struct list_head dps;
15562306a36Sopenharmony_ci	struct work_struct dp_notify_work;
15662306a36Sopenharmony_ci	struct delayed_work masks_rebalance;
15762306a36Sopenharmony_ci#if	IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
15862306a36Sopenharmony_ci	struct ovs_ct_limit_info *ct_limit_info;
15962306a36Sopenharmony_ci#endif
16062306a36Sopenharmony_ci
16162306a36Sopenharmony_ci	/* Module reference for configuring conntrack. */
16262306a36Sopenharmony_ci	bool xt_label;
16362306a36Sopenharmony_ci};
16462306a36Sopenharmony_ci
16562306a36Sopenharmony_ci/**
16662306a36Sopenharmony_ci * enum ovs_pkt_hash_types - hash info to include with a packet
16762306a36Sopenharmony_ci * to send to userspace.
16862306a36Sopenharmony_ci * @OVS_PACKET_HASH_SW_BIT: indicates hash was computed in software stack.
16962306a36Sopenharmony_ci * @OVS_PACKET_HASH_L4_BIT: indicates hash is a canonical 4-tuple hash
17062306a36Sopenharmony_ci * over transport ports.
17162306a36Sopenharmony_ci */
17262306a36Sopenharmony_cienum ovs_pkt_hash_types {
17362306a36Sopenharmony_ci	OVS_PACKET_HASH_SW_BIT = (1ULL << 32),
17462306a36Sopenharmony_ci	OVS_PACKET_HASH_L4_BIT = (1ULL << 33),
17562306a36Sopenharmony_ci};
17662306a36Sopenharmony_ci
17762306a36Sopenharmony_ciextern unsigned int ovs_net_id;
17862306a36Sopenharmony_civoid ovs_lock(void);
17962306a36Sopenharmony_civoid ovs_unlock(void);
18062306a36Sopenharmony_ci
18162306a36Sopenharmony_ci#ifdef CONFIG_LOCKDEP
18262306a36Sopenharmony_ciint lockdep_ovsl_is_held(void);
18362306a36Sopenharmony_ci#else
18462306a36Sopenharmony_ci#define lockdep_ovsl_is_held()	1
18562306a36Sopenharmony_ci#endif
18662306a36Sopenharmony_ci
18762306a36Sopenharmony_ci#define ASSERT_OVSL()		WARN_ON(!lockdep_ovsl_is_held())
18862306a36Sopenharmony_ci#define ovsl_dereference(p)					\
18962306a36Sopenharmony_ci	rcu_dereference_protected(p, lockdep_ovsl_is_held())
19062306a36Sopenharmony_ci#define rcu_dereference_ovsl(p)					\
19162306a36Sopenharmony_ci	rcu_dereference_check(p, lockdep_ovsl_is_held())
19262306a36Sopenharmony_ci
19362306a36Sopenharmony_cistatic inline struct net *ovs_dp_get_net(const struct datapath *dp)
19462306a36Sopenharmony_ci{
19562306a36Sopenharmony_ci	return read_pnet(&dp->net);
19662306a36Sopenharmony_ci}
19762306a36Sopenharmony_ci
19862306a36Sopenharmony_cistatic inline void ovs_dp_set_net(struct datapath *dp, struct net *net)
19962306a36Sopenharmony_ci{
20062306a36Sopenharmony_ci	write_pnet(&dp->net, net);
20162306a36Sopenharmony_ci}
20262306a36Sopenharmony_ci
20362306a36Sopenharmony_cistruct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no);
20462306a36Sopenharmony_ci
20562306a36Sopenharmony_cistatic inline struct vport *ovs_vport_rcu(const struct datapath *dp, int port_no)
20662306a36Sopenharmony_ci{
20762306a36Sopenharmony_ci	WARN_ON_ONCE(!rcu_read_lock_held());
20862306a36Sopenharmony_ci	return ovs_lookup_vport(dp, port_no);
20962306a36Sopenharmony_ci}
21062306a36Sopenharmony_ci
21162306a36Sopenharmony_cistatic inline struct vport *ovs_vport_ovsl_rcu(const struct datapath *dp, int port_no)
21262306a36Sopenharmony_ci{
21362306a36Sopenharmony_ci	WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_ovsl_is_held());
21462306a36Sopenharmony_ci	return ovs_lookup_vport(dp, port_no);
21562306a36Sopenharmony_ci}
21662306a36Sopenharmony_ci
21762306a36Sopenharmony_cistatic inline struct vport *ovs_vport_ovsl(const struct datapath *dp, int port_no)
21862306a36Sopenharmony_ci{
21962306a36Sopenharmony_ci	ASSERT_OVSL();
22062306a36Sopenharmony_ci	return ovs_lookup_vport(dp, port_no);
22162306a36Sopenharmony_ci}
22262306a36Sopenharmony_ci
22362306a36Sopenharmony_ci/* Must be called with rcu_read_lock. */
22462306a36Sopenharmony_cistatic inline struct datapath *get_dp_rcu(struct net *net, int dp_ifindex)
22562306a36Sopenharmony_ci{
22662306a36Sopenharmony_ci	struct net_device *dev = dev_get_by_index_rcu(net, dp_ifindex);
22762306a36Sopenharmony_ci
22862306a36Sopenharmony_ci	if (dev) {
22962306a36Sopenharmony_ci		struct vport *vport = ovs_internal_dev_get_vport(dev);
23062306a36Sopenharmony_ci
23162306a36Sopenharmony_ci		if (vport)
23262306a36Sopenharmony_ci			return vport->dp;
23362306a36Sopenharmony_ci	}
23462306a36Sopenharmony_ci
23562306a36Sopenharmony_ci	return NULL;
23662306a36Sopenharmony_ci}
23762306a36Sopenharmony_ci
23862306a36Sopenharmony_ci/* The caller must hold either ovs_mutex or rcu_read_lock to keep the
23962306a36Sopenharmony_ci * returned dp pointer valid.
24062306a36Sopenharmony_ci */
24162306a36Sopenharmony_cistatic inline struct datapath *get_dp(struct net *net, int dp_ifindex)
24262306a36Sopenharmony_ci{
24362306a36Sopenharmony_ci	struct datapath *dp;
24462306a36Sopenharmony_ci
24562306a36Sopenharmony_ci	WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_ovsl_is_held());
24662306a36Sopenharmony_ci	rcu_read_lock();
24762306a36Sopenharmony_ci	dp = get_dp_rcu(net, dp_ifindex);
24862306a36Sopenharmony_ci	rcu_read_unlock();
24962306a36Sopenharmony_ci
25062306a36Sopenharmony_ci	return dp;
25162306a36Sopenharmony_ci}
25262306a36Sopenharmony_ci
25362306a36Sopenharmony_ciextern struct notifier_block ovs_dp_device_notifier;
25462306a36Sopenharmony_ciextern struct genl_family dp_vport_genl_family;
25562306a36Sopenharmony_ci
25662306a36Sopenharmony_civoid ovs_dp_process_packet(struct sk_buff *skb, struct sw_flow_key *key);
25762306a36Sopenharmony_civoid ovs_dp_detach_port(struct vport *);
25862306a36Sopenharmony_ciint ovs_dp_upcall(struct datapath *, struct sk_buff *,
25962306a36Sopenharmony_ci		  const struct sw_flow_key *, const struct dp_upcall_info *,
26062306a36Sopenharmony_ci		  uint32_t cutlen);
26162306a36Sopenharmony_ci
26262306a36Sopenharmony_ciu32 ovs_dp_get_upcall_portid(const struct datapath *dp, uint32_t cpu_id);
26362306a36Sopenharmony_ci
26462306a36Sopenharmony_ciconst char *ovs_dp_name(const struct datapath *dp);
26562306a36Sopenharmony_cistruct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, struct net *net,
26662306a36Sopenharmony_ci					 u32 portid, u32 seq, u8 cmd);
26762306a36Sopenharmony_ci
26862306a36Sopenharmony_ciint ovs_execute_actions(struct datapath *dp, struct sk_buff *skb,
26962306a36Sopenharmony_ci			const struct sw_flow_actions *, struct sw_flow_key *);
27062306a36Sopenharmony_ci
27162306a36Sopenharmony_civoid ovs_dp_notify_wq(struct work_struct *work);
27262306a36Sopenharmony_ci
27362306a36Sopenharmony_ciint action_fifos_init(void);
27462306a36Sopenharmony_civoid action_fifos_exit(void);
27562306a36Sopenharmony_ci
27662306a36Sopenharmony_ci/* 'KEY' must not have any bits set outside of the 'MASK' */
27762306a36Sopenharmony_ci#define OVS_MASKED(OLD, KEY, MASK) ((KEY) | ((OLD) & ~(MASK)))
27862306a36Sopenharmony_ci#define OVS_SET_MASKED(OLD, KEY, MASK) ((OLD) = OVS_MASKED(OLD, KEY, MASK))
27962306a36Sopenharmony_ci
28062306a36Sopenharmony_ci#define OVS_NLERR(logging_allowed, fmt, ...)			\
28162306a36Sopenharmony_cido {								\
28262306a36Sopenharmony_ci	if (logging_allowed && net_ratelimit())			\
28362306a36Sopenharmony_ci		pr_info("netlink: " fmt "\n", ##__VA_ARGS__);	\
28462306a36Sopenharmony_ci} while (0)
28562306a36Sopenharmony_ci#endif /* datapath.h */
286