]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - net/openvswitch/datapath.h
net: openvswitch: fixes potential deadlock in dp cleanup code
[mirror_ubuntu-jammy-kernel.git] / net / openvswitch / datapath.h
CommitLineData
c9422999 1/* SPDX-License-Identifier: GPL-2.0-only */
ccb1352e 2/*
971427f3 3 * Copyright (c) 2007-2014 Nicira, Inc.
ccb1352e
JG
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>
1d8fff90 15#include <net/ip_tunnels.h>
ccb1352e 16
7f8a436e 17#include "conntrack.h"
ccb1352e 18#include "flow.h"
e6445719 19#include "flow_table.h"
cd8a6c33 20#include "meter.h"
9602c01e 21#include "vport-internal_dev.h"
ccb1352e 22
eac87c41
EC
23#define DP_MAX_PORTS USHRT_MAX
24#define DP_VPORT_HASH_BUCKETS 1024
25#define DP_MASKS_REBALANCE_INTERVAL 4000
15eac2a7 26
ccb1352e
JG
27/**
28 * struct dp_stats_percpu - per-cpu packet processing statistics for a given
29 * datapath.
30 * @n_hit: Number of received packets for which a matching flow was found in
31 * the flow table.
32 * @n_miss: Number of received packets that had no matching flow in the flow
33 * table. The sum of @n_hit and @n_miss is the number of packets that have
34 * been received by the datapath.
35 * @n_lost: Number of received packets that had no matching flow in the flow
36 * table that could not be sent to userspace (normally due to an overflow in
37 * one of the datapath's queues).
1bd7116f
AZ
38 * @n_mask_hit: Number of masks looked up for flow match.
39 * @n_mask_hit / (@n_hit + @n_missed) will be the average masks looked
40 * up per packet.
ccb1352e
JG
41 */
42struct dp_stats_percpu {
43 u64 n_hit;
44 u64 n_missed;
45 u64 n_lost;
1bd7116f 46 u64 n_mask_hit;
df9d9fdf 47 struct u64_stats_sync syncp;
ccb1352e
JG
48};
49
50/**
51 * struct datapath - datapath for flow-based packet switching
52 * @rcu: RCU callback head for deferred destruction.
53 * @list_node: Element in global 'dps' list.
b637e498 54 * @table: flow table.
15eac2a7 55 * @ports: Hash table for ports. %OVSP_LOCAL port always exists. Protected by
8e4e1713 56 * ovs_mutex and RCU.
ccb1352e 57 * @stats_percpu: Per-CPU datapath statistics.
46df7b81 58 * @net: Reference to net namespace.
3a927bc7
PA
59 * @max_headroom: the maximum headroom of all vports in this datapath; it will
60 * be used by all the internal vports in this dp.
ccb1352e
JG
61 *
62 * Context: See the comment on locking at the top of datapath.c for additional
63 * locking information.
64 */
65struct datapath {
66 struct rcu_head rcu;
67 struct list_head list_node;
68
69 /* Flow table. */
b637e498 70 struct flow_table table;
ccb1352e
JG
71
72 /* Switch ports. */
15eac2a7 73 struct hlist_head *ports;
ccb1352e
JG
74
75 /* Stats. */
76 struct dp_stats_percpu __percpu *stats_percpu;
46df7b81 77
46df7b81 78 /* Network namespace ref. */
0c5c9fb5 79 possible_net_t net;
43d4be9c
TG
80
81 u32 user_features;
3a927bc7
PA
82
83 u32 max_headroom;
96fbc13d
AZ
84
85 /* Switch meters. */
c7c4c44c 86 struct dp_meter_table meter_tbl;
ccb1352e
JG
87};
88
89/**
90 * struct ovs_skb_cb - OVS data in skb CB
83c8df26
PS
91 * @input_vport: The original vport packet came in on. This value is cached
92 * when a packet is received by OVS.
7f8a436e
JS
93 * @mru: The maximum received fragement size; 0 if the packet is not
94 * fragmented.
494bea39 95 * @acts_origlen: The netlink size of the flow actions applied to this skb.
52427fa0 96 * @cutlen: The number of bytes from the packet end to be removed.
ccb1352e
JG
97 */
98struct ovs_skb_cb {
83c8df26 99 struct vport *input_vport;
7f8a436e 100 u16 mru;
494bea39 101 u16 acts_origlen;
f2a4d086 102 u32 cutlen;
ccb1352e
JG
103};
104#define OVS_CB(skb) ((struct ovs_skb_cb *)(skb)->cb)
105
106/**
107 * struct dp_upcall - metadata to include with a packet to send to userspace
108 * @cmd: One of %OVS_PACKET_CMD_*.
4490108b 109 * @userdata: If nonnull, its variable-length value is passed to userspace as
ccb1352e 110 * %OVS_PACKET_ATTR_USERDATA.
e8eedb85
PS
111 * @portid: Netlink portid to which packet should be sent. If @portid is 0
112 * then no packet is sent and the packet is accounted in the datapath's @n_lost
ccb1352e 113 * counter.
8f0aad6f 114 * @egress_tun_info: If nonnull, becomes %OVS_PACKET_ATTR_EGRESS_TUN_KEY.
7f8a436e 115 * @mru: If not zero, Maximum received IP fragment size.
ccb1352e
JG
116 */
117struct dp_upcall_info {
4c222798 118 struct ip_tunnel_info *egress_tun_info;
ccb1352e 119 const struct nlattr *userdata;
ccea7445
NM
120 const struct nlattr *actions;
121 int actions_len;
15e47304 122 u32 portid;
e8eedb85 123 u8 cmd;
7f8a436e 124 u16 mru;
ccb1352e
JG
125};
126
8e4e1713
PS
127/**
128 * struct ovs_net - Per net-namespace data for ovs.
129 * @dps: List of datapaths to enable dumping them all out.
130 * Protected by genl_mutex.
131 */
132struct ovs_net {
133 struct list_head dps;
134 struct work_struct dp_notify_work;
a65878d6 135 struct delayed_work masks_rebalance;
11efd5cb
YHW
136#if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
137 struct ovs_ct_limit_info *ct_limit_info;
138#endif
c2ac6673
JS
139
140 /* Module reference for configuring conntrack. */
141 bool xt_label;
bd1903b7
TZ
142};
143
144/**
145 * enum ovs_pkt_hash_types - hash info to include with a packet
146 * to send to userspace.
147 * @OVS_PACKET_HASH_SW_BIT: indicates hash was computed in software stack.
148 * @OVS_PACKET_HASH_L4_BIT: indicates hash is a canonical 4-tuple hash
149 * over transport ports.
150 */
151enum ovs_pkt_hash_types {
152 OVS_PACKET_HASH_SW_BIT = (1ULL << 32),
153 OVS_PACKET_HASH_L4_BIT = (1ULL << 33),
8e4e1713
PS
154};
155
c7d03a00 156extern unsigned int ovs_net_id;
8e4e1713
PS
157void ovs_lock(void);
158void ovs_unlock(void);
159
160#ifdef CONFIG_LOCKDEP
161int lockdep_ovsl_is_held(void);
162#else
163#define lockdep_ovsl_is_held() 1
164#endif
165
80019d31 166#define ASSERT_OVSL() WARN_ON(!lockdep_ovsl_is_held())
8e4e1713
PS
167#define ovsl_dereference(p) \
168 rcu_dereference_protected(p, lockdep_ovsl_is_held())
663efa36
JG
169#define rcu_dereference_ovsl(p) \
170 rcu_dereference_check(p, lockdep_ovsl_is_held())
8e4e1713 171
12eb18f7 172static inline struct net *ovs_dp_get_net(const struct datapath *dp)
46df7b81
PS
173{
174 return read_pnet(&dp->net);
175}
176
177static inline void ovs_dp_set_net(struct datapath *dp, struct net *net)
178{
179 write_pnet(&dp->net, net);
180}
181
8e4e1713
PS
182struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no);
183
184static inline struct vport *ovs_vport_rcu(const struct datapath *dp, int port_no)
185{
186 WARN_ON_ONCE(!rcu_read_lock_held());
187 return ovs_lookup_vport(dp, port_no);
188}
189
190static inline struct vport *ovs_vport_ovsl_rcu(const struct datapath *dp, int port_no)
191{
192 WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_ovsl_is_held());
193 return ovs_lookup_vport(dp, port_no);
194}
195
196static inline struct vport *ovs_vport_ovsl(const struct datapath *dp, int port_no)
197{
198 ASSERT_OVSL();
199 return ovs_lookup_vport(dp, port_no);
200}
201
9602c01e
AZ
202/* Must be called with rcu_read_lock. */
203static inline struct datapath *get_dp_rcu(struct net *net, int dp_ifindex)
204{
205 struct net_device *dev = dev_get_by_index_rcu(net, dp_ifindex);
206
207 if (dev) {
208 struct vport *vport = ovs_internal_dev_get_vport(dev);
209
210 if (vport)
211 return vport->dp;
212 }
213
214 return NULL;
215}
216
217/* The caller must hold either ovs_mutex or rcu_read_lock to keep the
218 * returned dp pointer valid.
219 */
220static inline struct datapath *get_dp(struct net *net, int dp_ifindex)
221{
222 struct datapath *dp;
223
224 WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_ovsl_is_held());
225 rcu_read_lock();
226 dp = get_dp_rcu(net, dp_ifindex);
227 rcu_read_unlock();
228
229 return dp;
230}
231
ccb1352e 232extern struct notifier_block ovs_dp_device_notifier;
68eb5503 233extern struct genl_family dp_vport_genl_family;
ccb1352e 234
95a7233c
PB
235DECLARE_STATIC_KEY_FALSE(tc_recirc_sharing_support);
236
8c8b1b83 237void ovs_dp_process_packet(struct sk_buff *skb, struct sw_flow_key *key);
ccb1352e
JG
238void ovs_dp_detach_port(struct vport *);
239int ovs_dp_upcall(struct datapath *, struct sk_buff *,
f2a4d086
WT
240 const struct sw_flow_key *, const struct dp_upcall_info *,
241 uint32_t cutlen);
ccb1352e 242
971427f3 243const char *ovs_dp_name(const struct datapath *dp);
9354d452
JB
244struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, struct net *net,
245 u32 portid, u32 seq, u8 cmd);
ccb1352e 246
2ff3e4e4 247int ovs_execute_actions(struct datapath *dp, struct sk_buff *skb,
12eb18f7 248 const struct sw_flow_actions *, struct sw_flow_key *);
971427f3 249
8e4e1713 250void ovs_dp_notify_wq(struct work_struct *work);
03f0d916 251
971427f3
AZ
252int action_fifos_init(void);
253void action_fifos_exit(void);
254
be26b9a8
JS
255/* 'KEY' must not have any bits set outside of the 'MASK' */
256#define OVS_MASKED(OLD, KEY, MASK) ((KEY) | ((OLD) & ~(MASK)))
257#define OVS_SET_MASKED(OLD, KEY, MASK) ((OLD) = OVS_MASKED(OLD, KEY, MASK))
258
05da5898 259#define OVS_NLERR(logging_allowed, fmt, ...) \
1815a883 260do { \
05da5898
JR
261 if (logging_allowed && net_ratelimit()) \
262 pr_info("netlink: " fmt "\n", ##__VA_ARGS__); \
1815a883 263} while (0)
ccb1352e 264#endif /* datapath.h */