Commit | Line | Data |
---|---|---|
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> |
3af4cdd6 | 16 | #include <net/mpls.h> |
ccb1352e | 17 | |
7f8a436e | 18 | #include "conntrack.h" |
ccb1352e | 19 | #include "flow.h" |
e6445719 | 20 | #include "flow_table.h" |
cd8a6c33 | 21 | #include "meter.h" |
9602c01e | 22 | #include "vport-internal_dev.h" |
ccb1352e | 23 | |
eac87c41 EC |
24 | #define DP_MAX_PORTS USHRT_MAX |
25 | #define DP_VPORT_HASH_BUCKETS 1024 | |
26 | #define DP_MASKS_REBALANCE_INTERVAL 4000 | |
15eac2a7 | 27 | |
ccb1352e JG |
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. | |
6bb0dcb3 IM |
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 | |
ccb1352e JG |
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). | |
1bd7116f AZ |
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. | |
9d2f627b EC |
42 | * @n_cache_hit: The number of received packets that had their mask found using |
43 | * the mask cache. | |
6bb0dcb3 | 44 | * @syncp: Synchronization point for 64bit counters. |
ccb1352e JG |
45 | */ |
46 | struct dp_stats_percpu { | |
47 | u64 n_hit; | |
48 | u64 n_missed; | |
49 | u64 n_lost; | |
1bd7116f | 50 | u64 n_mask_hit; |
9d2f627b | 51 | u64 n_cache_hit; |
df9d9fdf | 52 | struct u64_stats_sync syncp; |
ccb1352e JG |
53 | }; |
54 | ||
b83d23a2 MG |
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 | */ | |
64 | struct dp_nlsk_pids { | |
65 | struct rcu_head rcu; | |
66 | u32 n_pids; | |
67 | u32 pids[]; | |
68 | }; | |
69 | ||
ccb1352e JG |
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. | |
b637e498 | 74 | * @table: flow table. |
15eac2a7 | 75 | * @ports: Hash table for ports. %OVSP_LOCAL port always exists. Protected by |
8e4e1713 | 76 | * ovs_mutex and RCU. |
ccb1352e | 77 | * @stats_percpu: Per-CPU datapath statistics. |
46df7b81 | 78 | * @net: Reference to net namespace. |
6bb0dcb3 IM |
79 | * @user_features: Bitmap of enabled %OVS_DP_F_* features. |
80 | * @max_headroom: The maximum headroom of all vports in this datapath; it will | |
3a927bc7 | 81 | * be used by all the internal vports in this dp. |
6bb0dcb3 | 82 | * @meter_tbl: Meter table. |
b83d23a2 | 83 | * @upcall_portids: RCU protected 'struct dp_nlsk_pids'. |
ccb1352e JG |
84 | * |
85 | * Context: See the comment on locking at the top of datapath.c for additional | |
86 | * locking information. | |
87 | */ | |
88 | struct datapath { | |
89 | struct rcu_head rcu; | |
90 | struct list_head list_node; | |
91 | ||
92 | /* Flow table. */ | |
b637e498 | 93 | struct flow_table table; |
ccb1352e JG |
94 | |
95 | /* Switch ports. */ | |
15eac2a7 | 96 | struct hlist_head *ports; |
ccb1352e JG |
97 | |
98 | /* Stats. */ | |
99 | struct dp_stats_percpu __percpu *stats_percpu; | |
46df7b81 | 100 | |
46df7b81 | 101 | /* Network namespace ref. */ |
0c5c9fb5 | 102 | possible_net_t net; |
43d4be9c TG |
103 | |
104 | u32 user_features; | |
3a927bc7 PA |
105 | |
106 | u32 max_headroom; | |
96fbc13d AZ |
107 | |
108 | /* Switch meters. */ | |
c7c4c44c | 109 | struct dp_meter_table meter_tbl; |
b83d23a2 MG |
110 | |
111 | struct dp_nlsk_pids __rcu *upcall_portids; | |
ccb1352e JG |
112 | }; |
113 | ||
114 | /** | |
115 | * struct ovs_skb_cb - OVS data in skb CB | |
83c8df26 PS |
116 | * @input_vport: The original vport packet came in on. This value is cached |
117 | * when a packet is received by OVS. | |
7f8a436e JS |
118 | * @mru: The maximum received fragement size; 0 if the packet is not |
119 | * fragmented. | |
494bea39 | 120 | * @acts_origlen: The netlink size of the flow actions applied to this skb. |
52427fa0 | 121 | * @cutlen: The number of bytes from the packet end to be removed. |
71763d8a AM |
122 | * @probability: The sampling probability that was applied to this skb; 0 means |
123 | * no sampling has occurred; U32_MAX means 100% probability. | |
ccb1352e JG |
124 | */ |
125 | struct ovs_skb_cb { | |
83c8df26 | 126 | struct vport *input_vport; |
7f8a436e | 127 | u16 mru; |
494bea39 | 128 | u16 acts_origlen; |
f2a4d086 | 129 | u32 cutlen; |
71763d8a | 130 | u32 probability; |
ccb1352e JG |
131 | }; |
132 | #define OVS_CB(skb) ((struct ovs_skb_cb *)(skb)->cb) | |
133 | ||
134 | /** | |
6bb0dcb3 | 135 | * struct dp_upcall_info - metadata to include with a packet sent to userspace |
ccb1352e | 136 | * @cmd: One of %OVS_PACKET_CMD_*. |
4490108b | 137 | * @userdata: If nonnull, its variable-length value is passed to userspace as |
ccb1352e | 138 | * %OVS_PACKET_ATTR_USERDATA. |
6bb0dcb3 IM |
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. | |
e8eedb85 PS |
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 | |
ccb1352e | 144 | * counter. |
8f0aad6f | 145 | * @egress_tun_info: If nonnull, becomes %OVS_PACKET_ATTR_EGRESS_TUN_KEY. |
7f8a436e | 146 | * @mru: If not zero, Maximum received IP fragment size. |
ccb1352e JG |
147 | */ |
148 | struct dp_upcall_info { | |
4c222798 | 149 | struct ip_tunnel_info *egress_tun_info; |
ccb1352e | 150 | const struct nlattr *userdata; |
ccea7445 NM |
151 | const struct nlattr *actions; |
152 | int actions_len; | |
15e47304 | 153 | u32 portid; |
e8eedb85 | 154 | u8 cmd; |
7f8a436e | 155 | u16 mru; |
ccb1352e JG |
156 | }; |
157 | ||
8e4e1713 PS |
158 | /** |
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. | |
6bb0dcb3 IM |
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. | |
8e4e1713 PS |
166 | */ |
167 | struct ovs_net { | |
168 | struct list_head dps; | |
169 | struct work_struct dp_notify_work; | |
a65878d6 | 170 | struct delayed_work masks_rebalance; |
11efd5cb YHW |
171 | #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT) |
172 | struct ovs_ct_limit_info *ct_limit_info; | |
173 | #endif | |
1063ae07 | 174 | bool xt_label; |
bd1903b7 TZ |
175 | }; |
176 | ||
3af4cdd6 SAS |
177 | #define MAX_L2_LEN (VLAN_ETH_HLEN + 3 * MPLS_HLEN) |
178 | struct ovs_frag_data { | |
179 | unsigned long dst; | |
180 | struct vport *vport; | |
181 | struct ovs_skb_cb cb; | |
182 | __be16 inner_protocol; | |
183 | u16 network_offset; /* valid only for MPLS */ | |
184 | u16 vlan_tci; | |
185 | __be16 vlan_proto; | |
186 | unsigned int l2_len; | |
187 | u8 mac_proto; | |
188 | u8 l2_data[MAX_L2_LEN]; | |
189 | }; | |
190 | ||
67231833 SAS |
191 | struct deferred_action { |
192 | struct sk_buff *skb; | |
193 | const struct nlattr *actions; | |
194 | int actions_len; | |
195 | ||
196 | /* Store pkt_key clone when creating deferred action. */ | |
197 | struct sw_flow_key pkt_key; | |
198 | }; | |
199 | ||
200 | #define DEFERRED_ACTION_FIFO_SIZE 10 | |
201 | #define OVS_RECURSION_LIMIT 5 | |
202 | #define OVS_DEFERRED_ACTION_THRESHOLD (OVS_RECURSION_LIMIT - 2) | |
203 | ||
204 | struct action_fifo { | |
205 | int head; | |
206 | int tail; | |
207 | /* Deferred action fifo queue storage. */ | |
208 | struct deferred_action fifo[DEFERRED_ACTION_FIFO_SIZE]; | |
209 | }; | |
210 | ||
211 | struct action_flow_keys { | |
212 | struct sw_flow_key key[OVS_DEFERRED_ACTION_THRESHOLD]; | |
213 | }; | |
214 | ||
215 | struct ovs_pcpu_storage { | |
216 | struct action_fifo action_fifos; | |
217 | struct action_flow_keys flow_keys; | |
3af4cdd6 | 218 | struct ovs_frag_data frag_data; |
67231833 SAS |
219 | int exec_level; |
220 | struct task_struct *owner; | |
221 | local_lock_t bh_lock; | |
222 | }; | |
7b4ac12c SAS |
223 | |
224 | extern struct ovs_pcpu_storage __percpu *ovs_pcpu_storage; | |
67231833 | 225 | |
bd1903b7 TZ |
226 | /** |
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. | |
232 | */ | |
233 | enum ovs_pkt_hash_types { | |
234 | OVS_PACKET_HASH_SW_BIT = (1ULL << 32), | |
235 | OVS_PACKET_HASH_L4_BIT = (1ULL << 33), | |
8e4e1713 PS |
236 | }; |
237 | ||
c7d03a00 | 238 | extern unsigned int ovs_net_id; |
8e4e1713 PS |
239 | void ovs_lock(void); |
240 | void ovs_unlock(void); | |
241 | ||
242 | #ifdef CONFIG_LOCKDEP | |
243 | int lockdep_ovsl_is_held(void); | |
244 | #else | |
245 | #define lockdep_ovsl_is_held() 1 | |
246 | #endif | |
247 | ||
80019d31 | 248 | #define ASSERT_OVSL() WARN_ON(!lockdep_ovsl_is_held()) |
8e4e1713 PS |
249 | #define ovsl_dereference(p) \ |
250 | rcu_dereference_protected(p, lockdep_ovsl_is_held()) | |
663efa36 JG |
251 | #define rcu_dereference_ovsl(p) \ |
252 | rcu_dereference_check(p, lockdep_ovsl_is_held()) | |
8e4e1713 | 253 | |
12eb18f7 | 254 | static inline struct net *ovs_dp_get_net(const struct datapath *dp) |
46df7b81 PS |
255 | { |
256 | return read_pnet(&dp->net); | |
257 | } | |
258 | ||
259 | static inline void ovs_dp_set_net(struct datapath *dp, struct net *net) | |
260 | { | |
261 | write_pnet(&dp->net, net); | |
262 | } | |
263 | ||
8e4e1713 PS |
264 | struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no); |
265 | ||
266 | static inline struct vport *ovs_vport_rcu(const struct datapath *dp, int port_no) | |
267 | { | |
268 | WARN_ON_ONCE(!rcu_read_lock_held()); | |
269 | return ovs_lookup_vport(dp, port_no); | |
270 | } | |
271 | ||
272 | static inline struct vport *ovs_vport_ovsl_rcu(const struct datapath *dp, int port_no) | |
273 | { | |
274 | WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_ovsl_is_held()); | |
275 | return ovs_lookup_vport(dp, port_no); | |
276 | } | |
277 | ||
278 | static inline struct vport *ovs_vport_ovsl(const struct datapath *dp, int port_no) | |
279 | { | |
280 | ASSERT_OVSL(); | |
281 | return ovs_lookup_vport(dp, port_no); | |
282 | } | |
283 | ||
9602c01e AZ |
284 | /* Must be called with rcu_read_lock. */ |
285 | static inline struct datapath *get_dp_rcu(struct net *net, int dp_ifindex) | |
286 | { | |
287 | struct net_device *dev = dev_get_by_index_rcu(net, dp_ifindex); | |
288 | ||
289 | if (dev) { | |
290 | struct vport *vport = ovs_internal_dev_get_vport(dev); | |
291 | ||
292 | if (vport) | |
293 | return vport->dp; | |
294 | } | |
295 | ||
296 | return NULL; | |
297 | } | |
298 | ||
299 | /* The caller must hold either ovs_mutex or rcu_read_lock to keep the | |
300 | * returned dp pointer valid. | |
301 | */ | |
302 | static inline struct datapath *get_dp(struct net *net, int dp_ifindex) | |
303 | { | |
304 | struct datapath *dp; | |
305 | ||
306 | WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_ovsl_is_held()); | |
307 | rcu_read_lock(); | |
308 | dp = get_dp_rcu(net, dp_ifindex); | |
309 | rcu_read_unlock(); | |
310 | ||
311 | return dp; | |
312 | } | |
313 | ||
ccb1352e | 314 | extern struct notifier_block ovs_dp_device_notifier; |
68eb5503 | 315 | extern struct genl_family dp_vport_genl_family; |
ccb1352e | 316 | |
8c8b1b83 | 317 | void ovs_dp_process_packet(struct sk_buff *skb, struct sw_flow_key *key); |
ccb1352e JG |
318 | void ovs_dp_detach_port(struct vport *); |
319 | int ovs_dp_upcall(struct datapath *, struct sk_buff *, | |
f2a4d086 WT |
320 | const struct sw_flow_key *, const struct dp_upcall_info *, |
321 | uint32_t cutlen); | |
ccb1352e | 322 | |
b83d23a2 MG |
323 | u32 ovs_dp_get_upcall_portid(const struct datapath *dp, uint32_t cpu_id); |
324 | ||
971427f3 | 325 | const char *ovs_dp_name(const struct datapath *dp); |
9354d452 JB |
326 | struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, struct net *net, |
327 | u32 portid, u32 seq, u8 cmd); | |
ccb1352e | 328 | |
2ff3e4e4 | 329 | int ovs_execute_actions(struct datapath *dp, struct sk_buff *skb, |
12eb18f7 | 330 | const struct sw_flow_actions *, struct sw_flow_key *); |
971427f3 | 331 | |
8e4e1713 | 332 | void ovs_dp_notify_wq(struct work_struct *work); |
03f0d916 | 333 | |
be26b9a8 JS |
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)) | |
337 | ||
05da5898 | 338 | #define OVS_NLERR(logging_allowed, fmt, ...) \ |
1815a883 | 339 | do { \ |
05da5898 JR |
340 | if (logging_allowed && net_ratelimit()) \ |
341 | pr_info("netlink: " fmt "\n", ##__VA_ARGS__); \ | |
1815a883 | 342 | } while (0) |
ccb1352e | 343 | #endif /* datapath.h */ |