File Index
Files and directories
Sign in
FILE

datapath.h

File preview with syntax highlighting.

linux-6.19.14/net/openvswitch/datapath.h C
  1/* SPDX-License-Identifier: GPL-2.0-only */
  2/*
  3 * Copyright (c) 2007-2014 Nicira, Inc.
  4 */
  5
  6#ifndef DATAPATH_H
  7#define DATAPATH_H 1
  8
  9#include <asm/page.h>
 10#include <linux/kernel.h>
 11#include <linux/mutex.h>
 12#include <linux/netdevice.h>
 13#include <linux/skbuff.h>
 14#include <linux/u64_stats_sync.h>
 15#include <net/ip_tunnels.h>
 16#include <net/mpls.h>
 17
 18#include "conntrack.h"
 19#include "flow.h"
 20#include "flow_table.h"
 21#include "meter.h"
 22#include "vport-internal_dev.h"
 23
 24#define DP_MAX_PORTS                USHRT_MAX
 25#define DP_VPORT_HASH_BUCKETS       1024
 26#define DP_MASKS_REBALANCE_INTERVAL 4000
 27
 28/**
 29 * struct dp_stats_percpu - per-cpu packet processing statistics for a given
 30 * datapath.
 31 * @n_hit: Number of received packets for which a matching flow was found in
 32 * the flow table.
 33 * @n_missed: Number of received packets that had no matching flow in the flow
 34 * table.  The sum of @n_hit and @n_missed is the number of packets that have
 35 * been received by the datapath.
 36 * @n_lost: Number of received packets that had no matching flow in the flow
 37 * table that could not be sent to userspace (normally due to an overflow in
 38 * one of the datapath's queues).
 39 * @n_mask_hit: Number of masks looked up for flow match.
 40 *   @n_mask_hit / (@n_hit + @n_missed)  will be the average masks looked
 41 *   up per packet.
 42 * @n_cache_hit: The number of received packets that had their mask found using
 43 * the mask cache.
 44 * @syncp: Synchronization point for 64bit counters.
 45 */
 46struct dp_stats_percpu {
 47	u64 n_hit;
 48	u64 n_missed;
 49	u64 n_lost;
 50	u64 n_mask_hit;
 51	u64 n_cache_hit;
 52	struct u64_stats_sync syncp;
 53};
 54
 55/**
 56 * struct dp_nlsk_pids - array of netlink portids of for a datapath.
 57 *                       This is used when OVS_DP_F_DISPATCH_UPCALL_PER_CPU
 58 *                       is enabled and must be protected by rcu.
 59 * @rcu: RCU callback head for deferred destruction.
 60 * @n_pids: Size of @pids array.
 61 * @pids: Array storing the Netlink socket PIDs indexed by CPU ID for packets
 62 *       that miss the flow table.
 63 */
 64struct dp_nlsk_pids {
 65	struct rcu_head rcu;
 66	u32 n_pids;
 67	u32 pids[];
 68};
 69
 70/**
 71 * struct datapath - datapath for flow-based packet switching
 72 * @rcu: RCU callback head for deferred destruction.
 73 * @list_node: Element in global 'dps' list.
 74 * @table: flow table.
 75 * @ports: Hash table for ports.  %OVSP_LOCAL port always exists.  Protected by
 76 * ovs_mutex and RCU.
 77 * @stats_percpu: Per-CPU datapath statistics.
 78 * @net: Reference to net namespace.
 79 * @user_features: Bitmap of enabled %OVS_DP_F_* features.
 80 * @max_headroom: The maximum headroom of all vports in this datapath; it will
 81 * be used by all the internal vports in this dp.
 82 * @meter_tbl: Meter table.
 83 * @upcall_portids: RCU protected 'struct dp_nlsk_pids'.
 84 *
 85 * Context: See the comment on locking at the top of datapath.c for additional
 86 * locking information.
 87 */
 88struct datapath {
 89	struct rcu_head rcu;
 90	struct list_head list_node;
 91
 92	/* Flow table. */
 93	struct flow_table table;
 94
 95	/* Switch ports. */
 96	struct hlist_head *ports;
 97
 98	/* Stats. */
 99	struct dp_stats_percpu __percpu *stats_percpu;
100
101	/* Network namespace ref. */
102	possible_net_t net;
103
104	u32 user_features;
105
106	u32 max_headroom;
107
108	/* Switch meters. */
109	struct dp_meter_table meter_tbl;
110
111	struct dp_nlsk_pids __rcu *upcall_portids;
112};
113
114/**
115 * struct ovs_skb_cb - OVS data in skb CB
116 * @input_vport: The original vport packet came in on. This value is cached
117 * when a packet is received by OVS.
118 * @mru: The maximum received fragement size; 0 if the packet is not
119 * fragmented.
120 * @acts_origlen: The netlink size of the flow actions applied to this skb.
121 * @cutlen: The number of bytes from the packet end to be removed.
122 * @probability: The sampling probability that was applied to this skb; 0 means
123 * no sampling has occurred; U32_MAX means 100% probability.
124 * @upcall_pid: Netlink socket PID to use for sending this packet to userspace;
125 * 0 means "not set" and default per-CPU or per-vport dispatch should be used.
126 */
127struct ovs_skb_cb {
128	struct vport		*input_vport;
129	u16			mru;
130	u16			acts_origlen;
131	u32			cutlen;
132	u32			probability;
133	u32			upcall_pid;
134};
135#define OVS_CB(skb) ((struct ovs_skb_cb *)(skb)->cb)
136
137/**
138 * struct dp_upcall_info - metadata to include with a packet sent to userspace
139 * @cmd: One of %OVS_PACKET_CMD_*.
140 * @userdata: If nonnull, its variable-length value is passed to userspace as
141 * %OVS_PACKET_ATTR_USERDATA.
142 * @actions: If nonnull, its variable-length value is passed to userspace as
143 * %OVS_PACKET_ATTR_ACTIONS.
144 * @actions_len: The length of the @actions.
145 * @portid: Netlink portid to which packet should be sent.  If @portid is 0
146 * then no packet is sent and the packet is accounted in the datapath's @n_lost
147 * counter.
148 * @egress_tun_info: If nonnull, becomes %OVS_PACKET_ATTR_EGRESS_TUN_KEY.
149 * @mru: If not zero, Maximum received IP fragment size.
150 */
151struct dp_upcall_info {
152	struct ip_tunnel_info *egress_tun_info;
153	const struct nlattr *userdata;
154	const struct nlattr *actions;
155	int actions_len;
156	u32 portid;
157	u8 cmd;
158	u16 mru;
159};
160
161/**
162 * struct ovs_net - Per net-namespace data for ovs.
163 * @dps: List of datapaths to enable dumping them all out.
164 * Protected by genl_mutex.
165 * @dp_notify_work: A work notifier to handle port unregistering.
166 * @masks_rebalance: A work to periodically optimize flow table caches.
167 * @ct_limit_info: A hash table of conntrack zone connection limits.
168 * @xt_label: Whether connlables are configured for the network or not.
169 */
170struct ovs_net {
171	struct list_head dps;
172	struct work_struct dp_notify_work;
173	struct delayed_work masks_rebalance;
174#if	IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
175	struct ovs_ct_limit_info *ct_limit_info;
176#endif
177	bool xt_label;
178};
179
180#define MAX_L2_LEN	(VLAN_ETH_HLEN + 3 * MPLS_HLEN)
181struct ovs_frag_data {
182	unsigned long dst;
183	struct vport *vport;
184	struct ovs_skb_cb cb;
185	__be16 inner_protocol;
186	u16 network_offset;	/* valid only for MPLS */
187	u16 vlan_tci;
188	__be16 vlan_proto;
189	unsigned int l2_len;
190	u8 mac_proto;
191	u8 l2_data[MAX_L2_LEN];
192};
193
194struct deferred_action {
195	struct sk_buff *skb;
196	const struct nlattr *actions;
197	int actions_len;
198
199	/* Store pkt_key clone when creating deferred action. */
200	struct sw_flow_key pkt_key;
201};
202
203#define DEFERRED_ACTION_FIFO_SIZE 10
204#define OVS_RECURSION_LIMIT 5
205#define OVS_DEFERRED_ACTION_THRESHOLD (OVS_RECURSION_LIMIT - 2)
206
207struct action_fifo {
208	int head;
209	int tail;
210	/* Deferred action fifo queue storage. */
211	struct deferred_action fifo[DEFERRED_ACTION_FIFO_SIZE];
212};
213
214struct action_flow_keys {
215	struct sw_flow_key key[OVS_DEFERRED_ACTION_THRESHOLD];
216};
217
218struct ovs_pcpu_storage {
219	struct action_fifo action_fifos;
220	struct action_flow_keys flow_keys;
221	struct ovs_frag_data frag_data;
222	int exec_level;
223	struct task_struct *owner;
224	local_lock_t bh_lock;
225};
226
227extern struct ovs_pcpu_storage __percpu *ovs_pcpu_storage;
228
229/**
230 * enum ovs_pkt_hash_types - hash info to include with a packet
231 * to send to userspace.
232 * @OVS_PACKET_HASH_SW_BIT: indicates hash was computed in software stack.
233 * @OVS_PACKET_HASH_L4_BIT: indicates hash is a canonical 4-tuple hash
234 * over transport ports.
235 */
236enum ovs_pkt_hash_types {
237	OVS_PACKET_HASH_SW_BIT = (1ULL << 32),
238	OVS_PACKET_HASH_L4_BIT = (1ULL << 33),
239};
240
241extern unsigned int ovs_net_id;
242void ovs_lock(void);
243void ovs_unlock(void);
244
245#ifdef CONFIG_LOCKDEP
246int lockdep_ovsl_is_held(void);
247#else
248#define lockdep_ovsl_is_held()	1
249#endif
250
251#define ASSERT_OVSL()		WARN_ON(!lockdep_ovsl_is_held())
252#define ovsl_dereference(p)					\
253	rcu_dereference_protected(p, lockdep_ovsl_is_held())
254#define rcu_dereference_ovsl(p)					\
255	rcu_dereference_check(p, lockdep_ovsl_is_held())
256
257static inline struct net *ovs_dp_get_net(const struct datapath *dp)
258{
259	return read_pnet(&dp->net);
260}
261
262static inline void ovs_dp_set_net(struct datapath *dp, struct net *net)
263{
264	write_pnet(&dp->net, net);
265}
266
267struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no);
268
269static inline struct vport *ovs_vport_rcu(const struct datapath *dp, int port_no)
270{
271	WARN_ON_ONCE(!rcu_read_lock_held());
272	return ovs_lookup_vport(dp, port_no);
273}
274
275static inline struct vport *ovs_vport_ovsl_rcu(const struct datapath *dp, int port_no)
276{
277	WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_ovsl_is_held());
278	return ovs_lookup_vport(dp, port_no);
279}
280
281static inline struct vport *ovs_vport_ovsl(const struct datapath *dp, int port_no)
282{
283	ASSERT_OVSL();
284	return ovs_lookup_vport(dp, port_no);
285}
286
287/* Must be called with rcu_read_lock. */
288static inline struct datapath *get_dp_rcu(struct net *net, int dp_ifindex)
289{
290	struct net_device *dev = dev_get_by_index_rcu(net, dp_ifindex);
291
292	if (dev) {
293		struct vport *vport = ovs_internal_dev_get_vport(dev);
294
295		if (vport)
296			return vport->dp;
297	}
298
299	return NULL;
300}
301
302/* The caller must hold either ovs_mutex or rcu_read_lock to keep the
303 * returned dp pointer valid.
304 */
305static inline struct datapath *get_dp(struct net *net, int dp_ifindex)
306{
307	struct datapath *dp;
308
309	WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_ovsl_is_held());
310	rcu_read_lock();
311	dp = get_dp_rcu(net, dp_ifindex);
312	rcu_read_unlock();
313
314	return dp;
315}
316
317extern struct notifier_block ovs_dp_device_notifier;
318extern struct genl_family dp_vport_genl_family;
319
320void ovs_dp_process_packet(struct sk_buff *skb, struct sw_flow_key *key);
321void ovs_dp_detach_port(struct vport *);
322int ovs_dp_upcall(struct datapath *, struct sk_buff *,
323		  const struct sw_flow_key *, const struct dp_upcall_info *,
324		  uint32_t cutlen);
325
326u32 ovs_dp_get_upcall_portid(const struct datapath *dp, uint32_t cpu_id);
327
328const char *ovs_dp_name(const struct datapath *dp);
329struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, struct net *net,
330					 u32 portid, u32 seq, u8 cmd);
331
332int ovs_execute_actions(struct datapath *dp, struct sk_buff *skb,
333			const struct sw_flow_actions *, struct sw_flow_key *);
334
335void ovs_dp_notify_wq(struct work_struct *work);
336
337/* 'KEY' must not have any bits set outside of the 'MASK' */
338#define OVS_MASKED(OLD, KEY, MASK) ((KEY) | ((OLD) & ~(MASK)))
339#define OVS_SET_MASKED(OLD, KEY, MASK) ((OLD) = OVS_MASKED(OLD, KEY, MASK))
340
341#define OVS_NLERR(logging_allowed, fmt, ...)			\
342do {								\
343	if (logging_allowed && net_ratelimit())			\
344		pr_info("netlink: " fmt "\n", ##__VA_ARGS__);	\
345} while (0)
346#endif /* datapath.h */
~/ Read-only file preview.