1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Copyright (c) 2007-2014 Nicira, Inc.
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>
18 #include "conntrack.h"
20 #include "flow_table.h"
22 #include "vport-internal_dev.h"
24 #define DP_MAX_PORTS USHRT_MAX
25 #define DP_VPORT_HASH_BUCKETS 1024
26 #define DP_MASKS_REBALANCE_INTERVAL 4000
29 * struct dp_stats_percpu - per-cpu packet processing statistics for a given
31 * @n_hit: Number of received packets for which a matching flow was found in
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
42 * @n_cache_hit: The number of received packets that had their mask found using
44 * @syncp: Synchronization point for 64bit counters.
46 struct dp_stats_percpu {
52 struct u64_stats_sync syncp;
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.
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.
75 * @ports: Hash table for ports. %OVSP_LOCAL port always exists. Protected by
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'.
85 * Context: See the comment on locking at the top of datapath.c for additional
86 * locking information.
90 struct list_head list_node;
93 struct flow_table table;
96 struct hlist_head *ports;
99 struct dp_stats_percpu __percpu *stats_percpu;
101 /* Network namespace ref. */
109 struct dp_meter_table meter_tbl;
111 struct dp_nlsk_pids __rcu *upcall_portids;
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
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.
126 struct vport *input_vport;
132 #define OVS_CB(skb) ((struct ovs_skb_cb *)(skb)->cb)
135 * struct dp_upcall_info - metadata to include with a packet sent to userspace
136 * @cmd: One of %OVS_PACKET_CMD_*.
137 * @userdata: If nonnull, its variable-length value is passed to userspace as
138 * %OVS_PACKET_ATTR_USERDATA.
139 * @actions: If nonnull, its variable-length value is passed to userspace as
140 * %OVS_PACKET_ATTR_ACTIONS.
141 * @actions_len: The length of the @actions.
142 * @portid: Netlink portid to which packet should be sent. If @portid is 0
143 * then no packet is sent and the packet is accounted in the datapath's @n_lost
145 * @egress_tun_info: If nonnull, becomes %OVS_PACKET_ATTR_EGRESS_TUN_KEY.
146 * @mru: If not zero, Maximum received IP fragment size.
148 struct dp_upcall_info {
149 struct ip_tunnel_info *egress_tun_info;
150 const struct nlattr *userdata;
151 const struct nlattr *actions;
159 * struct ovs_net - Per net-namespace data for ovs.
160 * @dps: List of datapaths to enable dumping them all out.
161 * Protected by genl_mutex.
162 * @dp_notify_work: A work notifier to handle port unregistering.
163 * @masks_rebalance: A work to periodically optimize flow table caches.
164 * @ct_limit_info: A hash table of conntrack zone connection limits.
165 * @xt_label: Whether connlables are configured for the network or not.
168 struct list_head dps;
169 struct work_struct dp_notify_work;
170 struct delayed_work masks_rebalance;
171 #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
172 struct ovs_ct_limit_info *ct_limit_info;
177 #define MAX_L2_LEN (VLAN_ETH_HLEN + 3 * MPLS_HLEN)
178 struct ovs_frag_data {
181 struct ovs_skb_cb cb;
182 __be16 inner_protocol;
183 u16 network_offset; /* valid only for MPLS */
188 u8 l2_data[MAX_L2_LEN];
191 struct deferred_action {
193 const struct nlattr *actions;
196 /* Store pkt_key clone when creating deferred action. */
197 struct sw_flow_key pkt_key;
200 #define DEFERRED_ACTION_FIFO_SIZE 10
201 #define OVS_RECURSION_LIMIT 5
202 #define OVS_DEFERRED_ACTION_THRESHOLD (OVS_RECURSION_LIMIT - 2)
207 /* Deferred action fifo queue storage. */
208 struct deferred_action fifo[DEFERRED_ACTION_FIFO_SIZE];
211 struct action_flow_keys {
212 struct sw_flow_key key[OVS_DEFERRED_ACTION_THRESHOLD];
215 struct ovs_pcpu_storage {
216 struct action_fifo action_fifos;
217 struct action_flow_keys flow_keys;
218 struct ovs_frag_data frag_data;
220 struct task_struct *owner;
221 local_lock_t bh_lock;
224 extern struct ovs_pcpu_storage __percpu *ovs_pcpu_storage;
227 * enum ovs_pkt_hash_types - hash info to include with a packet
228 * to send to userspace.
229 * @OVS_PACKET_HASH_SW_BIT: indicates hash was computed in software stack.
230 * @OVS_PACKET_HASH_L4_BIT: indicates hash is a canonical 4-tuple hash
231 * over transport ports.
233 enum ovs_pkt_hash_types {
234 OVS_PACKET_HASH_SW_BIT = (1ULL << 32),
235 OVS_PACKET_HASH_L4_BIT = (1ULL << 33),
238 extern unsigned int ovs_net_id;
240 void ovs_unlock(void);
242 #ifdef CONFIG_LOCKDEP
243 int lockdep_ovsl_is_held(void);
245 #define lockdep_ovsl_is_held() 1
248 #define ASSERT_OVSL() WARN_ON(!lockdep_ovsl_is_held())
249 #define ovsl_dereference(p) \
250 rcu_dereference_protected(p, lockdep_ovsl_is_held())
251 #define rcu_dereference_ovsl(p) \
252 rcu_dereference_check(p, lockdep_ovsl_is_held())
254 static inline struct net *ovs_dp_get_net(const struct datapath *dp)
256 return read_pnet(&dp->net);
259 static inline void ovs_dp_set_net(struct datapath *dp, struct net *net)
261 write_pnet(&dp->net, net);
264 struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no);
266 static inline struct vport *ovs_vport_rcu(const struct datapath *dp, int port_no)
268 WARN_ON_ONCE(!rcu_read_lock_held());
269 return ovs_lookup_vport(dp, port_no);
272 static inline struct vport *ovs_vport_ovsl_rcu(const struct datapath *dp, int port_no)
274 WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_ovsl_is_held());
275 return ovs_lookup_vport(dp, port_no);
278 static inline struct vport *ovs_vport_ovsl(const struct datapath *dp, int port_no)
281 return ovs_lookup_vport(dp, port_no);
284 /* Must be called with rcu_read_lock. */
285 static inline struct datapath *get_dp_rcu(struct net *net, int dp_ifindex)
287 struct net_device *dev = dev_get_by_index_rcu(net, dp_ifindex);
290 struct vport *vport = ovs_internal_dev_get_vport(dev);
299 /* The caller must hold either ovs_mutex or rcu_read_lock to keep the
300 * returned dp pointer valid.
302 static inline struct datapath *get_dp(struct net *net, int dp_ifindex)
306 WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_ovsl_is_held());
308 dp = get_dp_rcu(net, dp_ifindex);
314 extern struct notifier_block ovs_dp_device_notifier;
315 extern struct genl_family dp_vport_genl_family;
317 void ovs_dp_process_packet(struct sk_buff *skb, struct sw_flow_key *key);
318 void ovs_dp_detach_port(struct vport *);
319 int ovs_dp_upcall(struct datapath *, struct sk_buff *,
320 const struct sw_flow_key *, const struct dp_upcall_info *,
323 u32 ovs_dp_get_upcall_portid(const struct datapath *dp, uint32_t cpu_id);
325 const char *ovs_dp_name(const struct datapath *dp);
326 struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, struct net *net,
327 u32 portid, u32 seq, u8 cmd);
329 int ovs_execute_actions(struct datapath *dp, struct sk_buff *skb,
330 const struct sw_flow_actions *, struct sw_flow_key *);
332 void ovs_dp_notify_wq(struct work_struct *work);
334 /* 'KEY' must not have any bits set outside of the 'MASK' */
335 #define OVS_MASKED(OLD, KEY, MASK) ((KEY) | ((OLD) & ~(MASK)))
336 #define OVS_SET_MASKED(OLD, KEY, MASK) ((OLD) = OVS_MASKED(OLD, KEY, MASK))
338 #define OVS_NLERR(logging_allowed, fmt, ...) \
340 if (logging_allowed && net_ratelimit()) \
341 pr_info("netlink: " fmt "\n", ##__VA_ARGS__); \
343 #endif /* datapath.h */