Commit | Line | Data |
---|---|---|
ccb1352e | 1 | /* |
ad552007 | 2 | * Copyright (c) 2007-2014 Nicira, Inc. |
ccb1352e JG |
3 | * |
4 | * This program is free software; you can redistribute it and/or | |
5 | * modify it under the terms of version 2 of the GNU General Public | |
6 | * License as published by the Free Software Foundation. | |
7 | * | |
8 | * This program is distributed in the hope that it will be useful, but | |
9 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
11 | * General Public License for more details. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public License | |
14 | * along with this program; if not, write to the Free Software | |
15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | |
16 | * 02110-1301, USA | |
17 | */ | |
18 | ||
19 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt | |
20 | ||
21 | #include <linux/init.h> | |
22 | #include <linux/module.h> | |
23 | #include <linux/if_arp.h> | |
24 | #include <linux/if_vlan.h> | |
25 | #include <linux/in.h> | |
26 | #include <linux/ip.h> | |
27 | #include <linux/jhash.h> | |
28 | #include <linux/delay.h> | |
29 | #include <linux/time.h> | |
30 | #include <linux/etherdevice.h> | |
31 | #include <linux/genetlink.h> | |
32 | #include <linux/kernel.h> | |
33 | #include <linux/kthread.h> | |
34 | #include <linux/mutex.h> | |
35 | #include <linux/percpu.h> | |
36 | #include <linux/rcupdate.h> | |
37 | #include <linux/tcp.h> | |
38 | #include <linux/udp.h> | |
ccb1352e JG |
39 | #include <linux/ethtool.h> |
40 | #include <linux/wait.h> | |
ccb1352e JG |
41 | #include <asm/div64.h> |
42 | #include <linux/highmem.h> | |
43 | #include <linux/netfilter_bridge.h> | |
44 | #include <linux/netfilter_ipv4.h> | |
45 | #include <linux/inetdevice.h> | |
46 | #include <linux/list.h> | |
47 | #include <linux/openvswitch.h> | |
48 | #include <linux/rculist.h> | |
49 | #include <linux/dmi.h> | |
ccb1352e | 50 | #include <net/genetlink.h> |
46df7b81 PS |
51 | #include <net/net_namespace.h> |
52 | #include <net/netns/generic.h> | |
ccb1352e JG |
53 | |
54 | #include "datapath.h" | |
55 | #include "flow.h" | |
e80857cc | 56 | #include "flow_table.h" |
e6445719 | 57 | #include "flow_netlink.h" |
ccb1352e | 58 | #include "vport-internal_dev.h" |
cff63a52 | 59 | #include "vport-netdev.h" |
ccb1352e | 60 | |
8e4e1713 | 61 | int ovs_net_id __read_mostly; |
9ba559d9 | 62 | EXPORT_SYMBOL_GPL(ovs_net_id); |
8e4e1713 | 63 | |
0c200ef9 PS |
64 | static struct genl_family dp_packet_genl_family; |
65 | static struct genl_family dp_flow_genl_family; | |
66 | static struct genl_family dp_datapath_genl_family; | |
67 | ||
74ed7ab9 JS |
68 | static const struct nla_policy flow_policy[]; |
69 | ||
48e48a70 | 70 | static const struct genl_multicast_group ovs_dp_flow_multicast_group = { |
71 | .name = OVS_FLOW_MCGROUP, | |
0c200ef9 PS |
72 | }; |
73 | ||
48e48a70 | 74 | static const struct genl_multicast_group ovs_dp_datapath_multicast_group = { |
75 | .name = OVS_DATAPATH_MCGROUP, | |
0c200ef9 PS |
76 | }; |
77 | ||
48e48a70 | 78 | static const struct genl_multicast_group ovs_dp_vport_multicast_group = { |
79 | .name = OVS_VPORT_MCGROUP, | |
0c200ef9 PS |
80 | }; |
81 | ||
fb5d1e9e JR |
82 | /* Check if need to build a reply message. |
83 | * OVS userspace sets the NLM_F_ECHO flag if it needs the reply. */ | |
9b67aa4a SG |
84 | static bool ovs_must_notify(struct genl_family *family, struct genl_info *info, |
85 | unsigned int group) | |
fb5d1e9e JR |
86 | { |
87 | return info->nlhdr->nlmsg_flags & NLM_F_ECHO || | |
f8403a2e | 88 | genl_has_listeners(family, genl_info_net(info), group); |
fb5d1e9e JR |
89 | } |
90 | ||
68eb5503 | 91 | static void ovs_notify(struct genl_family *family, |
2a94fe48 | 92 | struct sk_buff *skb, struct genl_info *info) |
ed661185 | 93 | { |
68eb5503 | 94 | genl_notify(family, skb, genl_info_net(info), info->snd_portid, |
2a94fe48 | 95 | 0, info->nlhdr, GFP_KERNEL); |
ed661185 TG |
96 | } |
97 | ||
ccb1352e JG |
98 | /** |
99 | * DOC: Locking: | |
100 | * | |
8e4e1713 PS |
101 | * All writes e.g. Writes to device state (add/remove datapath, port, set |
102 | * operations on vports, etc.), Writes to other state (flow table | |
103 | * modifications, set miscellaneous datapath parameters, etc.) are protected | |
104 | * by ovs_lock. | |
ccb1352e JG |
105 | * |
106 | * Reads are protected by RCU. | |
107 | * | |
108 | * There are a few special cases (mostly stats) that have their own | |
109 | * synchronization but they nest under all of above and don't interact with | |
110 | * each other. | |
8e4e1713 PS |
111 | * |
112 | * The RTNL lock nests inside ovs_mutex. | |
ccb1352e JG |
113 | */ |
114 | ||
8e4e1713 PS |
115 | static DEFINE_MUTEX(ovs_mutex); |
116 | ||
117 | void ovs_lock(void) | |
118 | { | |
119 | mutex_lock(&ovs_mutex); | |
120 | } | |
121 | ||
122 | void ovs_unlock(void) | |
123 | { | |
124 | mutex_unlock(&ovs_mutex); | |
125 | } | |
126 | ||
127 | #ifdef CONFIG_LOCKDEP | |
128 | int lockdep_ovsl_is_held(void) | |
129 | { | |
130 | if (debug_locks) | |
131 | return lockdep_is_held(&ovs_mutex); | |
132 | else | |
133 | return 1; | |
134 | } | |
9ba559d9 | 135 | EXPORT_SYMBOL_GPL(lockdep_ovsl_is_held); |
8e4e1713 PS |
136 | #endif |
137 | ||
ccb1352e | 138 | static struct vport *new_vport(const struct vport_parms *); |
8055a89c | 139 | static int queue_gso_packets(struct datapath *dp, struct sk_buff *, |
e8eedb85 | 140 | const struct sw_flow_key *, |
ccb1352e | 141 | const struct dp_upcall_info *); |
8055a89c | 142 | static int queue_userspace_packet(struct datapath *dp, struct sk_buff *, |
e8eedb85 | 143 | const struct sw_flow_key *, |
ccb1352e JG |
144 | const struct dp_upcall_info *); |
145 | ||
cc3a5ae6 AZ |
146 | /* Must be called with rcu_read_lock. */ |
147 | static struct datapath *get_dp_rcu(struct net *net, int dp_ifindex) | |
ccb1352e | 148 | { |
cc3a5ae6 | 149 | struct net_device *dev = dev_get_by_index_rcu(net, dp_ifindex); |
ccb1352e | 150 | |
ccb1352e JG |
151 | if (dev) { |
152 | struct vport *vport = ovs_internal_dev_get_vport(dev); | |
153 | if (vport) | |
cc3a5ae6 | 154 | return vport->dp; |
ccb1352e | 155 | } |
cc3a5ae6 AZ |
156 | |
157 | return NULL; | |
158 | } | |
159 | ||
160 | /* The caller must hold either ovs_mutex or rcu_read_lock to keep the | |
161 | * returned dp pointer valid. | |
162 | */ | |
163 | static inline struct datapath *get_dp(struct net *net, int dp_ifindex) | |
164 | { | |
165 | struct datapath *dp; | |
166 | ||
167 | WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_ovsl_is_held()); | |
168 | rcu_read_lock(); | |
169 | dp = get_dp_rcu(net, dp_ifindex); | |
ccb1352e JG |
170 | rcu_read_unlock(); |
171 | ||
172 | return dp; | |
173 | } | |
174 | ||
8e4e1713 | 175 | /* Must be called with rcu_read_lock or ovs_mutex. */ |
971427f3 | 176 | const char *ovs_dp_name(const struct datapath *dp) |
ccb1352e | 177 | { |
8e4e1713 | 178 | struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL); |
ccb1352e JG |
179 | return vport->ops->get_name(vport); |
180 | } | |
181 | ||
12eb18f7 | 182 | static int get_dpifindex(const struct datapath *dp) |
ccb1352e JG |
183 | { |
184 | struct vport *local; | |
185 | int ifindex; | |
186 | ||
187 | rcu_read_lock(); | |
188 | ||
15eac2a7 | 189 | local = ovs_vport_rcu(dp, OVSP_LOCAL); |
ccb1352e | 190 | if (local) |
cff63a52 | 191 | ifindex = netdev_vport_priv(local)->dev->ifindex; |
ccb1352e JG |
192 | else |
193 | ifindex = 0; | |
194 | ||
195 | rcu_read_unlock(); | |
196 | ||
197 | return ifindex; | |
198 | } | |
199 | ||
200 | static void destroy_dp_rcu(struct rcu_head *rcu) | |
201 | { | |
202 | struct datapath *dp = container_of(rcu, struct datapath, rcu); | |
203 | ||
9b996e54 | 204 | ovs_flow_tbl_destroy(&dp->table); |
ccb1352e | 205 | free_percpu(dp->stats_percpu); |
46df7b81 | 206 | release_net(ovs_dp_get_net(dp)); |
15eac2a7 | 207 | kfree(dp->ports); |
ccb1352e JG |
208 | kfree(dp); |
209 | } | |
210 | ||
15eac2a7 PS |
211 | static struct hlist_head *vport_hash_bucket(const struct datapath *dp, |
212 | u16 port_no) | |
213 | { | |
214 | return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)]; | |
215 | } | |
216 | ||
bb6f9a70 | 217 | /* Called with ovs_mutex or RCU read lock. */ |
15eac2a7 PS |
218 | struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no) |
219 | { | |
220 | struct vport *vport; | |
15eac2a7 PS |
221 | struct hlist_head *head; |
222 | ||
223 | head = vport_hash_bucket(dp, port_no); | |
b67bfe0d | 224 | hlist_for_each_entry_rcu(vport, head, dp_hash_node) { |
15eac2a7 PS |
225 | if (vport->port_no == port_no) |
226 | return vport; | |
227 | } | |
228 | return NULL; | |
229 | } | |
230 | ||
8e4e1713 | 231 | /* Called with ovs_mutex. */ |
ccb1352e JG |
232 | static struct vport *new_vport(const struct vport_parms *parms) |
233 | { | |
234 | struct vport *vport; | |
235 | ||
236 | vport = ovs_vport_add(parms); | |
237 | if (!IS_ERR(vport)) { | |
238 | struct datapath *dp = parms->dp; | |
15eac2a7 | 239 | struct hlist_head *head = vport_hash_bucket(dp, vport->port_no); |
ccb1352e | 240 | |
15eac2a7 | 241 | hlist_add_head_rcu(&vport->dp_hash_node, head); |
ccb1352e | 242 | } |
ccb1352e JG |
243 | return vport; |
244 | } | |
245 | ||
ccb1352e JG |
246 | void ovs_dp_detach_port(struct vport *p) |
247 | { | |
8e4e1713 | 248 | ASSERT_OVSL(); |
ccb1352e JG |
249 | |
250 | /* First drop references to device. */ | |
15eac2a7 | 251 | hlist_del_rcu(&p->dp_hash_node); |
ccb1352e JG |
252 | |
253 | /* Then destroy it. */ | |
254 | ovs_vport_del(p); | |
255 | } | |
256 | ||
257 | /* Must be called with rcu_read_lock. */ | |
8c8b1b83 | 258 | void ovs_dp_process_packet(struct sk_buff *skb, struct sw_flow_key *key) |
ccb1352e | 259 | { |
83c8df26 | 260 | const struct vport *p = OVS_CB(skb)->input_vport; |
ccb1352e JG |
261 | struct datapath *dp = p->dp; |
262 | struct sw_flow *flow; | |
d98612b8 | 263 | struct sw_flow_actions *sf_acts; |
ccb1352e | 264 | struct dp_stats_percpu *stats; |
ccb1352e | 265 | u64 *stats_counter; |
1bd7116f | 266 | u32 n_mask_hit; |
ccb1352e | 267 | |
404f2f10 | 268 | stats = this_cpu_ptr(dp->stats_percpu); |
ccb1352e | 269 | |
ccb1352e | 270 | /* Look up flow. */ |
8c8b1b83 | 271 | flow = ovs_flow_tbl_lookup_stats(&dp->table, key, &n_mask_hit); |
ccb1352e JG |
272 | if (unlikely(!flow)) { |
273 | struct dp_upcall_info upcall; | |
8c8b1b83 | 274 | int error; |
ccb1352e JG |
275 | |
276 | upcall.cmd = OVS_PACKET_CMD_MISS; | |
ccb1352e | 277 | upcall.userdata = NULL; |
5cd667b0 | 278 | upcall.portid = ovs_vport_find_upcall_portid(p, skb); |
8f0aad6f | 279 | upcall.egress_tun_info = NULL; |
e8eedb85 | 280 | error = ovs_dp_upcall(dp, skb, key, &upcall); |
c5eba0b6 LR |
281 | if (unlikely(error)) |
282 | kfree_skb(skb); | |
283 | else | |
284 | consume_skb(skb); | |
ccb1352e JG |
285 | stats_counter = &stats->n_missed; |
286 | goto out; | |
287 | } | |
288 | ||
d98612b8 LJ |
289 | ovs_flow_stats_update(flow, key->tp.flags, skb); |
290 | sf_acts = rcu_dereference(flow->sf_acts); | |
291 | ovs_execute_actions(dp, skb, sf_acts, key); | |
ccb1352e | 292 | |
e298e505 | 293 | stats_counter = &stats->n_hit; |
ccb1352e JG |
294 | |
295 | out: | |
296 | /* Update datapath statistics. */ | |
df9d9fdf | 297 | u64_stats_update_begin(&stats->syncp); |
ccb1352e | 298 | (*stats_counter)++; |
1bd7116f | 299 | stats->n_mask_hit += n_mask_hit; |
df9d9fdf | 300 | u64_stats_update_end(&stats->syncp); |
ccb1352e JG |
301 | } |
302 | ||
ccb1352e | 303 | int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb, |
e8eedb85 | 304 | const struct sw_flow_key *key, |
46df7b81 | 305 | const struct dp_upcall_info *upcall_info) |
ccb1352e JG |
306 | { |
307 | struct dp_stats_percpu *stats; | |
ccb1352e JG |
308 | int err; |
309 | ||
15e47304 | 310 | if (upcall_info->portid == 0) { |
ccb1352e JG |
311 | err = -ENOTCONN; |
312 | goto err; | |
313 | } | |
314 | ||
ccb1352e | 315 | if (!skb_is_gso(skb)) |
e8eedb85 | 316 | err = queue_userspace_packet(dp, skb, key, upcall_info); |
ccb1352e | 317 | else |
e8eedb85 | 318 | err = queue_gso_packets(dp, skb, key, upcall_info); |
ccb1352e JG |
319 | if (err) |
320 | goto err; | |
321 | ||
322 | return 0; | |
323 | ||
324 | err: | |
404f2f10 | 325 | stats = this_cpu_ptr(dp->stats_percpu); |
ccb1352e | 326 | |
df9d9fdf | 327 | u64_stats_update_begin(&stats->syncp); |
ccb1352e | 328 | stats->n_lost++; |
df9d9fdf | 329 | u64_stats_update_end(&stats->syncp); |
ccb1352e JG |
330 | |
331 | return err; | |
332 | } | |
333 | ||
8055a89c | 334 | static int queue_gso_packets(struct datapath *dp, struct sk_buff *skb, |
e8eedb85 | 335 | const struct sw_flow_key *key, |
ccb1352e JG |
336 | const struct dp_upcall_info *upcall_info) |
337 | { | |
a1b5d0dd | 338 | unsigned short gso_type = skb_shinfo(skb)->gso_type; |
ccb1352e JG |
339 | struct sw_flow_key later_key; |
340 | struct sk_buff *segs, *nskb; | |
e8eedb85 | 341 | struct ovs_skb_cb ovs_cb; |
ccb1352e JG |
342 | int err; |
343 | ||
e8eedb85 | 344 | ovs_cb = *OVS_CB(skb); |
09c5e605 | 345 | segs = __skb_gso_segment(skb, NETIF_F_SG, false); |
e8eedb85 | 346 | *OVS_CB(skb) = ovs_cb; |
92e5dfc3 PS |
347 | if (IS_ERR(segs)) |
348 | return PTR_ERR(segs); | |
330966e5 FW |
349 | if (segs == NULL) |
350 | return -EINVAL; | |
ccb1352e | 351 | |
e8eedb85 PS |
352 | if (gso_type & SKB_GSO_UDP) { |
353 | /* The initial flow key extracted by ovs_flow_key_extract() | |
354 | * in this case is for a first fragment, so we need to | |
355 | * properly mark later fragments. | |
356 | */ | |
357 | later_key = *key; | |
358 | later_key.ip.frag = OVS_FRAG_TYPE_LATER; | |
359 | } | |
360 | ||
ccb1352e JG |
361 | /* Queue all of the segments. */ |
362 | skb = segs; | |
363 | do { | |
e8eedb85 PS |
364 | *OVS_CB(skb) = ovs_cb; |
365 | if (gso_type & SKB_GSO_UDP && skb != segs) | |
366 | key = &later_key; | |
367 | ||
368 | err = queue_userspace_packet(dp, skb, key, upcall_info); | |
ccb1352e JG |
369 | if (err) |
370 | break; | |
371 | ||
ccb1352e JG |
372 | } while ((skb = skb->next)); |
373 | ||
374 | /* Free all of the segments. */ | |
375 | skb = segs; | |
376 | do { | |
377 | nskb = skb->next; | |
378 | if (err) | |
379 | kfree_skb(skb); | |
380 | else | |
381 | consume_skb(skb); | |
382 | } while ((skb = nskb)); | |
383 | return err; | |
384 | } | |
385 | ||
8f0aad6f | 386 | static size_t upcall_msg_size(const struct dp_upcall_info *upcall_info, |
bda56f14 | 387 | unsigned int hdrlen) |
c3ff8cfe TG |
388 | { |
389 | size_t size = NLMSG_ALIGN(sizeof(struct ovs_header)) | |
bda56f14 | 390 | + nla_total_size(hdrlen) /* OVS_PACKET_ATTR_PACKET */ |
41af73e9 | 391 | + nla_total_size(ovs_key_attr_size()); /* OVS_PACKET_ATTR_KEY */ |
c3ff8cfe TG |
392 | |
393 | /* OVS_PACKET_ATTR_USERDATA */ | |
8f0aad6f WZ |
394 | if (upcall_info->userdata) |
395 | size += NLA_ALIGN(upcall_info->userdata->nla_len); | |
396 | ||
397 | /* OVS_PACKET_ATTR_EGRESS_TUN_KEY */ | |
398 | if (upcall_info->egress_tun_info) | |
399 | size += nla_total_size(ovs_tun_key_attr_size()); | |
c3ff8cfe TG |
400 | |
401 | return size; | |
402 | } | |
403 | ||
8055a89c | 404 | static int queue_userspace_packet(struct datapath *dp, struct sk_buff *skb, |
e8eedb85 | 405 | const struct sw_flow_key *key, |
ccb1352e JG |
406 | const struct dp_upcall_info *upcall_info) |
407 | { | |
408 | struct ovs_header *upcall; | |
409 | struct sk_buff *nskb = NULL; | |
4ee45ea0 | 410 | struct sk_buff *user_skb = NULL; /* to be queued to userspace */ |
ccb1352e | 411 | struct nlattr *nla; |
795449d8 | 412 | struct genl_info info = { |
8055a89c | 413 | .dst_sk = ovs_dp_get_net(dp)->genl_sock, |
795449d8 TG |
414 | .snd_portid = upcall_info->portid, |
415 | }; | |
416 | size_t len; | |
bda56f14 | 417 | unsigned int hlen; |
8055a89c TG |
418 | int err, dp_ifindex; |
419 | ||
420 | dp_ifindex = get_dpifindex(dp); | |
421 | if (!dp_ifindex) | |
422 | return -ENODEV; | |
ccb1352e | 423 | |
df8a39de | 424 | if (skb_vlan_tag_present(skb)) { |
ccb1352e JG |
425 | nskb = skb_clone(skb, GFP_ATOMIC); |
426 | if (!nskb) | |
427 | return -ENOMEM; | |
428 | ||
5968250c | 429 | nskb = __vlan_hwaccel_push_inside(nskb); |
8aa51d64 | 430 | if (!nskb) |
ccb1352e JG |
431 | return -ENOMEM; |
432 | ||
ccb1352e JG |
433 | skb = nskb; |
434 | } | |
435 | ||
436 | if (nla_attr_size(skb->len) > USHRT_MAX) { | |
437 | err = -EFBIG; | |
438 | goto out; | |
439 | } | |
440 | ||
bda56f14 TG |
441 | /* Complete checksum if needed */ |
442 | if (skb->ip_summed == CHECKSUM_PARTIAL && | |
443 | (err = skb_checksum_help(skb))) | |
444 | goto out; | |
445 | ||
446 | /* Older versions of OVS user space enforce alignment of the last | |
447 | * Netlink attribute to NLA_ALIGNTO which would require extensive | |
448 | * padding logic. Only perform zerocopy if padding is not required. | |
449 | */ | |
450 | if (dp->user_features & OVS_DP_F_UNALIGNED) | |
451 | hlen = skb_zerocopy_headlen(skb); | |
452 | else | |
453 | hlen = skb->len; | |
454 | ||
8f0aad6f | 455 | len = upcall_msg_size(upcall_info, hlen); |
795449d8 | 456 | user_skb = genlmsg_new_unicast(len, &info, GFP_ATOMIC); |
ccb1352e JG |
457 | if (!user_skb) { |
458 | err = -ENOMEM; | |
459 | goto out; | |
460 | } | |
461 | ||
462 | upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family, | |
463 | 0, upcall_info->cmd); | |
464 | upcall->dp_ifindex = dp_ifindex; | |
465 | ||
5b4237bb | 466 | err = ovs_nla_put_key(key, key, OVS_PACKET_ATTR_KEY, false, user_skb); |
f53e3831 | 467 | BUG_ON(err); |
ccb1352e JG |
468 | |
469 | if (upcall_info->userdata) | |
4490108b BP |
470 | __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA, |
471 | nla_len(upcall_info->userdata), | |
472 | nla_data(upcall_info->userdata)); | |
ccb1352e | 473 | |
8f0aad6f WZ |
474 | if (upcall_info->egress_tun_info) { |
475 | nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_EGRESS_TUN_KEY); | |
476 | err = ovs_nla_put_egress_tunnel_key(user_skb, | |
477 | upcall_info->egress_tun_info); | |
478 | BUG_ON(err); | |
479 | nla_nest_end(user_skb, nla); | |
480 | } | |
481 | ||
bda56f14 TG |
482 | /* Only reserve room for attribute header, packet data is added |
483 | * in skb_zerocopy() */ | |
484 | if (!(nla = nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, 0))) { | |
485 | err = -ENOBUFS; | |
486 | goto out; | |
487 | } | |
488 | nla->nla_len = nla_attr_size(skb->len); | |
ccb1352e | 489 | |
36d5fe6a ZK |
490 | err = skb_zerocopy(user_skb, skb, skb->len, hlen); |
491 | if (err) | |
492 | goto out; | |
ccb1352e | 493 | |
aea0bb4f TG |
494 | /* Pad OVS_PACKET_ATTR_PACKET if linear copy was performed */ |
495 | if (!(dp->user_features & OVS_DP_F_UNALIGNED)) { | |
496 | size_t plen = NLA_ALIGN(user_skb->len) - user_skb->len; | |
497 | ||
498 | if (plen > 0) | |
499 | memset(skb_put(user_skb, plen), 0, plen); | |
500 | } | |
501 | ||
bda56f14 | 502 | ((struct nlmsghdr *) user_skb->data)->nlmsg_len = user_skb->len; |
ccb1352e | 503 | |
bda56f14 | 504 | err = genlmsg_unicast(ovs_dp_get_net(dp), user_skb, upcall_info->portid); |
4ee45ea0 | 505 | user_skb = NULL; |
ccb1352e | 506 | out: |
36d5fe6a ZK |
507 | if (err) |
508 | skb_tx_error(skb); | |
4ee45ea0 | 509 | kfree_skb(user_skb); |
ccb1352e JG |
510 | kfree_skb(nskb); |
511 | return err; | |
512 | } | |
513 | ||
ccb1352e JG |
514 | static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info) |
515 | { | |
516 | struct ovs_header *ovs_header = info->userhdr; | |
517 | struct nlattr **a = info->attrs; | |
518 | struct sw_flow_actions *acts; | |
519 | struct sk_buff *packet; | |
520 | struct sw_flow *flow; | |
d98612b8 | 521 | struct sw_flow_actions *sf_acts; |
ccb1352e JG |
522 | struct datapath *dp; |
523 | struct ethhdr *eth; | |
83c8df26 | 524 | struct vport *input_vport; |
ccb1352e JG |
525 | int len; |
526 | int err; | |
1ba39804 | 527 | bool log = !a[OVS_PACKET_ATTR_PROBE]; |
ccb1352e JG |
528 | |
529 | err = -EINVAL; | |
530 | if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] || | |
dded45fc | 531 | !a[OVS_PACKET_ATTR_ACTIONS]) |
ccb1352e JG |
532 | goto err; |
533 | ||
534 | len = nla_len(a[OVS_PACKET_ATTR_PACKET]); | |
535 | packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL); | |
536 | err = -ENOMEM; | |
537 | if (!packet) | |
538 | goto err; | |
539 | skb_reserve(packet, NET_IP_ALIGN); | |
540 | ||
32686a9d | 541 | nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len); |
ccb1352e JG |
542 | |
543 | skb_reset_mac_header(packet); | |
544 | eth = eth_hdr(packet); | |
545 | ||
546 | /* Normally, setting the skb 'protocol' field would be handled by a | |
547 | * call to eth_type_trans(), but it assumes there's a sending | |
548 | * device, which we may not have. */ | |
e5c5d22e | 549 | if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN) |
ccb1352e JG |
550 | packet->protocol = eth->h_proto; |
551 | else | |
552 | packet->protocol = htons(ETH_P_802_2); | |
553 | ||
554 | /* Build an sw_flow for sending this packet. */ | |
23dabf88 | 555 | flow = ovs_flow_alloc(); |
ccb1352e JG |
556 | err = PTR_ERR(flow); |
557 | if (IS_ERR(flow)) | |
558 | goto err_kfree_skb; | |
559 | ||
83c8df26 | 560 | err = ovs_flow_key_extract_userspace(a[OVS_PACKET_ATTR_KEY], packet, |
05da5898 | 561 | &flow->key, log); |
ccb1352e JG |
562 | if (err) |
563 | goto err_flow_free; | |
564 | ||
e6445719 | 565 | err = ovs_nla_copy_actions(a[OVS_PACKET_ATTR_ACTIONS], |
05da5898 | 566 | &flow->key, &acts, log); |
74f84a57 PS |
567 | if (err) |
568 | goto err_flow_free; | |
ccb1352e | 569 | |
f5796684 | 570 | rcu_assign_pointer(flow->sf_acts, acts); |
f5796684 | 571 | OVS_CB(packet)->egress_tun_info = NULL; |
ccb1352e | 572 | packet->priority = flow->key.phy.priority; |
39c7caeb | 573 | packet->mark = flow->key.phy.skb_mark; |
ccb1352e JG |
574 | |
575 | rcu_read_lock(); | |
cc3a5ae6 | 576 | dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex); |
ccb1352e JG |
577 | err = -ENODEV; |
578 | if (!dp) | |
579 | goto err_unlock; | |
580 | ||
83c8df26 PS |
581 | input_vport = ovs_vport_rcu(dp, flow->key.phy.in_port); |
582 | if (!input_vport) | |
583 | input_vport = ovs_vport_rcu(dp, OVSP_LOCAL); | |
584 | ||
585 | if (!input_vport) | |
586 | goto err_unlock; | |
587 | ||
588 | OVS_CB(packet)->input_vport = input_vport; | |
d98612b8 | 589 | sf_acts = rcu_dereference(flow->sf_acts); |
83c8df26 | 590 | |
ccb1352e | 591 | local_bh_disable(); |
d98612b8 | 592 | err = ovs_execute_actions(dp, packet, sf_acts, &flow->key); |
ccb1352e JG |
593 | local_bh_enable(); |
594 | rcu_read_unlock(); | |
595 | ||
03f0d916 | 596 | ovs_flow_free(flow, false); |
ccb1352e JG |
597 | return err; |
598 | ||
599 | err_unlock: | |
600 | rcu_read_unlock(); | |
601 | err_flow_free: | |
03f0d916 | 602 | ovs_flow_free(flow, false); |
ccb1352e JG |
603 | err_kfree_skb: |
604 | kfree_skb(packet); | |
605 | err: | |
606 | return err; | |
607 | } | |
608 | ||
609 | static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = { | |
dded45fc | 610 | [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN }, |
ccb1352e JG |
611 | [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED }, |
612 | [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED }, | |
1ba39804 | 613 | [OVS_PACKET_ATTR_PROBE] = { .type = NLA_FLAG }, |
ccb1352e JG |
614 | }; |
615 | ||
4534de83 | 616 | static const struct genl_ops dp_packet_genl_ops[] = { |
ccb1352e JG |
617 | { .cmd = OVS_PACKET_CMD_EXECUTE, |
618 | .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ | |
619 | .policy = packet_policy, | |
620 | .doit = ovs_packet_cmd_execute | |
621 | } | |
622 | }; | |
623 | ||
0c200ef9 PS |
624 | static struct genl_family dp_packet_genl_family = { |
625 | .id = GENL_ID_GENERATE, | |
626 | .hdrsize = sizeof(struct ovs_header), | |
627 | .name = OVS_PACKET_FAMILY, | |
628 | .version = OVS_PACKET_VERSION, | |
629 | .maxattr = OVS_PACKET_ATTR_MAX, | |
630 | .netnsok = true, | |
631 | .parallel_ops = true, | |
632 | .ops = dp_packet_genl_ops, | |
633 | .n_ops = ARRAY_SIZE(dp_packet_genl_ops), | |
634 | }; | |
635 | ||
12eb18f7 | 636 | static void get_dp_stats(const struct datapath *dp, struct ovs_dp_stats *stats, |
1bd7116f | 637 | struct ovs_dp_megaflow_stats *mega_stats) |
ccb1352e JG |
638 | { |
639 | int i; | |
ccb1352e | 640 | |
1bd7116f AZ |
641 | memset(mega_stats, 0, sizeof(*mega_stats)); |
642 | ||
b637e498 | 643 | stats->n_flows = ovs_flow_tbl_count(&dp->table); |
1bd7116f | 644 | mega_stats->n_masks = ovs_flow_tbl_num_masks(&dp->table); |
ccb1352e JG |
645 | |
646 | stats->n_hit = stats->n_missed = stats->n_lost = 0; | |
1bd7116f | 647 | |
ccb1352e JG |
648 | for_each_possible_cpu(i) { |
649 | const struct dp_stats_percpu *percpu_stats; | |
650 | struct dp_stats_percpu local_stats; | |
651 | unsigned int start; | |
652 | ||
653 | percpu_stats = per_cpu_ptr(dp->stats_percpu, i); | |
654 | ||
655 | do { | |
57a7744e | 656 | start = u64_stats_fetch_begin_irq(&percpu_stats->syncp); |
ccb1352e | 657 | local_stats = *percpu_stats; |
57a7744e | 658 | } while (u64_stats_fetch_retry_irq(&percpu_stats->syncp, start)); |
ccb1352e JG |
659 | |
660 | stats->n_hit += local_stats.n_hit; | |
661 | stats->n_missed += local_stats.n_missed; | |
662 | stats->n_lost += local_stats.n_lost; | |
1bd7116f | 663 | mega_stats->n_mask_hit += local_stats.n_mask_hit; |
ccb1352e JG |
664 | } |
665 | } | |
666 | ||
74ed7ab9 JS |
667 | static bool should_fill_key(const struct sw_flow_id *sfid, uint32_t ufid_flags) |
668 | { | |
669 | return ovs_identifier_is_ufid(sfid) && | |
670 | !(ufid_flags & OVS_UFID_F_OMIT_KEY); | |
671 | } | |
672 | ||
673 | static bool should_fill_mask(uint32_t ufid_flags) | |
674 | { | |
675 | return !(ufid_flags & OVS_UFID_F_OMIT_MASK); | |
676 | } | |
677 | ||
678 | static bool should_fill_actions(uint32_t ufid_flags) | |
c3ff8cfe | 679 | { |
74ed7ab9 JS |
680 | return !(ufid_flags & OVS_UFID_F_OMIT_ACTIONS); |
681 | } | |
682 | ||
683 | static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts, | |
684 | const struct sw_flow_id *sfid, | |
685 | uint32_t ufid_flags) | |
686 | { | |
687 | size_t len = NLMSG_ALIGN(sizeof(struct ovs_header)); | |
688 | ||
689 | /* OVS_FLOW_ATTR_UFID */ | |
690 | if (sfid && ovs_identifier_is_ufid(sfid)) | |
691 | len += nla_total_size(sfid->ufid_len); | |
692 | ||
693 | /* OVS_FLOW_ATTR_KEY */ | |
694 | if (!sfid || should_fill_key(sfid, ufid_flags)) | |
695 | len += nla_total_size(ovs_key_attr_size()); | |
696 | ||
697 | /* OVS_FLOW_ATTR_MASK */ | |
698 | if (should_fill_mask(ufid_flags)) | |
699 | len += nla_total_size(ovs_key_attr_size()); | |
700 | ||
701 | /* OVS_FLOW_ATTR_ACTIONS */ | |
702 | if (should_fill_actions(ufid_flags)) | |
703 | len += nla_total_size(acts->actions_len); | |
704 | ||
705 | return len | |
c3ff8cfe TG |
706 | + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */ |
707 | + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */ | |
74ed7ab9 | 708 | + nla_total_size(8); /* OVS_FLOW_ATTR_USED */ |
c3ff8cfe TG |
709 | } |
710 | ||
ca7105f2 JS |
711 | /* Called with ovs_mutex or RCU read lock. */ |
712 | static int ovs_flow_cmd_fill_stats(const struct sw_flow *flow, | |
713 | struct sk_buff *skb) | |
714 | { | |
715 | struct ovs_flow_stats stats; | |
716 | __be16 tcp_flags; | |
717 | unsigned long used; | |
ccb1352e | 718 | |
e298e505 | 719 | ovs_flow_stats_get(flow, &stats, &used, &tcp_flags); |
0e9796b4 | 720 | |
028d6a67 DM |
721 | if (used && |
722 | nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used))) | |
ca7105f2 | 723 | return -EMSGSIZE; |
ccb1352e | 724 | |
028d6a67 | 725 | if (stats.n_packets && |
e298e505 | 726 | nla_put(skb, OVS_FLOW_ATTR_STATS, sizeof(struct ovs_flow_stats), &stats)) |
ca7105f2 | 727 | return -EMSGSIZE; |
ccb1352e | 728 | |
e298e505 PS |
729 | if ((u8)ntohs(tcp_flags) && |
730 | nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, (u8)ntohs(tcp_flags))) | |
ca7105f2 JS |
731 | return -EMSGSIZE; |
732 | ||
733 | return 0; | |
734 | } | |
735 | ||
736 | /* Called with ovs_mutex or RCU read lock. */ | |
737 | static int ovs_flow_cmd_fill_actions(const struct sw_flow *flow, | |
738 | struct sk_buff *skb, int skb_orig_len) | |
739 | { | |
740 | struct nlattr *start; | |
741 | int err; | |
ccb1352e JG |
742 | |
743 | /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if | |
744 | * this is the first flow to be dumped into 'skb'. This is unusual for | |
745 | * Netlink but individual action lists can be longer than | |
746 | * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this. | |
747 | * The userspace caller can always fetch the actions separately if it | |
748 | * really wants them. (Most userspace callers in fact don't care.) | |
749 | * | |
750 | * This can only fail for dump operations because the skb is always | |
751 | * properly sized for single flows. | |
752 | */ | |
74f84a57 PS |
753 | start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS); |
754 | if (start) { | |
d57170b1 PS |
755 | const struct sw_flow_actions *sf_acts; |
756 | ||
663efa36 | 757 | sf_acts = rcu_dereference_ovsl(flow->sf_acts); |
e6445719 PS |
758 | err = ovs_nla_put_actions(sf_acts->actions, |
759 | sf_acts->actions_len, skb); | |
0e9796b4 | 760 | |
74f84a57 PS |
761 | if (!err) |
762 | nla_nest_end(skb, start); | |
763 | else { | |
764 | if (skb_orig_len) | |
ca7105f2 | 765 | return err; |
74f84a57 PS |
766 | |
767 | nla_nest_cancel(skb, start); | |
768 | } | |
ca7105f2 JS |
769 | } else if (skb_orig_len) { |
770 | return -EMSGSIZE; | |
771 | } | |
772 | ||
773 | return 0; | |
774 | } | |
775 | ||
776 | /* Called with ovs_mutex or RCU read lock. */ | |
777 | static int ovs_flow_cmd_fill_info(const struct sw_flow *flow, int dp_ifindex, | |
778 | struct sk_buff *skb, u32 portid, | |
74ed7ab9 | 779 | u32 seq, u32 flags, u8 cmd, u32 ufid_flags) |
ca7105f2 JS |
780 | { |
781 | const int skb_orig_len = skb->len; | |
782 | struct ovs_header *ovs_header; | |
783 | int err; | |
784 | ||
785 | ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, | |
786 | flags, cmd); | |
787 | if (!ovs_header) | |
788 | return -EMSGSIZE; | |
789 | ||
790 | ovs_header->dp_ifindex = dp_ifindex; | |
791 | ||
74ed7ab9 | 792 | err = ovs_nla_put_identifier(flow, skb); |
5b4237bb JS |
793 | if (err) |
794 | goto error; | |
795 | ||
74ed7ab9 JS |
796 | if (should_fill_key(&flow->id, ufid_flags)) { |
797 | err = ovs_nla_put_masked_key(flow, skb); | |
798 | if (err) | |
799 | goto error; | |
800 | } | |
801 | ||
802 | if (should_fill_mask(ufid_flags)) { | |
803 | err = ovs_nla_put_mask(flow, skb); | |
804 | if (err) | |
805 | goto error; | |
806 | } | |
ca7105f2 JS |
807 | |
808 | err = ovs_flow_cmd_fill_stats(flow, skb); | |
809 | if (err) | |
810 | goto error; | |
811 | ||
74ed7ab9 JS |
812 | if (should_fill_actions(ufid_flags)) { |
813 | err = ovs_flow_cmd_fill_actions(flow, skb, skb_orig_len); | |
814 | if (err) | |
815 | goto error; | |
816 | } | |
ccb1352e | 817 | |
053c095a JB |
818 | genlmsg_end(skb, ovs_header); |
819 | return 0; | |
ccb1352e | 820 | |
ccb1352e JG |
821 | error: |
822 | genlmsg_cancel(skb, ovs_header); | |
823 | return err; | |
824 | } | |
825 | ||
0e9796b4 JR |
826 | /* May not be called with RCU read lock. */ |
827 | static struct sk_buff *ovs_flow_cmd_alloc_info(const struct sw_flow_actions *acts, | |
74ed7ab9 | 828 | const struct sw_flow_id *sfid, |
fb5d1e9e | 829 | struct genl_info *info, |
74ed7ab9 JS |
830 | bool always, |
831 | uint32_t ufid_flags) | |
ccb1352e | 832 | { |
fb5d1e9e | 833 | struct sk_buff *skb; |
74ed7ab9 | 834 | size_t len; |
ccb1352e | 835 | |
9b67aa4a | 836 | if (!always && !ovs_must_notify(&dp_flow_genl_family, info, 0)) |
fb5d1e9e JR |
837 | return NULL; |
838 | ||
74ed7ab9 JS |
839 | len = ovs_flow_cmd_msg_size(acts, sfid, ufid_flags); |
840 | skb = genlmsg_new_unicast(len, info, GFP_KERNEL); | |
fb5d1e9e JR |
841 | if (!skb) |
842 | return ERR_PTR(-ENOMEM); | |
843 | ||
844 | return skb; | |
ccb1352e JG |
845 | } |
846 | ||
0e9796b4 JR |
847 | /* Called with ovs_mutex. */ |
848 | static struct sk_buff *ovs_flow_cmd_build_info(const struct sw_flow *flow, | |
849 | int dp_ifindex, | |
850 | struct genl_info *info, u8 cmd, | |
74ed7ab9 | 851 | bool always, u32 ufid_flags) |
ccb1352e JG |
852 | { |
853 | struct sk_buff *skb; | |
854 | int retval; | |
855 | ||
74ed7ab9 JS |
856 | skb = ovs_flow_cmd_alloc_info(ovsl_dereference(flow->sf_acts), |
857 | &flow->id, info, always, ufid_flags); | |
d0e992aa | 858 | if (IS_ERR_OR_NULL(skb)) |
fb5d1e9e | 859 | return skb; |
ccb1352e | 860 | |
0e9796b4 JR |
861 | retval = ovs_flow_cmd_fill_info(flow, dp_ifindex, skb, |
862 | info->snd_portid, info->snd_seq, 0, | |
74ed7ab9 | 863 | cmd, ufid_flags); |
ccb1352e JG |
864 | BUG_ON(retval < 0); |
865 | return skb; | |
866 | } | |
867 | ||
37bdc87b | 868 | static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info) |
ccb1352e JG |
869 | { |
870 | struct nlattr **a = info->attrs; | |
871 | struct ovs_header *ovs_header = info->userhdr; | |
74ed7ab9 | 872 | struct sw_flow *flow = NULL, *new_flow; |
03f0d916 | 873 | struct sw_flow_mask mask; |
ccb1352e JG |
874 | struct sk_buff *reply; |
875 | struct datapath *dp; | |
74ed7ab9 | 876 | struct sw_flow_key key; |
37bdc87b | 877 | struct sw_flow_actions *acts; |
03f0d916 | 878 | struct sw_flow_match match; |
74ed7ab9 | 879 | u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]); |
ccb1352e | 880 | int error; |
05da5898 | 881 | bool log = !a[OVS_FLOW_ATTR_PROBE]; |
ccb1352e | 882 | |
893f139b | 883 | /* Must have key and actions. */ |
ccb1352e | 884 | error = -EINVAL; |
426cda5c | 885 | if (!a[OVS_FLOW_ATTR_KEY]) { |
05da5898 | 886 | OVS_NLERR(log, "Flow key attr not present in new flow."); |
ccb1352e | 887 | goto error; |
426cda5c JG |
888 | } |
889 | if (!a[OVS_FLOW_ATTR_ACTIONS]) { | |
05da5898 | 890 | OVS_NLERR(log, "Flow actions attr not present in new flow."); |
893f139b | 891 | goto error; |
426cda5c | 892 | } |
03f0d916 | 893 | |
893f139b JR |
894 | /* Most of the time we need to allocate a new flow, do it before |
895 | * locking. | |
896 | */ | |
897 | new_flow = ovs_flow_alloc(); | |
898 | if (IS_ERR(new_flow)) { | |
899 | error = PTR_ERR(new_flow); | |
900 | goto error; | |
901 | } | |
902 | ||
903 | /* Extract key. */ | |
74ed7ab9 | 904 | ovs_match_init(&match, &key, &mask); |
05da5898 JR |
905 | error = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], |
906 | a[OVS_FLOW_ATTR_MASK], log); | |
ccb1352e | 907 | if (error) |
893f139b | 908 | goto err_kfree_flow; |
ccb1352e | 909 | |
74ed7ab9 JS |
910 | ovs_flow_mask_key(&new_flow->key, &key, &mask); |
911 | ||
912 | /* Extract flow identifier. */ | |
913 | error = ovs_nla_get_identifier(&new_flow->id, a[OVS_FLOW_ATTR_UFID], | |
914 | &key, log); | |
915 | if (error) | |
916 | goto err_kfree_flow; | |
74f84a57 | 917 | |
893f139b | 918 | /* Validate actions. */ |
893f139b | 919 | error = ovs_nla_copy_actions(a[OVS_FLOW_ATTR_ACTIONS], &new_flow->key, |
05da5898 | 920 | &acts, log); |
37bdc87b | 921 | if (error) { |
05da5898 | 922 | OVS_NLERR(log, "Flow actions may not be safe on all matching packets."); |
2fdb957d | 923 | goto err_kfree_flow; |
893f139b JR |
924 | } |
925 | ||
74ed7ab9 JS |
926 | reply = ovs_flow_cmd_alloc_info(acts, &new_flow->id, info, false, |
927 | ufid_flags); | |
893f139b JR |
928 | if (IS_ERR(reply)) { |
929 | error = PTR_ERR(reply); | |
930 | goto err_kfree_acts; | |
ccb1352e JG |
931 | } |
932 | ||
8e4e1713 | 933 | ovs_lock(); |
46df7b81 | 934 | dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex); |
893f139b JR |
935 | if (unlikely(!dp)) { |
936 | error = -ENODEV; | |
8e4e1713 | 937 | goto err_unlock_ovs; |
893f139b | 938 | } |
74ed7ab9 | 939 | |
03f0d916 | 940 | /* Check if this is a duplicate flow */ |
74ed7ab9 JS |
941 | if (ovs_identifier_is_ufid(&new_flow->id)) |
942 | flow = ovs_flow_tbl_lookup_ufid(&dp->table, &new_flow->id); | |
943 | if (!flow) | |
944 | flow = ovs_flow_tbl_lookup(&dp->table, &key); | |
893f139b JR |
945 | if (likely(!flow)) { |
946 | rcu_assign_pointer(new_flow->sf_acts, acts); | |
ccb1352e JG |
947 | |
948 | /* Put flow in bucket. */ | |
893f139b JR |
949 | error = ovs_flow_tbl_insert(&dp->table, new_flow, &mask); |
950 | if (unlikely(error)) { | |
618ed0c8 | 951 | acts = NULL; |
893f139b JR |
952 | goto err_unlock_ovs; |
953 | } | |
954 | ||
955 | if (unlikely(reply)) { | |
956 | error = ovs_flow_cmd_fill_info(new_flow, | |
957 | ovs_header->dp_ifindex, | |
958 | reply, info->snd_portid, | |
959 | info->snd_seq, 0, | |
74ed7ab9 JS |
960 | OVS_FLOW_CMD_NEW, |
961 | ufid_flags); | |
893f139b | 962 | BUG_ON(error < 0); |
618ed0c8 | 963 | } |
893f139b | 964 | ovs_unlock(); |
ccb1352e | 965 | } else { |
37bdc87b JR |
966 | struct sw_flow_actions *old_acts; |
967 | ||
ccb1352e JG |
968 | /* Bail out if we're not allowed to modify an existing flow. |
969 | * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL | |
970 | * because Generic Netlink treats the latter as a dump | |
971 | * request. We also accept NLM_F_EXCL in case that bug ever | |
972 | * gets fixed. | |
973 | */ | |
893f139b JR |
974 | if (unlikely(info->nlhdr->nlmsg_flags & (NLM_F_CREATE |
975 | | NLM_F_EXCL))) { | |
976 | error = -EEXIST; | |
8e4e1713 | 977 | goto err_unlock_ovs; |
893f139b | 978 | } |
74ed7ab9 JS |
979 | /* The flow identifier has to be the same for flow updates. |
980 | * Look for any overlapping flow. | |
981 | */ | |
982 | if (unlikely(!ovs_flow_cmp(flow, &match))) { | |
983 | if (ovs_identifier_is_key(&flow->id)) | |
984 | flow = ovs_flow_tbl_lookup_exact(&dp->table, | |
985 | &match); | |
986 | else /* UFID matches but key is different */ | |
987 | flow = NULL; | |
4a46b24e AW |
988 | if (!flow) { |
989 | error = -ENOENT; | |
990 | goto err_unlock_ovs; | |
991 | } | |
893f139b | 992 | } |
37bdc87b JR |
993 | /* Update actions. */ |
994 | old_acts = ovsl_dereference(flow->sf_acts); | |
995 | rcu_assign_pointer(flow->sf_acts, acts); | |
37bdc87b | 996 | |
893f139b JR |
997 | if (unlikely(reply)) { |
998 | error = ovs_flow_cmd_fill_info(flow, | |
999 | ovs_header->dp_ifindex, | |
1000 | reply, info->snd_portid, | |
1001 | info->snd_seq, 0, | |
74ed7ab9 JS |
1002 | OVS_FLOW_CMD_NEW, |
1003 | ufid_flags); | |
893f139b JR |
1004 | BUG_ON(error < 0); |
1005 | } | |
1006 | ovs_unlock(); | |
37bdc87b | 1007 | |
893f139b JR |
1008 | ovs_nla_free_flow_actions(old_acts); |
1009 | ovs_flow_free(new_flow, false); | |
37bdc87b | 1010 | } |
893f139b JR |
1011 | |
1012 | if (reply) | |
1013 | ovs_notify(&dp_flow_genl_family, reply, info); | |
37bdc87b JR |
1014 | return 0; |
1015 | ||
37bdc87b JR |
1016 | err_unlock_ovs: |
1017 | ovs_unlock(); | |
893f139b JR |
1018 | kfree_skb(reply); |
1019 | err_kfree_acts: | |
37bdc87b | 1020 | kfree(acts); |
893f139b JR |
1021 | err_kfree_flow: |
1022 | ovs_flow_free(new_flow, false); | |
37bdc87b JR |
1023 | error: |
1024 | return error; | |
1025 | } | |
ccb1352e | 1026 | |
2fdb957d | 1027 | /* Factor out action copy to avoid "Wframe-larger-than=1024" warning. */ |
6b205b2c JG |
1028 | static struct sw_flow_actions *get_flow_actions(const struct nlattr *a, |
1029 | const struct sw_flow_key *key, | |
05da5898 JR |
1030 | const struct sw_flow_mask *mask, |
1031 | bool log) | |
6b205b2c JG |
1032 | { |
1033 | struct sw_flow_actions *acts; | |
1034 | struct sw_flow_key masked_key; | |
1035 | int error; | |
1036 | ||
6b205b2c | 1037 | ovs_flow_mask_key(&masked_key, key, mask); |
05da5898 | 1038 | error = ovs_nla_copy_actions(a, &masked_key, &acts, log); |
6b205b2c | 1039 | if (error) { |
05da5898 JR |
1040 | OVS_NLERR(log, |
1041 | "Actions may not be safe on all matching packets"); | |
6b205b2c JG |
1042 | return ERR_PTR(error); |
1043 | } | |
1044 | ||
1045 | return acts; | |
1046 | } | |
1047 | ||
37bdc87b JR |
1048 | static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info) |
1049 | { | |
1050 | struct nlattr **a = info->attrs; | |
1051 | struct ovs_header *ovs_header = info->userhdr; | |
6b205b2c | 1052 | struct sw_flow_key key; |
37bdc87b JR |
1053 | struct sw_flow *flow; |
1054 | struct sw_flow_mask mask; | |
1055 | struct sk_buff *reply = NULL; | |
1056 | struct datapath *dp; | |
893f139b | 1057 | struct sw_flow_actions *old_acts = NULL, *acts = NULL; |
37bdc87b | 1058 | struct sw_flow_match match; |
74ed7ab9 JS |
1059 | struct sw_flow_id sfid; |
1060 | u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]); | |
37bdc87b | 1061 | int error; |
05da5898 | 1062 | bool log = !a[OVS_FLOW_ATTR_PROBE]; |
74ed7ab9 | 1063 | bool ufid_present; |
37bdc87b JR |
1064 | |
1065 | /* Extract key. */ | |
1066 | error = -EINVAL; | |
426cda5c | 1067 | if (!a[OVS_FLOW_ATTR_KEY]) { |
05da5898 | 1068 | OVS_NLERR(log, "Flow key attribute not present in set flow."); |
37bdc87b | 1069 | goto error; |
426cda5c | 1070 | } |
37bdc87b | 1071 | |
74ed7ab9 | 1072 | ufid_present = ovs_nla_get_ufid(&sfid, a[OVS_FLOW_ATTR_UFID], log); |
37bdc87b | 1073 | ovs_match_init(&match, &key, &mask); |
05da5898 JR |
1074 | error = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], |
1075 | a[OVS_FLOW_ATTR_MASK], log); | |
37bdc87b JR |
1076 | if (error) |
1077 | goto error; | |
1078 | ||
1079 | /* Validate actions. */ | |
1080 | if (a[OVS_FLOW_ATTR_ACTIONS]) { | |
05da5898 JR |
1081 | acts = get_flow_actions(a[OVS_FLOW_ATTR_ACTIONS], &key, &mask, |
1082 | log); | |
6b205b2c JG |
1083 | if (IS_ERR(acts)) { |
1084 | error = PTR_ERR(acts); | |
37bdc87b | 1085 | goto error; |
893f139b | 1086 | } |
893f139b | 1087 | |
2fdb957d | 1088 | /* Can allocate before locking if have acts. */ |
74ed7ab9 JS |
1089 | reply = ovs_flow_cmd_alloc_info(acts, &sfid, info, false, |
1090 | ufid_flags); | |
893f139b JR |
1091 | if (IS_ERR(reply)) { |
1092 | error = PTR_ERR(reply); | |
1093 | goto err_kfree_acts; | |
be52c9e9 | 1094 | } |
37bdc87b | 1095 | } |
0e9796b4 | 1096 | |
37bdc87b JR |
1097 | ovs_lock(); |
1098 | dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex); | |
893f139b JR |
1099 | if (unlikely(!dp)) { |
1100 | error = -ENODEV; | |
37bdc87b | 1101 | goto err_unlock_ovs; |
893f139b | 1102 | } |
37bdc87b | 1103 | /* Check that the flow exists. */ |
74ed7ab9 JS |
1104 | if (ufid_present) |
1105 | flow = ovs_flow_tbl_lookup_ufid(&dp->table, &sfid); | |
1106 | else | |
1107 | flow = ovs_flow_tbl_lookup_exact(&dp->table, &match); | |
893f139b JR |
1108 | if (unlikely(!flow)) { |
1109 | error = -ENOENT; | |
37bdc87b | 1110 | goto err_unlock_ovs; |
893f139b | 1111 | } |
4a46b24e | 1112 | |
37bdc87b | 1113 | /* Update actions, if present. */ |
893f139b | 1114 | if (likely(acts)) { |
37bdc87b JR |
1115 | old_acts = ovsl_dereference(flow->sf_acts); |
1116 | rcu_assign_pointer(flow->sf_acts, acts); | |
893f139b JR |
1117 | |
1118 | if (unlikely(reply)) { | |
1119 | error = ovs_flow_cmd_fill_info(flow, | |
1120 | ovs_header->dp_ifindex, | |
1121 | reply, info->snd_portid, | |
1122 | info->snd_seq, 0, | |
74ed7ab9 JS |
1123 | OVS_FLOW_CMD_NEW, |
1124 | ufid_flags); | |
893f139b JR |
1125 | BUG_ON(error < 0); |
1126 | } | |
1127 | } else { | |
1128 | /* Could not alloc without acts before locking. */ | |
1129 | reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex, | |
74ed7ab9 JS |
1130 | info, OVS_FLOW_CMD_NEW, false, |
1131 | ufid_flags); | |
1132 | ||
893f139b JR |
1133 | if (unlikely(IS_ERR(reply))) { |
1134 | error = PTR_ERR(reply); | |
1135 | goto err_unlock_ovs; | |
1136 | } | |
ccb1352e | 1137 | } |
37bdc87b | 1138 | |
37bdc87b JR |
1139 | /* Clear stats. */ |
1140 | if (a[OVS_FLOW_ATTR_CLEAR]) | |
1141 | ovs_flow_stats_clear(flow); | |
8e4e1713 | 1142 | ovs_unlock(); |
ccb1352e | 1143 | |
893f139b JR |
1144 | if (reply) |
1145 | ovs_notify(&dp_flow_genl_family, reply, info); | |
1146 | if (old_acts) | |
1147 | ovs_nla_free_flow_actions(old_acts); | |
fb5d1e9e | 1148 | |
ccb1352e JG |
1149 | return 0; |
1150 | ||
8e4e1713 PS |
1151 | err_unlock_ovs: |
1152 | ovs_unlock(); | |
893f139b JR |
1153 | kfree_skb(reply); |
1154 | err_kfree_acts: | |
74f84a57 | 1155 | kfree(acts); |
ccb1352e JG |
1156 | error: |
1157 | return error; | |
1158 | } | |
1159 | ||
1160 | static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info) | |
1161 | { | |
1162 | struct nlattr **a = info->attrs; | |
1163 | struct ovs_header *ovs_header = info->userhdr; | |
1164 | struct sw_flow_key key; | |
1165 | struct sk_buff *reply; | |
1166 | struct sw_flow *flow; | |
1167 | struct datapath *dp; | |
03f0d916 | 1168 | struct sw_flow_match match; |
74ed7ab9 JS |
1169 | struct sw_flow_id ufid; |
1170 | u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]); | |
1171 | int err = 0; | |
05da5898 | 1172 | bool log = !a[OVS_FLOW_ATTR_PROBE]; |
74ed7ab9 | 1173 | bool ufid_present; |
ccb1352e | 1174 | |
74ed7ab9 JS |
1175 | ufid_present = ovs_nla_get_ufid(&ufid, a[OVS_FLOW_ATTR_UFID], log); |
1176 | if (a[OVS_FLOW_ATTR_KEY]) { | |
1177 | ovs_match_init(&match, &key, NULL); | |
1178 | err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL, | |
1179 | log); | |
1180 | } else if (!ufid_present) { | |
05da5898 JR |
1181 | OVS_NLERR(log, |
1182 | "Flow get message rejected, Key attribute missing."); | |
74ed7ab9 | 1183 | err = -EINVAL; |
03f0d916 | 1184 | } |
ccb1352e JG |
1185 | if (err) |
1186 | return err; | |
1187 | ||
8e4e1713 | 1188 | ovs_lock(); |
46df7b81 | 1189 | dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex); |
8e4e1713 PS |
1190 | if (!dp) { |
1191 | err = -ENODEV; | |
1192 | goto unlock; | |
1193 | } | |
ccb1352e | 1194 | |
74ed7ab9 JS |
1195 | if (ufid_present) |
1196 | flow = ovs_flow_tbl_lookup_ufid(&dp->table, &ufid); | |
1197 | else | |
1198 | flow = ovs_flow_tbl_lookup_exact(&dp->table, &match); | |
4a46b24e | 1199 | if (!flow) { |
8e4e1713 PS |
1200 | err = -ENOENT; |
1201 | goto unlock; | |
1202 | } | |
ccb1352e | 1203 | |
0e9796b4 | 1204 | reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex, info, |
74ed7ab9 | 1205 | OVS_FLOW_CMD_NEW, true, ufid_flags); |
8e4e1713 PS |
1206 | if (IS_ERR(reply)) { |
1207 | err = PTR_ERR(reply); | |
1208 | goto unlock; | |
1209 | } | |
ccb1352e | 1210 | |
8e4e1713 | 1211 | ovs_unlock(); |
ccb1352e | 1212 | return genlmsg_reply(reply, info); |
8e4e1713 PS |
1213 | unlock: |
1214 | ovs_unlock(); | |
1215 | return err; | |
ccb1352e JG |
1216 | } |
1217 | ||
1218 | static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info) | |
1219 | { | |
1220 | struct nlattr **a = info->attrs; | |
1221 | struct ovs_header *ovs_header = info->userhdr; | |
1222 | struct sw_flow_key key; | |
1223 | struct sk_buff *reply; | |
74ed7ab9 | 1224 | struct sw_flow *flow = NULL; |
ccb1352e | 1225 | struct datapath *dp; |
03f0d916 | 1226 | struct sw_flow_match match; |
74ed7ab9 JS |
1227 | struct sw_flow_id ufid; |
1228 | u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]); | |
ccb1352e | 1229 | int err; |
05da5898 | 1230 | bool log = !a[OVS_FLOW_ATTR_PROBE]; |
74ed7ab9 | 1231 | bool ufid_present; |
ccb1352e | 1232 | |
74ed7ab9 JS |
1233 | ufid_present = ovs_nla_get_ufid(&ufid, a[OVS_FLOW_ATTR_UFID], log); |
1234 | if (a[OVS_FLOW_ATTR_KEY]) { | |
aed06778 | 1235 | ovs_match_init(&match, &key, NULL); |
05da5898 JR |
1236 | err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL, |
1237 | log); | |
aed06778 JR |
1238 | if (unlikely(err)) |
1239 | return err; | |
1240 | } | |
1241 | ||
8e4e1713 | 1242 | ovs_lock(); |
46df7b81 | 1243 | dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex); |
aed06778 | 1244 | if (unlikely(!dp)) { |
8e4e1713 PS |
1245 | err = -ENODEV; |
1246 | goto unlock; | |
1247 | } | |
46df7b81 | 1248 | |
74ed7ab9 | 1249 | if (unlikely(!a[OVS_FLOW_ATTR_KEY] && !ufid_present)) { |
b637e498 | 1250 | err = ovs_flow_tbl_flush(&dp->table); |
8e4e1713 PS |
1251 | goto unlock; |
1252 | } | |
03f0d916 | 1253 | |
74ed7ab9 JS |
1254 | if (ufid_present) |
1255 | flow = ovs_flow_tbl_lookup_ufid(&dp->table, &ufid); | |
1256 | else | |
1257 | flow = ovs_flow_tbl_lookup_exact(&dp->table, &match); | |
4a46b24e | 1258 | if (unlikely(!flow)) { |
8e4e1713 PS |
1259 | err = -ENOENT; |
1260 | goto unlock; | |
1261 | } | |
ccb1352e | 1262 | |
b637e498 | 1263 | ovs_flow_tbl_remove(&dp->table, flow); |
aed06778 | 1264 | ovs_unlock(); |
ccb1352e | 1265 | |
aed06778 | 1266 | reply = ovs_flow_cmd_alloc_info((const struct sw_flow_actions __force *) flow->sf_acts, |
74ed7ab9 | 1267 | &flow->id, info, false, ufid_flags); |
aed06778 JR |
1268 | if (likely(reply)) { |
1269 | if (likely(!IS_ERR(reply))) { | |
1270 | rcu_read_lock(); /*To keep RCU checker happy. */ | |
1271 | err = ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex, | |
1272 | reply, info->snd_portid, | |
1273 | info->snd_seq, 0, | |
74ed7ab9 JS |
1274 | OVS_FLOW_CMD_DEL, |
1275 | ufid_flags); | |
aed06778 JR |
1276 | rcu_read_unlock(); |
1277 | BUG_ON(err < 0); | |
1278 | ||
1279 | ovs_notify(&dp_flow_genl_family, reply, info); | |
1280 | } else { | |
1281 | netlink_set_err(sock_net(skb->sk)->genl_sock, 0, 0, PTR_ERR(reply)); | |
1282 | } | |
fb5d1e9e | 1283 | } |
ccb1352e | 1284 | |
aed06778 | 1285 | ovs_flow_free(flow, true); |
ccb1352e | 1286 | return 0; |
8e4e1713 PS |
1287 | unlock: |
1288 | ovs_unlock(); | |
1289 | return err; | |
ccb1352e JG |
1290 | } |
1291 | ||
1292 | static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb) | |
1293 | { | |
74ed7ab9 | 1294 | struct nlattr *a[__OVS_FLOW_ATTR_MAX]; |
ccb1352e | 1295 | struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh)); |
b637e498 | 1296 | struct table_instance *ti; |
ccb1352e | 1297 | struct datapath *dp; |
74ed7ab9 JS |
1298 | u32 ufid_flags; |
1299 | int err; | |
1300 | ||
1301 | err = genlmsg_parse(cb->nlh, &dp_flow_genl_family, a, | |
1302 | OVS_FLOW_ATTR_MAX, flow_policy); | |
1303 | if (err) | |
1304 | return err; | |
1305 | ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]); | |
ccb1352e | 1306 | |
d57170b1 | 1307 | rcu_read_lock(); |
cc3a5ae6 | 1308 | dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex); |
8e4e1713 | 1309 | if (!dp) { |
d57170b1 | 1310 | rcu_read_unlock(); |
ccb1352e | 1311 | return -ENODEV; |
8e4e1713 | 1312 | } |
ccb1352e | 1313 | |
b637e498 | 1314 | ti = rcu_dereference(dp->table.ti); |
ccb1352e JG |
1315 | for (;;) { |
1316 | struct sw_flow *flow; | |
1317 | u32 bucket, obj; | |
1318 | ||
1319 | bucket = cb->args[0]; | |
1320 | obj = cb->args[1]; | |
b637e498 | 1321 | flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj); |
ccb1352e JG |
1322 | if (!flow) |
1323 | break; | |
1324 | ||
0e9796b4 | 1325 | if (ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex, skb, |
15e47304 | 1326 | NETLINK_CB(cb->skb).portid, |
ccb1352e | 1327 | cb->nlh->nlmsg_seq, NLM_F_MULTI, |
74ed7ab9 | 1328 | OVS_FLOW_CMD_NEW, ufid_flags) < 0) |
ccb1352e JG |
1329 | break; |
1330 | ||
1331 | cb->args[0] = bucket; | |
1332 | cb->args[1] = obj; | |
1333 | } | |
d57170b1 | 1334 | rcu_read_unlock(); |
ccb1352e JG |
1335 | return skb->len; |
1336 | } | |
1337 | ||
0c200ef9 PS |
1338 | static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = { |
1339 | [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED }, | |
05da5898 | 1340 | [OVS_FLOW_ATTR_MASK] = { .type = NLA_NESTED }, |
0c200ef9 PS |
1341 | [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED }, |
1342 | [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG }, | |
05da5898 | 1343 | [OVS_FLOW_ATTR_PROBE] = { .type = NLA_FLAG }, |
74ed7ab9 JS |
1344 | [OVS_FLOW_ATTR_UFID] = { .type = NLA_UNSPEC, .len = 1 }, |
1345 | [OVS_FLOW_ATTR_UFID_FLAGS] = { .type = NLA_U32 }, | |
0c200ef9 PS |
1346 | }; |
1347 | ||
48e48a70 | 1348 | static const struct genl_ops dp_flow_genl_ops[] = { |
ccb1352e JG |
1349 | { .cmd = OVS_FLOW_CMD_NEW, |
1350 | .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ | |
1351 | .policy = flow_policy, | |
37bdc87b | 1352 | .doit = ovs_flow_cmd_new |
ccb1352e JG |
1353 | }, |
1354 | { .cmd = OVS_FLOW_CMD_DEL, | |
1355 | .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ | |
1356 | .policy = flow_policy, | |
1357 | .doit = ovs_flow_cmd_del | |
1358 | }, | |
1359 | { .cmd = OVS_FLOW_CMD_GET, | |
1360 | .flags = 0, /* OK for unprivileged users. */ | |
1361 | .policy = flow_policy, | |
1362 | .doit = ovs_flow_cmd_get, | |
1363 | .dumpit = ovs_flow_cmd_dump | |
1364 | }, | |
1365 | { .cmd = OVS_FLOW_CMD_SET, | |
1366 | .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ | |
1367 | .policy = flow_policy, | |
37bdc87b | 1368 | .doit = ovs_flow_cmd_set, |
ccb1352e JG |
1369 | }, |
1370 | }; | |
1371 | ||
0c200ef9 | 1372 | static struct genl_family dp_flow_genl_family = { |
ccb1352e JG |
1373 | .id = GENL_ID_GENERATE, |
1374 | .hdrsize = sizeof(struct ovs_header), | |
0c200ef9 PS |
1375 | .name = OVS_FLOW_FAMILY, |
1376 | .version = OVS_FLOW_VERSION, | |
1377 | .maxattr = OVS_FLOW_ATTR_MAX, | |
3a4e0d6a PS |
1378 | .netnsok = true, |
1379 | .parallel_ops = true, | |
0c200ef9 PS |
1380 | .ops = dp_flow_genl_ops, |
1381 | .n_ops = ARRAY_SIZE(dp_flow_genl_ops), | |
1382 | .mcgrps = &ovs_dp_flow_multicast_group, | |
1383 | .n_mcgrps = 1, | |
ccb1352e JG |
1384 | }; |
1385 | ||
c3ff8cfe TG |
1386 | static size_t ovs_dp_cmd_msg_size(void) |
1387 | { | |
1388 | size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header)); | |
1389 | ||
1390 | msgsize += nla_total_size(IFNAMSIZ); | |
1391 | msgsize += nla_total_size(sizeof(struct ovs_dp_stats)); | |
1bd7116f | 1392 | msgsize += nla_total_size(sizeof(struct ovs_dp_megaflow_stats)); |
45fb9c35 | 1393 | msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_USER_FEATURES */ |
c3ff8cfe TG |
1394 | |
1395 | return msgsize; | |
1396 | } | |
1397 | ||
8ec609d8 | 1398 | /* Called with ovs_mutex. */ |
ccb1352e | 1399 | static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb, |
15e47304 | 1400 | u32 portid, u32 seq, u32 flags, u8 cmd) |
ccb1352e JG |
1401 | { |
1402 | struct ovs_header *ovs_header; | |
1403 | struct ovs_dp_stats dp_stats; | |
1bd7116f | 1404 | struct ovs_dp_megaflow_stats dp_megaflow_stats; |
ccb1352e JG |
1405 | int err; |
1406 | ||
15e47304 | 1407 | ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family, |
ccb1352e JG |
1408 | flags, cmd); |
1409 | if (!ovs_header) | |
1410 | goto error; | |
1411 | ||
1412 | ovs_header->dp_ifindex = get_dpifindex(dp); | |
1413 | ||
ccb1352e | 1414 | err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp)); |
ccb1352e JG |
1415 | if (err) |
1416 | goto nla_put_failure; | |
1417 | ||
1bd7116f AZ |
1418 | get_dp_stats(dp, &dp_stats, &dp_megaflow_stats); |
1419 | if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats), | |
1420 | &dp_stats)) | |
1421 | goto nla_put_failure; | |
1422 | ||
1423 | if (nla_put(skb, OVS_DP_ATTR_MEGAFLOW_STATS, | |
1424 | sizeof(struct ovs_dp_megaflow_stats), | |
1425 | &dp_megaflow_stats)) | |
028d6a67 | 1426 | goto nla_put_failure; |
ccb1352e | 1427 | |
43d4be9c TG |
1428 | if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, dp->user_features)) |
1429 | goto nla_put_failure; | |
1430 | ||
053c095a JB |
1431 | genlmsg_end(skb, ovs_header); |
1432 | return 0; | |
ccb1352e JG |
1433 | |
1434 | nla_put_failure: | |
1435 | genlmsg_cancel(skb, ovs_header); | |
1436 | error: | |
1437 | return -EMSGSIZE; | |
1438 | } | |
1439 | ||
6093ae9a | 1440 | static struct sk_buff *ovs_dp_cmd_alloc_info(struct genl_info *info) |
ccb1352e | 1441 | { |
6093ae9a | 1442 | return genlmsg_new_unicast(ovs_dp_cmd_msg_size(), info, GFP_KERNEL); |
ccb1352e JG |
1443 | } |
1444 | ||
bb6f9a70 | 1445 | /* Called with rcu_read_lock or ovs_mutex. */ |
46df7b81 | 1446 | static struct datapath *lookup_datapath(struct net *net, |
12eb18f7 | 1447 | const struct ovs_header *ovs_header, |
ccb1352e JG |
1448 | struct nlattr *a[OVS_DP_ATTR_MAX + 1]) |
1449 | { | |
1450 | struct datapath *dp; | |
1451 | ||
1452 | if (!a[OVS_DP_ATTR_NAME]) | |
46df7b81 | 1453 | dp = get_dp(net, ovs_header->dp_ifindex); |
ccb1352e JG |
1454 | else { |
1455 | struct vport *vport; | |
1456 | ||
46df7b81 | 1457 | vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME])); |
ccb1352e | 1458 | dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL; |
ccb1352e JG |
1459 | } |
1460 | return dp ? dp : ERR_PTR(-ENODEV); | |
1461 | } | |
1462 | ||
44da5ae5 TG |
1463 | static void ovs_dp_reset_user_features(struct sk_buff *skb, struct genl_info *info) |
1464 | { | |
1465 | struct datapath *dp; | |
1466 | ||
1467 | dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs); | |
3c7eacfc | 1468 | if (IS_ERR(dp)) |
44da5ae5 TG |
1469 | return; |
1470 | ||
1471 | WARN(dp->user_features, "Dropping previously announced user features\n"); | |
1472 | dp->user_features = 0; | |
1473 | } | |
1474 | ||
12eb18f7 | 1475 | static void ovs_dp_change(struct datapath *dp, struct nlattr *a[]) |
43d4be9c TG |
1476 | { |
1477 | if (a[OVS_DP_ATTR_USER_FEATURES]) | |
1478 | dp->user_features = nla_get_u32(a[OVS_DP_ATTR_USER_FEATURES]); | |
1479 | } | |
1480 | ||
ccb1352e JG |
1481 | static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info) |
1482 | { | |
1483 | struct nlattr **a = info->attrs; | |
1484 | struct vport_parms parms; | |
1485 | struct sk_buff *reply; | |
1486 | struct datapath *dp; | |
1487 | struct vport *vport; | |
46df7b81 | 1488 | struct ovs_net *ovs_net; |
15eac2a7 | 1489 | int err, i; |
ccb1352e JG |
1490 | |
1491 | err = -EINVAL; | |
1492 | if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID]) | |
1493 | goto err; | |
1494 | ||
6093ae9a JR |
1495 | reply = ovs_dp_cmd_alloc_info(info); |
1496 | if (!reply) | |
1497 | return -ENOMEM; | |
ccb1352e JG |
1498 | |
1499 | err = -ENOMEM; | |
1500 | dp = kzalloc(sizeof(*dp), GFP_KERNEL); | |
1501 | if (dp == NULL) | |
6093ae9a | 1502 | goto err_free_reply; |
46df7b81 | 1503 | |
46df7b81 | 1504 | ovs_dp_set_net(dp, hold_net(sock_net(skb->sk))); |
ccb1352e JG |
1505 | |
1506 | /* Allocate table. */ | |
b637e498 PS |
1507 | err = ovs_flow_tbl_init(&dp->table); |
1508 | if (err) | |
ccb1352e JG |
1509 | goto err_free_dp; |
1510 | ||
1c213bd2 | 1511 | dp->stats_percpu = netdev_alloc_pcpu_stats(struct dp_stats_percpu); |
ccb1352e JG |
1512 | if (!dp->stats_percpu) { |
1513 | err = -ENOMEM; | |
1514 | goto err_destroy_table; | |
1515 | } | |
1516 | ||
15eac2a7 | 1517 | dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head), |
e6445719 | 1518 | GFP_KERNEL); |
15eac2a7 PS |
1519 | if (!dp->ports) { |
1520 | err = -ENOMEM; | |
1521 | goto err_destroy_percpu; | |
1522 | } | |
1523 | ||
1524 | for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) | |
1525 | INIT_HLIST_HEAD(&dp->ports[i]); | |
1526 | ||
ccb1352e JG |
1527 | /* Set up our datapath device. */ |
1528 | parms.name = nla_data(a[OVS_DP_ATTR_NAME]); | |
1529 | parms.type = OVS_VPORT_TYPE_INTERNAL; | |
1530 | parms.options = NULL; | |
1531 | parms.dp = dp; | |
1532 | parms.port_no = OVSP_LOCAL; | |
5cd667b0 | 1533 | parms.upcall_portids = a[OVS_DP_ATTR_UPCALL_PID]; |
ccb1352e | 1534 | |
43d4be9c TG |
1535 | ovs_dp_change(dp, a); |
1536 | ||
6093ae9a JR |
1537 | /* So far only local changes have been made, now need the lock. */ |
1538 | ovs_lock(); | |
1539 | ||
ccb1352e JG |
1540 | vport = new_vport(&parms); |
1541 | if (IS_ERR(vport)) { | |
1542 | err = PTR_ERR(vport); | |
1543 | if (err == -EBUSY) | |
1544 | err = -EEXIST; | |
1545 | ||
44da5ae5 TG |
1546 | if (err == -EEXIST) { |
1547 | /* An outdated user space instance that does not understand | |
1548 | * the concept of user_features has attempted to create a new | |
1549 | * datapath and is likely to reuse it. Drop all user features. | |
1550 | */ | |
1551 | if (info->genlhdr->version < OVS_DP_VER_FEATURES) | |
1552 | ovs_dp_reset_user_features(skb, info); | |
1553 | } | |
1554 | ||
15eac2a7 | 1555 | goto err_destroy_ports_array; |
ccb1352e JG |
1556 | } |
1557 | ||
6093ae9a JR |
1558 | err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid, |
1559 | info->snd_seq, 0, OVS_DP_CMD_NEW); | |
1560 | BUG_ON(err < 0); | |
ccb1352e | 1561 | |
46df7b81 | 1562 | ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id); |
59a35d60 | 1563 | list_add_tail_rcu(&dp->list_node, &ovs_net->dps); |
8e4e1713 PS |
1564 | |
1565 | ovs_unlock(); | |
ccb1352e | 1566 | |
2a94fe48 | 1567 | ovs_notify(&dp_datapath_genl_family, reply, info); |
ccb1352e JG |
1568 | return 0; |
1569 | ||
15eac2a7 | 1570 | err_destroy_ports_array: |
6093ae9a | 1571 | ovs_unlock(); |
15eac2a7 | 1572 | kfree(dp->ports); |
ccb1352e JG |
1573 | err_destroy_percpu: |
1574 | free_percpu(dp->stats_percpu); | |
1575 | err_destroy_table: | |
9b996e54 | 1576 | ovs_flow_tbl_destroy(&dp->table); |
ccb1352e | 1577 | err_free_dp: |
46df7b81 | 1578 | release_net(ovs_dp_get_net(dp)); |
ccb1352e | 1579 | kfree(dp); |
6093ae9a JR |
1580 | err_free_reply: |
1581 | kfree_skb(reply); | |
ccb1352e JG |
1582 | err: |
1583 | return err; | |
1584 | } | |
1585 | ||
8e4e1713 | 1586 | /* Called with ovs_mutex. */ |
46df7b81 | 1587 | static void __dp_destroy(struct datapath *dp) |
ccb1352e | 1588 | { |
15eac2a7 | 1589 | int i; |
ccb1352e | 1590 | |
15eac2a7 PS |
1591 | for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) { |
1592 | struct vport *vport; | |
b67bfe0d | 1593 | struct hlist_node *n; |
15eac2a7 | 1594 | |
b67bfe0d | 1595 | hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node) |
15eac2a7 PS |
1596 | if (vport->port_no != OVSP_LOCAL) |
1597 | ovs_dp_detach_port(vport); | |
1598 | } | |
ccb1352e | 1599 | |
59a35d60 | 1600 | list_del_rcu(&dp->list_node); |
ccb1352e | 1601 | |
8e4e1713 | 1602 | /* OVSP_LOCAL is datapath internal port. We need to make sure that |
e80857cc | 1603 | * all ports in datapath are destroyed first before freeing datapath. |
ccb1352e | 1604 | */ |
8e4e1713 | 1605 | ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL)); |
ccb1352e | 1606 | |
e80857cc | 1607 | /* RCU destroy the flow table */ |
ccb1352e | 1608 | call_rcu(&dp->rcu, destroy_dp_rcu); |
46df7b81 PS |
1609 | } |
1610 | ||
1611 | static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info) | |
1612 | { | |
1613 | struct sk_buff *reply; | |
1614 | struct datapath *dp; | |
1615 | int err; | |
1616 | ||
6093ae9a JR |
1617 | reply = ovs_dp_cmd_alloc_info(info); |
1618 | if (!reply) | |
1619 | return -ENOMEM; | |
1620 | ||
8e4e1713 | 1621 | ovs_lock(); |
46df7b81 PS |
1622 | dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs); |
1623 | err = PTR_ERR(dp); | |
1624 | if (IS_ERR(dp)) | |
6093ae9a | 1625 | goto err_unlock_free; |
46df7b81 | 1626 | |
6093ae9a JR |
1627 | err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid, |
1628 | info->snd_seq, 0, OVS_DP_CMD_DEL); | |
1629 | BUG_ON(err < 0); | |
46df7b81 PS |
1630 | |
1631 | __dp_destroy(dp); | |
8e4e1713 | 1632 | ovs_unlock(); |
ccb1352e | 1633 | |
2a94fe48 | 1634 | ovs_notify(&dp_datapath_genl_family, reply, info); |
ccb1352e JG |
1635 | |
1636 | return 0; | |
6093ae9a JR |
1637 | |
1638 | err_unlock_free: | |
8e4e1713 | 1639 | ovs_unlock(); |
6093ae9a | 1640 | kfree_skb(reply); |
8e4e1713 | 1641 | return err; |
ccb1352e JG |
1642 | } |
1643 | ||
1644 | static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info) | |
1645 | { | |
1646 | struct sk_buff *reply; | |
1647 | struct datapath *dp; | |
1648 | int err; | |
1649 | ||
6093ae9a JR |
1650 | reply = ovs_dp_cmd_alloc_info(info); |
1651 | if (!reply) | |
1652 | return -ENOMEM; | |
1653 | ||
8e4e1713 | 1654 | ovs_lock(); |
46df7b81 | 1655 | dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs); |
8e4e1713 | 1656 | err = PTR_ERR(dp); |
ccb1352e | 1657 | if (IS_ERR(dp)) |
6093ae9a | 1658 | goto err_unlock_free; |
ccb1352e | 1659 | |
43d4be9c TG |
1660 | ovs_dp_change(dp, info->attrs); |
1661 | ||
6093ae9a JR |
1662 | err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid, |
1663 | info->snd_seq, 0, OVS_DP_CMD_NEW); | |
1664 | BUG_ON(err < 0); | |
ccb1352e | 1665 | |
8e4e1713 | 1666 | ovs_unlock(); |
2a94fe48 | 1667 | ovs_notify(&dp_datapath_genl_family, reply, info); |
ccb1352e JG |
1668 | |
1669 | return 0; | |
6093ae9a JR |
1670 | |
1671 | err_unlock_free: | |
8e4e1713 | 1672 | ovs_unlock(); |
6093ae9a | 1673 | kfree_skb(reply); |
8e4e1713 | 1674 | return err; |
ccb1352e JG |
1675 | } |
1676 | ||
1677 | static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info) | |
1678 | { | |
1679 | struct sk_buff *reply; | |
1680 | struct datapath *dp; | |
8e4e1713 | 1681 | int err; |
ccb1352e | 1682 | |
6093ae9a JR |
1683 | reply = ovs_dp_cmd_alloc_info(info); |
1684 | if (!reply) | |
1685 | return -ENOMEM; | |
1686 | ||
8ec609d8 | 1687 | ovs_lock(); |
46df7b81 | 1688 | dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs); |
8e4e1713 PS |
1689 | if (IS_ERR(dp)) { |
1690 | err = PTR_ERR(dp); | |
6093ae9a | 1691 | goto err_unlock_free; |
8e4e1713 | 1692 | } |
6093ae9a JR |
1693 | err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid, |
1694 | info->snd_seq, 0, OVS_DP_CMD_NEW); | |
1695 | BUG_ON(err < 0); | |
8ec609d8 | 1696 | ovs_unlock(); |
ccb1352e JG |
1697 | |
1698 | return genlmsg_reply(reply, info); | |
8e4e1713 | 1699 | |
6093ae9a | 1700 | err_unlock_free: |
8ec609d8 | 1701 | ovs_unlock(); |
6093ae9a | 1702 | kfree_skb(reply); |
8e4e1713 | 1703 | return err; |
ccb1352e JG |
1704 | } |
1705 | ||
1706 | static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb) | |
1707 | { | |
46df7b81 | 1708 | struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id); |
ccb1352e JG |
1709 | struct datapath *dp; |
1710 | int skip = cb->args[0]; | |
1711 | int i = 0; | |
1712 | ||
8ec609d8 PS |
1713 | ovs_lock(); |
1714 | list_for_each_entry(dp, &ovs_net->dps, list_node) { | |
77676fdb | 1715 | if (i >= skip && |
15e47304 | 1716 | ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid, |
ccb1352e JG |
1717 | cb->nlh->nlmsg_seq, NLM_F_MULTI, |
1718 | OVS_DP_CMD_NEW) < 0) | |
1719 | break; | |
1720 | i++; | |
1721 | } | |
8ec609d8 | 1722 | ovs_unlock(); |
ccb1352e JG |
1723 | |
1724 | cb->args[0] = i; | |
1725 | ||
1726 | return skb->len; | |
1727 | } | |
1728 | ||
0c200ef9 PS |
1729 | static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = { |
1730 | [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 }, | |
1731 | [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 }, | |
1732 | [OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 }, | |
1733 | }; | |
1734 | ||
48e48a70 | 1735 | static const struct genl_ops dp_datapath_genl_ops[] = { |
ccb1352e JG |
1736 | { .cmd = OVS_DP_CMD_NEW, |
1737 | .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ | |
1738 | .policy = datapath_policy, | |
1739 | .doit = ovs_dp_cmd_new | |
1740 | }, | |
1741 | { .cmd = OVS_DP_CMD_DEL, | |
1742 | .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ | |
1743 | .policy = datapath_policy, | |
1744 | .doit = ovs_dp_cmd_del | |
1745 | }, | |
1746 | { .cmd = OVS_DP_CMD_GET, | |
1747 | .flags = 0, /* OK for unprivileged users. */ | |
1748 | .policy = datapath_policy, | |
1749 | .doit = ovs_dp_cmd_get, | |
1750 | .dumpit = ovs_dp_cmd_dump | |
1751 | }, | |
1752 | { .cmd = OVS_DP_CMD_SET, | |
1753 | .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ | |
1754 | .policy = datapath_policy, | |
1755 | .doit = ovs_dp_cmd_set, | |
1756 | }, | |
1757 | }; | |
1758 | ||
0c200ef9 | 1759 | static struct genl_family dp_datapath_genl_family = { |
ccb1352e JG |
1760 | .id = GENL_ID_GENERATE, |
1761 | .hdrsize = sizeof(struct ovs_header), | |
0c200ef9 PS |
1762 | .name = OVS_DATAPATH_FAMILY, |
1763 | .version = OVS_DATAPATH_VERSION, | |
1764 | .maxattr = OVS_DP_ATTR_MAX, | |
3a4e0d6a PS |
1765 | .netnsok = true, |
1766 | .parallel_ops = true, | |
0c200ef9 PS |
1767 | .ops = dp_datapath_genl_ops, |
1768 | .n_ops = ARRAY_SIZE(dp_datapath_genl_ops), | |
1769 | .mcgrps = &ovs_dp_datapath_multicast_group, | |
1770 | .n_mcgrps = 1, | |
ccb1352e JG |
1771 | }; |
1772 | ||
8e4e1713 | 1773 | /* Called with ovs_mutex or RCU read lock. */ |
ccb1352e | 1774 | static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb, |
15e47304 | 1775 | u32 portid, u32 seq, u32 flags, u8 cmd) |
ccb1352e JG |
1776 | { |
1777 | struct ovs_header *ovs_header; | |
1778 | struct ovs_vport_stats vport_stats; | |
1779 | int err; | |
1780 | ||
15e47304 | 1781 | ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family, |
ccb1352e JG |
1782 | flags, cmd); |
1783 | if (!ovs_header) | |
1784 | return -EMSGSIZE; | |
1785 | ||
1786 | ovs_header->dp_ifindex = get_dpifindex(vport->dp); | |
1787 | ||
028d6a67 DM |
1788 | if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) || |
1789 | nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) || | |
5cd667b0 AW |
1790 | nla_put_string(skb, OVS_VPORT_ATTR_NAME, |
1791 | vport->ops->get_name(vport))) | |
028d6a67 | 1792 | goto nla_put_failure; |
ccb1352e JG |
1793 | |
1794 | ovs_vport_get_stats(vport, &vport_stats); | |
028d6a67 DM |
1795 | if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats), |
1796 | &vport_stats)) | |
1797 | goto nla_put_failure; | |
ccb1352e | 1798 | |
5cd667b0 AW |
1799 | if (ovs_vport_get_upcall_portids(vport, skb)) |
1800 | goto nla_put_failure; | |
1801 | ||
ccb1352e JG |
1802 | err = ovs_vport_get_options(vport, skb); |
1803 | if (err == -EMSGSIZE) | |
1804 | goto error; | |
1805 | ||
053c095a JB |
1806 | genlmsg_end(skb, ovs_header); |
1807 | return 0; | |
ccb1352e JG |
1808 | |
1809 | nla_put_failure: | |
1810 | err = -EMSGSIZE; | |
1811 | error: | |
1812 | genlmsg_cancel(skb, ovs_header); | |
1813 | return err; | |
1814 | } | |
1815 | ||
6093ae9a JR |
1816 | static struct sk_buff *ovs_vport_cmd_alloc_info(void) |
1817 | { | |
1818 | return nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); | |
1819 | } | |
1820 | ||
1821 | /* Called with ovs_mutex, only via ovs_dp_notify_wq(). */ | |
15e47304 | 1822 | struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid, |
ccb1352e JG |
1823 | u32 seq, u8 cmd) |
1824 | { | |
1825 | struct sk_buff *skb; | |
1826 | int retval; | |
1827 | ||
1828 | skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC); | |
1829 | if (!skb) | |
1830 | return ERR_PTR(-ENOMEM); | |
1831 | ||
15e47304 | 1832 | retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd); |
a9341512 JG |
1833 | BUG_ON(retval < 0); |
1834 | ||
ccb1352e JG |
1835 | return skb; |
1836 | } | |
1837 | ||
8e4e1713 | 1838 | /* Called with ovs_mutex or RCU read lock. */ |
46df7b81 | 1839 | static struct vport *lookup_vport(struct net *net, |
12eb18f7 | 1840 | const struct ovs_header *ovs_header, |
ccb1352e JG |
1841 | struct nlattr *a[OVS_VPORT_ATTR_MAX + 1]) |
1842 | { | |
1843 | struct datapath *dp; | |
1844 | struct vport *vport; | |
1845 | ||
1846 | if (a[OVS_VPORT_ATTR_NAME]) { | |
46df7b81 | 1847 | vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME])); |
ccb1352e JG |
1848 | if (!vport) |
1849 | return ERR_PTR(-ENODEV); | |
651a68ea BP |
1850 | if (ovs_header->dp_ifindex && |
1851 | ovs_header->dp_ifindex != get_dpifindex(vport->dp)) | |
1852 | return ERR_PTR(-ENODEV); | |
ccb1352e JG |
1853 | return vport; |
1854 | } else if (a[OVS_VPORT_ATTR_PORT_NO]) { | |
1855 | u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]); | |
1856 | ||
1857 | if (port_no >= DP_MAX_PORTS) | |
1858 | return ERR_PTR(-EFBIG); | |
1859 | ||
46df7b81 | 1860 | dp = get_dp(net, ovs_header->dp_ifindex); |
ccb1352e JG |
1861 | if (!dp) |
1862 | return ERR_PTR(-ENODEV); | |
1863 | ||
8e4e1713 | 1864 | vport = ovs_vport_ovsl_rcu(dp, port_no); |
ccb1352e | 1865 | if (!vport) |
14408dba | 1866 | return ERR_PTR(-ENODEV); |
ccb1352e JG |
1867 | return vport; |
1868 | } else | |
1869 | return ERR_PTR(-EINVAL); | |
1870 | } | |
1871 | ||
1872 | static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info) | |
1873 | { | |
1874 | struct nlattr **a = info->attrs; | |
1875 | struct ovs_header *ovs_header = info->userhdr; | |
1876 | struct vport_parms parms; | |
1877 | struct sk_buff *reply; | |
1878 | struct vport *vport; | |
1879 | struct datapath *dp; | |
1880 | u32 port_no; | |
1881 | int err; | |
1882 | ||
ccb1352e JG |
1883 | if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] || |
1884 | !a[OVS_VPORT_ATTR_UPCALL_PID]) | |
6093ae9a JR |
1885 | return -EINVAL; |
1886 | ||
1887 | port_no = a[OVS_VPORT_ATTR_PORT_NO] | |
1888 | ? nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]) : 0; | |
1889 | if (port_no >= DP_MAX_PORTS) | |
1890 | return -EFBIG; | |
1891 | ||
1892 | reply = ovs_vport_cmd_alloc_info(); | |
1893 | if (!reply) | |
1894 | return -ENOMEM; | |
ccb1352e | 1895 | |
8e4e1713 | 1896 | ovs_lock(); |
62b9c8d0 | 1897 | restart: |
46df7b81 | 1898 | dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex); |
ccb1352e JG |
1899 | err = -ENODEV; |
1900 | if (!dp) | |
6093ae9a | 1901 | goto exit_unlock_free; |
ccb1352e | 1902 | |
6093ae9a | 1903 | if (port_no) { |
8e4e1713 | 1904 | vport = ovs_vport_ovsl(dp, port_no); |
ccb1352e JG |
1905 | err = -EBUSY; |
1906 | if (vport) | |
6093ae9a | 1907 | goto exit_unlock_free; |
ccb1352e JG |
1908 | } else { |
1909 | for (port_no = 1; ; port_no++) { | |
1910 | if (port_no >= DP_MAX_PORTS) { | |
1911 | err = -EFBIG; | |
6093ae9a | 1912 | goto exit_unlock_free; |
ccb1352e | 1913 | } |
8e4e1713 | 1914 | vport = ovs_vport_ovsl(dp, port_no); |
ccb1352e JG |
1915 | if (!vport) |
1916 | break; | |
1917 | } | |
1918 | } | |
1919 | ||
1920 | parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]); | |
1921 | parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]); | |
1922 | parms.options = a[OVS_VPORT_ATTR_OPTIONS]; | |
1923 | parms.dp = dp; | |
1924 | parms.port_no = port_no; | |
5cd667b0 | 1925 | parms.upcall_portids = a[OVS_VPORT_ATTR_UPCALL_PID]; |
ccb1352e JG |
1926 | |
1927 | vport = new_vport(&parms); | |
1928 | err = PTR_ERR(vport); | |
62b9c8d0 TG |
1929 | if (IS_ERR(vport)) { |
1930 | if (err == -EAGAIN) | |
1931 | goto restart; | |
6093ae9a | 1932 | goto exit_unlock_free; |
62b9c8d0 | 1933 | } |
ccb1352e | 1934 | |
6093ae9a JR |
1935 | err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid, |
1936 | info->snd_seq, 0, OVS_VPORT_CMD_NEW); | |
1937 | BUG_ON(err < 0); | |
1938 | ovs_unlock(); | |
ed661185 | 1939 | |
2a94fe48 | 1940 | ovs_notify(&dp_vport_genl_family, reply, info); |
6093ae9a | 1941 | return 0; |
ccb1352e | 1942 | |
6093ae9a | 1943 | exit_unlock_free: |
8e4e1713 | 1944 | ovs_unlock(); |
6093ae9a | 1945 | kfree_skb(reply); |
ccb1352e JG |
1946 | return err; |
1947 | } | |
1948 | ||
1949 | static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info) | |
1950 | { | |
1951 | struct nlattr **a = info->attrs; | |
1952 | struct sk_buff *reply; | |
1953 | struct vport *vport; | |
1954 | int err; | |
1955 | ||
6093ae9a JR |
1956 | reply = ovs_vport_cmd_alloc_info(); |
1957 | if (!reply) | |
1958 | return -ENOMEM; | |
1959 | ||
8e4e1713 | 1960 | ovs_lock(); |
46df7b81 | 1961 | vport = lookup_vport(sock_net(skb->sk), info->userhdr, a); |
ccb1352e JG |
1962 | err = PTR_ERR(vport); |
1963 | if (IS_ERR(vport)) | |
6093ae9a | 1964 | goto exit_unlock_free; |
ccb1352e | 1965 | |
ccb1352e | 1966 | if (a[OVS_VPORT_ATTR_TYPE] && |
f44f3408 | 1967 | nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) { |
ccb1352e | 1968 | err = -EINVAL; |
6093ae9a | 1969 | goto exit_unlock_free; |
a9341512 JG |
1970 | } |
1971 | ||
f44f3408 | 1972 | if (a[OVS_VPORT_ATTR_OPTIONS]) { |
ccb1352e | 1973 | err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]); |
f44f3408 | 1974 | if (err) |
6093ae9a | 1975 | goto exit_unlock_free; |
f44f3408 | 1976 | } |
a9341512 | 1977 | |
5cd667b0 AW |
1978 | |
1979 | if (a[OVS_VPORT_ATTR_UPCALL_PID]) { | |
1980 | struct nlattr *ids = a[OVS_VPORT_ATTR_UPCALL_PID]; | |
1981 | ||
1982 | err = ovs_vport_set_upcall_portids(vport, ids); | |
1983 | if (err) | |
1984 | goto exit_unlock_free; | |
1985 | } | |
ccb1352e | 1986 | |
a9341512 JG |
1987 | err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid, |
1988 | info->snd_seq, 0, OVS_VPORT_CMD_NEW); | |
1989 | BUG_ON(err < 0); | |
ccb1352e | 1990 | |
8e4e1713 | 1991 | ovs_unlock(); |
2a94fe48 | 1992 | ovs_notify(&dp_vport_genl_family, reply, info); |
8e4e1713 | 1993 | return 0; |
ccb1352e | 1994 | |
6093ae9a | 1995 | exit_unlock_free: |
8e4e1713 | 1996 | ovs_unlock(); |
6093ae9a | 1997 | kfree_skb(reply); |
ccb1352e JG |
1998 | return err; |
1999 | } | |
2000 | ||
2001 | static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info) | |
2002 | { | |
2003 | struct nlattr **a = info->attrs; | |
2004 | struct sk_buff *reply; | |
2005 | struct vport *vport; | |
2006 | int err; | |
2007 | ||
6093ae9a JR |
2008 | reply = ovs_vport_cmd_alloc_info(); |
2009 | if (!reply) | |
2010 | return -ENOMEM; | |
2011 | ||
8e4e1713 | 2012 | ovs_lock(); |
46df7b81 | 2013 | vport = lookup_vport(sock_net(skb->sk), info->userhdr, a); |
ccb1352e JG |
2014 | err = PTR_ERR(vport); |
2015 | if (IS_ERR(vport)) | |
6093ae9a | 2016 | goto exit_unlock_free; |
ccb1352e JG |
2017 | |
2018 | if (vport->port_no == OVSP_LOCAL) { | |
2019 | err = -EINVAL; | |
6093ae9a | 2020 | goto exit_unlock_free; |
ccb1352e JG |
2021 | } |
2022 | ||
6093ae9a JR |
2023 | err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid, |
2024 | info->snd_seq, 0, OVS_VPORT_CMD_DEL); | |
2025 | BUG_ON(err < 0); | |
ccb1352e | 2026 | ovs_dp_detach_port(vport); |
6093ae9a | 2027 | ovs_unlock(); |
ccb1352e | 2028 | |
2a94fe48 | 2029 | ovs_notify(&dp_vport_genl_family, reply, info); |
6093ae9a | 2030 | return 0; |
ccb1352e | 2031 | |
6093ae9a | 2032 | exit_unlock_free: |
8e4e1713 | 2033 | ovs_unlock(); |
6093ae9a | 2034 | kfree_skb(reply); |
ccb1352e JG |
2035 | return err; |
2036 | } | |
2037 | ||
2038 | static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info) | |
2039 | { | |
2040 | struct nlattr **a = info->attrs; | |
2041 | struct ovs_header *ovs_header = info->userhdr; | |
2042 | struct sk_buff *reply; | |
2043 | struct vport *vport; | |
2044 | int err; | |
2045 | ||
6093ae9a JR |
2046 | reply = ovs_vport_cmd_alloc_info(); |
2047 | if (!reply) | |
2048 | return -ENOMEM; | |
2049 | ||
ccb1352e | 2050 | rcu_read_lock(); |
46df7b81 | 2051 | vport = lookup_vport(sock_net(skb->sk), ovs_header, a); |
ccb1352e JG |
2052 | err = PTR_ERR(vport); |
2053 | if (IS_ERR(vport)) | |
6093ae9a JR |
2054 | goto exit_unlock_free; |
2055 | err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid, | |
2056 | info->snd_seq, 0, OVS_VPORT_CMD_NEW); | |
2057 | BUG_ON(err < 0); | |
ccb1352e JG |
2058 | rcu_read_unlock(); |
2059 | ||
2060 | return genlmsg_reply(reply, info); | |
2061 | ||
6093ae9a | 2062 | exit_unlock_free: |
ccb1352e | 2063 | rcu_read_unlock(); |
6093ae9a | 2064 | kfree_skb(reply); |
ccb1352e JG |
2065 | return err; |
2066 | } | |
2067 | ||
2068 | static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb) | |
2069 | { | |
2070 | struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh)); | |
2071 | struct datapath *dp; | |
15eac2a7 PS |
2072 | int bucket = cb->args[0], skip = cb->args[1]; |
2073 | int i, j = 0; | |
ccb1352e | 2074 | |
42ee19e2 | 2075 | rcu_read_lock(); |
cc3a5ae6 | 2076 | dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex); |
42ee19e2 JR |
2077 | if (!dp) { |
2078 | rcu_read_unlock(); | |
ccb1352e | 2079 | return -ENODEV; |
42ee19e2 | 2080 | } |
15eac2a7 | 2081 | for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) { |
ccb1352e | 2082 | struct vport *vport; |
15eac2a7 PS |
2083 | |
2084 | j = 0; | |
b67bfe0d | 2085 | hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) { |
15eac2a7 PS |
2086 | if (j >= skip && |
2087 | ovs_vport_cmd_fill_info(vport, skb, | |
15e47304 | 2088 | NETLINK_CB(cb->skb).portid, |
15eac2a7 PS |
2089 | cb->nlh->nlmsg_seq, |
2090 | NLM_F_MULTI, | |
2091 | OVS_VPORT_CMD_NEW) < 0) | |
2092 | goto out; | |
2093 | ||
2094 | j++; | |
2095 | } | |
2096 | skip = 0; | |
ccb1352e | 2097 | } |
15eac2a7 | 2098 | out: |
ccb1352e JG |
2099 | rcu_read_unlock(); |
2100 | ||
15eac2a7 PS |
2101 | cb->args[0] = i; |
2102 | cb->args[1] = j; | |
ccb1352e | 2103 | |
15eac2a7 | 2104 | return skb->len; |
ccb1352e JG |
2105 | } |
2106 | ||
0c200ef9 PS |
2107 | static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = { |
2108 | [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 }, | |
2109 | [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) }, | |
2110 | [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 }, | |
2111 | [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 }, | |
2112 | [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 }, | |
2113 | [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED }, | |
2114 | }; | |
2115 | ||
48e48a70 | 2116 | static const struct genl_ops dp_vport_genl_ops[] = { |
ccb1352e JG |
2117 | { .cmd = OVS_VPORT_CMD_NEW, |
2118 | .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ | |
2119 | .policy = vport_policy, | |
2120 | .doit = ovs_vport_cmd_new | |
2121 | }, | |
2122 | { .cmd = OVS_VPORT_CMD_DEL, | |
2123 | .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ | |
2124 | .policy = vport_policy, | |
2125 | .doit = ovs_vport_cmd_del | |
2126 | }, | |
2127 | { .cmd = OVS_VPORT_CMD_GET, | |
2128 | .flags = 0, /* OK for unprivileged users. */ | |
2129 | .policy = vport_policy, | |
2130 | .doit = ovs_vport_cmd_get, | |
2131 | .dumpit = ovs_vport_cmd_dump | |
2132 | }, | |
2133 | { .cmd = OVS_VPORT_CMD_SET, | |
2134 | .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ | |
2135 | .policy = vport_policy, | |
2136 | .doit = ovs_vport_cmd_set, | |
2137 | }, | |
2138 | }; | |
2139 | ||
0c200ef9 PS |
2140 | struct genl_family dp_vport_genl_family = { |
2141 | .id = GENL_ID_GENERATE, | |
2142 | .hdrsize = sizeof(struct ovs_header), | |
2143 | .name = OVS_VPORT_FAMILY, | |
2144 | .version = OVS_VPORT_VERSION, | |
2145 | .maxattr = OVS_VPORT_ATTR_MAX, | |
2146 | .netnsok = true, | |
2147 | .parallel_ops = true, | |
2148 | .ops = dp_vport_genl_ops, | |
2149 | .n_ops = ARRAY_SIZE(dp_vport_genl_ops), | |
2150 | .mcgrps = &ovs_dp_vport_multicast_group, | |
2151 | .n_mcgrps = 1, | |
ccb1352e JG |
2152 | }; |
2153 | ||
0c200ef9 PS |
2154 | static struct genl_family * const dp_genl_families[] = { |
2155 | &dp_datapath_genl_family, | |
2156 | &dp_vport_genl_family, | |
2157 | &dp_flow_genl_family, | |
2158 | &dp_packet_genl_family, | |
ccb1352e JG |
2159 | }; |
2160 | ||
2161 | static void dp_unregister_genl(int n_families) | |
2162 | { | |
2163 | int i; | |
2164 | ||
2165 | for (i = 0; i < n_families; i++) | |
0c200ef9 | 2166 | genl_unregister_family(dp_genl_families[i]); |
ccb1352e JG |
2167 | } |
2168 | ||
2169 | static int dp_register_genl(void) | |
2170 | { | |
ccb1352e JG |
2171 | int err; |
2172 | int i; | |
2173 | ||
ccb1352e | 2174 | for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) { |
ccb1352e | 2175 | |
0c200ef9 | 2176 | err = genl_register_family(dp_genl_families[i]); |
ccb1352e JG |
2177 | if (err) |
2178 | goto error; | |
ccb1352e JG |
2179 | } |
2180 | ||
2181 | return 0; | |
2182 | ||
2183 | error: | |
0c200ef9 | 2184 | dp_unregister_genl(i); |
ccb1352e JG |
2185 | return err; |
2186 | } | |
2187 | ||
46df7b81 PS |
2188 | static int __net_init ovs_init_net(struct net *net) |
2189 | { | |
2190 | struct ovs_net *ovs_net = net_generic(net, ovs_net_id); | |
2191 | ||
2192 | INIT_LIST_HEAD(&ovs_net->dps); | |
8e4e1713 | 2193 | INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq); |
46df7b81 PS |
2194 | return 0; |
2195 | } | |
2196 | ||
2197 | static void __net_exit ovs_exit_net(struct net *net) | |
2198 | { | |
46df7b81 | 2199 | struct datapath *dp, *dp_next; |
8e4e1713 | 2200 | struct ovs_net *ovs_net = net_generic(net, ovs_net_id); |
46df7b81 | 2201 | |
8e4e1713 | 2202 | ovs_lock(); |
46df7b81 PS |
2203 | list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node) |
2204 | __dp_destroy(dp); | |
8e4e1713 PS |
2205 | ovs_unlock(); |
2206 | ||
2207 | cancel_work_sync(&ovs_net->dp_notify_work); | |
46df7b81 PS |
2208 | } |
2209 | ||
2210 | static struct pernet_operations ovs_net_ops = { | |
2211 | .init = ovs_init_net, | |
2212 | .exit = ovs_exit_net, | |
2213 | .id = &ovs_net_id, | |
2214 | .size = sizeof(struct ovs_net), | |
2215 | }; | |
2216 | ||
ccb1352e JG |
2217 | static int __init dp_init(void) |
2218 | { | |
ccb1352e JG |
2219 | int err; |
2220 | ||
3523b29b | 2221 | BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb)); |
ccb1352e JG |
2222 | |
2223 | pr_info("Open vSwitch switching datapath\n"); | |
2224 | ||
971427f3 | 2225 | err = action_fifos_init(); |
ccb1352e JG |
2226 | if (err) |
2227 | goto error; | |
2228 | ||
971427f3 AZ |
2229 | err = ovs_internal_dev_rtnl_link_register(); |
2230 | if (err) | |
2231 | goto error_action_fifos_exit; | |
2232 | ||
5b9e7e16 JP |
2233 | err = ovs_flow_init(); |
2234 | if (err) | |
2235 | goto error_unreg_rtnl_link; | |
2236 | ||
ccb1352e JG |
2237 | err = ovs_vport_init(); |
2238 | if (err) | |
2239 | goto error_flow_exit; | |
2240 | ||
46df7b81 | 2241 | err = register_pernet_device(&ovs_net_ops); |
ccb1352e JG |
2242 | if (err) |
2243 | goto error_vport_exit; | |
2244 | ||
46df7b81 PS |
2245 | err = register_netdevice_notifier(&ovs_dp_device_notifier); |
2246 | if (err) | |
2247 | goto error_netns_exit; | |
2248 | ||
62b9c8d0 TG |
2249 | err = ovs_netdev_init(); |
2250 | if (err) | |
2251 | goto error_unreg_notifier; | |
2252 | ||
ccb1352e JG |
2253 | err = dp_register_genl(); |
2254 | if (err < 0) | |
62b9c8d0 | 2255 | goto error_unreg_netdev; |
ccb1352e | 2256 | |
ccb1352e JG |
2257 | return 0; |
2258 | ||
62b9c8d0 TG |
2259 | error_unreg_netdev: |
2260 | ovs_netdev_exit(); | |
ccb1352e JG |
2261 | error_unreg_notifier: |
2262 | unregister_netdevice_notifier(&ovs_dp_device_notifier); | |
46df7b81 PS |
2263 | error_netns_exit: |
2264 | unregister_pernet_device(&ovs_net_ops); | |
ccb1352e JG |
2265 | error_vport_exit: |
2266 | ovs_vport_exit(); | |
2267 | error_flow_exit: | |
2268 | ovs_flow_exit(); | |
5b9e7e16 JP |
2269 | error_unreg_rtnl_link: |
2270 | ovs_internal_dev_rtnl_link_unregister(); | |
971427f3 AZ |
2271 | error_action_fifos_exit: |
2272 | action_fifos_exit(); | |
ccb1352e JG |
2273 | error: |
2274 | return err; | |
2275 | } | |
2276 | ||
2277 | static void dp_cleanup(void) | |
2278 | { | |
ccb1352e | 2279 | dp_unregister_genl(ARRAY_SIZE(dp_genl_families)); |
62b9c8d0 | 2280 | ovs_netdev_exit(); |
ccb1352e | 2281 | unregister_netdevice_notifier(&ovs_dp_device_notifier); |
46df7b81 PS |
2282 | unregister_pernet_device(&ovs_net_ops); |
2283 | rcu_barrier(); | |
ccb1352e JG |
2284 | ovs_vport_exit(); |
2285 | ovs_flow_exit(); | |
5b9e7e16 | 2286 | ovs_internal_dev_rtnl_link_unregister(); |
971427f3 | 2287 | action_fifos_exit(); |
ccb1352e JG |
2288 | } |
2289 | ||
2290 | module_init(dp_init); | |
2291 | module_exit(dp_cleanup); | |
2292 | ||
2293 | MODULE_DESCRIPTION("Open vSwitch switching datapath"); | |
2294 | MODULE_LICENSE("GPL"); |