]> git.proxmox.com Git - mirror_ovs.git/blob - datapath/datapath.c
cirrus: Use FreeBSD 12.2.
[mirror_ovs.git] / datapath / datapath.c
1 /*
2 * Copyright (c) 2007-2015 Nicira, Inc.
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>
39 #include <linux/version.h>
40 #include <linux/ethtool.h>
41 #include <linux/wait.h>
42 #include <asm/div64.h>
43 #include <linux/highmem.h>
44 #include <linux/netfilter_bridge.h>
45 #include <linux/netfilter_ipv4.h>
46 #include <linux/inetdevice.h>
47 #include <linux/list.h>
48 #include <linux/openvswitch.h>
49 #include <linux/rculist.h>
50 #include <linux/dmi.h>
51 #include <net/genetlink.h>
52 #include <net/net_namespace.h>
53 #include <net/netns/generic.h>
54 #include <net/nsh.h>
55
56 #include "datapath.h"
57 #include "conntrack.h"
58 #include "flow.h"
59 #include "flow_table.h"
60 #include "flow_netlink.h"
61 #include "meter.h"
62 #include "gso.h"
63 #include "vport-internal_dev.h"
64 #include "vport-netdev.h"
65
66 unsigned int ovs_net_id __read_mostly;
67
68 static struct genl_family dp_packet_genl_family;
69 static struct genl_family dp_flow_genl_family;
70 static struct genl_family dp_datapath_genl_family;
71
72 static const struct nla_policy flow_policy[];
73
74 static const struct genl_multicast_group ovs_dp_flow_multicast_group = {
75 .name = OVS_FLOW_MCGROUP,
76 };
77
78 static const struct genl_multicast_group ovs_dp_datapath_multicast_group = {
79 .name = OVS_DATAPATH_MCGROUP,
80 };
81
82 const struct genl_multicast_group ovs_dp_vport_multicast_group = {
83 .name = OVS_VPORT_MCGROUP,
84 };
85
86 /* Check if need to build a reply message.
87 * OVS userspace sets the NLM_F_ECHO flag if it needs the reply.
88 */
89 static bool ovs_must_notify(struct genl_family *family, struct genl_info *info,
90 unsigned int group)
91 {
92 return info->nlhdr->nlmsg_flags & NLM_F_ECHO ||
93 genl_has_listeners(family, genl_info_net(info), group);
94 }
95
96 static void ovs_notify(struct genl_family *family,
97 const struct genl_multicast_group *grp,
98 struct sk_buff *skb, struct genl_info *info)
99 {
100 genl_notify(family, skb, info, GROUP_ID(grp), GFP_KERNEL);
101 }
102
103 /**
104 * DOC: Locking:
105 *
106 * All writes e.g. Writes to device state (add/remove datapath, port, set
107 * operations on vports, etc.), Writes to other state (flow table
108 * modifications, set miscellaneous datapath parameters, etc.) are protected
109 * by ovs_lock.
110 *
111 * Reads are protected by RCU.
112 *
113 * There are a few special cases (mostly stats) that have their own
114 * synchronization but they nest under all of above and don't interact with
115 * each other.
116 *
117 * The RTNL lock nests inside ovs_mutex.
118 */
119
120 static DEFINE_MUTEX(ovs_mutex);
121
122 void ovs_lock(void)
123 {
124 mutex_lock(&ovs_mutex);
125 }
126
127 void ovs_unlock(void)
128 {
129 mutex_unlock(&ovs_mutex);
130 }
131
132 #ifdef CONFIG_LOCKDEP
133 int lockdep_ovsl_is_held(void)
134 {
135 if (debug_locks)
136 return lockdep_is_held(&ovs_mutex);
137 else
138 return 1;
139 }
140 #endif
141
142 static int queue_gso_packets(struct datapath *dp, struct sk_buff *,
143 const struct sw_flow_key *,
144 const struct dp_upcall_info *,
145 uint32_t cutlen);
146 static int queue_userspace_packet(struct datapath *dp, struct sk_buff *,
147 const struct sw_flow_key *,
148 const struct dp_upcall_info *,
149 uint32_t cutlen);
150
151 /* Must be called with rcu_read_lock or ovs_mutex. */
152 const char *ovs_dp_name(const struct datapath *dp)
153 {
154 struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL);
155 return ovs_vport_name(vport);
156 }
157
158 static int get_dpifindex(const struct datapath *dp)
159 {
160 struct vport *local;
161 int ifindex;
162
163 rcu_read_lock();
164
165 local = ovs_vport_rcu(dp, OVSP_LOCAL);
166 if (local)
167 ifindex = local->dev->ifindex;
168 else
169 ifindex = 0;
170
171 rcu_read_unlock();
172
173 return ifindex;
174 }
175
176 static void destroy_dp_rcu(struct rcu_head *rcu)
177 {
178 struct datapath *dp = container_of(rcu, struct datapath, rcu);
179
180 ovs_flow_tbl_destroy(&dp->table);
181 free_percpu(dp->stats_percpu);
182 kfree(dp->ports);
183 ovs_meters_exit(dp);
184 kfree(dp);
185 }
186
187 static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
188 u16 port_no)
189 {
190 return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
191 }
192
193 /* Called with ovs_mutex or RCU read lock. */
194 struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
195 {
196 struct vport *vport;
197 struct hlist_head *head;
198
199 head = vport_hash_bucket(dp, port_no);
200 hlist_for_each_entry_rcu(vport, head, dp_hash_node) {
201 if (vport->port_no == port_no)
202 return vport;
203 }
204 return NULL;
205 }
206
207 /* Called with ovs_mutex. */
208 static struct vport *new_vport(const struct vport_parms *parms)
209 {
210 struct vport *vport;
211
212 vport = ovs_vport_add(parms);
213 if (!IS_ERR(vport)) {
214 struct datapath *dp = parms->dp;
215 struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
216
217 hlist_add_head_rcu(&vport->dp_hash_node, head);
218 }
219 return vport;
220 }
221
222 void ovs_dp_detach_port(struct vport *p)
223 {
224 ASSERT_OVSL();
225
226 /* First drop references to device. */
227 hlist_del_rcu(&p->dp_hash_node);
228
229 /* Then destroy it. */
230 ovs_vport_del(p);
231 }
232
233 /* Must be called with rcu_read_lock. */
234 void ovs_dp_process_packet(struct sk_buff *skb, struct sw_flow_key *key)
235 {
236 const struct vport *p = OVS_CB(skb)->input_vport;
237 struct datapath *dp = p->dp;
238 struct sw_flow *flow;
239 struct sw_flow_actions *sf_acts;
240 struct dp_stats_percpu *stats;
241 u64 *stats_counter;
242 u32 n_mask_hit;
243 int error;
244
245 stats = this_cpu_ptr(dp->stats_percpu);
246
247 /* Look up flow. */
248 flow = ovs_flow_tbl_lookup_stats(&dp->table, key, skb_get_hash(skb),
249 &n_mask_hit);
250 if (unlikely(!flow)) {
251 struct dp_upcall_info upcall;
252
253 memset(&upcall, 0, sizeof(upcall));
254 upcall.cmd = OVS_PACKET_CMD_MISS;
255 upcall.portid = ovs_vport_find_upcall_portid(p, skb);
256 upcall.mru = OVS_CB(skb)->mru;
257 error = ovs_dp_upcall(dp, skb, key, &upcall, 0);
258 if (unlikely(error))
259 kfree_skb(skb);
260 else
261 consume_skb(skb);
262 stats_counter = &stats->n_missed;
263 goto out;
264 }
265
266 ovs_flow_stats_update(flow, key->tp.flags, skb);
267 sf_acts = rcu_dereference(flow->sf_acts);
268 error = ovs_execute_actions(dp, skb, sf_acts, key);
269 if (unlikely(error))
270 net_dbg_ratelimited("ovs: action execution error on datapath %s: %d\n",
271 ovs_dp_name(dp), error);
272
273 stats_counter = &stats->n_hit;
274
275 out:
276 /* Update datapath statistics. */
277 u64_stats_update_begin(&stats->syncp);
278 (*stats_counter)++;
279 stats->n_mask_hit += n_mask_hit;
280 u64_stats_update_end(&stats->syncp);
281 }
282
283 int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
284 const struct sw_flow_key *key,
285 const struct dp_upcall_info *upcall_info,
286 uint32_t cutlen)
287 {
288 struct dp_stats_percpu *stats;
289 int err;
290
291 if (upcall_info->portid == 0) {
292 err = -ENOTCONN;
293 goto err;
294 }
295
296 if (!skb_is_gso(skb))
297 err = queue_userspace_packet(dp, skb, key, upcall_info, cutlen);
298 else
299 err = queue_gso_packets(dp, skb, key, upcall_info, cutlen);
300 if (err)
301 goto err;
302
303 return 0;
304
305 err:
306 stats = this_cpu_ptr(dp->stats_percpu);
307
308 u64_stats_update_begin(&stats->syncp);
309 stats->n_lost++;
310 u64_stats_update_end(&stats->syncp);
311
312 return err;
313 }
314
315 static int queue_gso_packets(struct datapath *dp, struct sk_buff *skb,
316 const struct sw_flow_key *key,
317 const struct dp_upcall_info *upcall_info,
318 uint32_t cutlen)
319 {
320 #ifdef HAVE_SKB_GSO_UDP
321 unsigned int gso_type = skb_shinfo(skb)->gso_type;
322 struct sw_flow_key later_key;
323 #endif
324 struct sk_buff *segs, *nskb;
325 struct ovs_skb_cb ovs_cb;
326 int err;
327
328 ovs_cb = *OVS_CB(skb);
329 segs = __skb_gso_segment(skb, NETIF_F_SG, false);
330 *OVS_CB(skb) = ovs_cb;
331 if (IS_ERR(segs))
332 return PTR_ERR(segs);
333 if (segs == NULL)
334 return -EINVAL;
335 #ifdef HAVE_SKB_GSO_UDP
336 if (gso_type & SKB_GSO_UDP) {
337 /* The initial flow key extracted by ovs_flow_key_extract()
338 * in this case is for a first fragment, so we need to
339 * properly mark later fragments.
340 */
341 later_key = *key;
342 later_key.ip.frag = OVS_FRAG_TYPE_LATER;
343 }
344 #endif
345 /* Queue all of the segments. */
346 skb_list_walk_safe(segs, skb, nskb) {
347 *OVS_CB(skb) = ovs_cb;
348 #ifdef HAVE_SKB_GSO_UDP
349 if (gso_type & SKB_GSO_UDP && skb != segs)
350 key = &later_key;
351 #endif
352 err = queue_userspace_packet(dp, skb, key, upcall_info, cutlen);
353 if (err)
354 break;
355
356 }
357
358 /* Free all of the segments. */
359 skb_list_walk_safe(segs, skb, nskb) {
360 if (err)
361 kfree_skb(skb);
362 else
363 consume_skb(skb);
364 }
365 return err;
366 }
367
368 static size_t upcall_msg_size(const struct dp_upcall_info *upcall_info,
369 unsigned int hdrlen, int actions_attrlen)
370 {
371 size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
372 + nla_total_size(hdrlen) /* OVS_PACKET_ATTR_PACKET */
373 + nla_total_size(ovs_key_attr_size()) /* OVS_PACKET_ATTR_KEY */
374 + nla_total_size(sizeof(unsigned int)) /* OVS_PACKET_ATTR_LEN */
375 + nla_total_size(sizeof(u64)); /* OVS_PACKET_ATTR_HASH */
376
377 /* OVS_PACKET_ATTR_USERDATA */
378 if (upcall_info->userdata)
379 size += NLA_ALIGN(upcall_info->userdata->nla_len);
380
381 /* OVS_PACKET_ATTR_EGRESS_TUN_KEY */
382 if (upcall_info->egress_tun_info)
383 size += nla_total_size(ovs_tun_key_attr_size());
384
385 /* OVS_PACKET_ATTR_ACTIONS */
386 if (upcall_info->actions_len)
387 size += nla_total_size(actions_attrlen);
388
389 /* OVS_PACKET_ATTR_MRU */
390 if (upcall_info->mru)
391 size += nla_total_size(sizeof(upcall_info->mru));
392
393 return size;
394 }
395
396 static void pad_packet(struct datapath *dp, struct sk_buff *skb)
397 {
398 if (!(dp->user_features & OVS_DP_F_UNALIGNED)) {
399 size_t plen = NLA_ALIGN(skb->len) - skb->len;
400
401 if (plen > 0)
402 skb_put_zero(skb, plen);
403 }
404 }
405
406 static int queue_userspace_packet(struct datapath *dp, struct sk_buff *skb,
407 const struct sw_flow_key *key,
408 const struct dp_upcall_info *upcall_info,
409 uint32_t cutlen)
410 {
411 struct ovs_header *upcall;
412 struct sk_buff *nskb = NULL;
413 struct sk_buff *user_skb = NULL; /* to be queued to userspace */
414 struct nlattr *nla;
415 size_t len;
416 unsigned int hlen;
417 int err, dp_ifindex;
418 u64 hash;
419
420 dp_ifindex = get_dpifindex(dp);
421 if (!dp_ifindex)
422 return -ENODEV;
423
424 if (skb_vlan_tag_present(skb)) {
425 nskb = skb_clone(skb, GFP_ATOMIC);
426 if (!nskb)
427 return -ENOMEM;
428
429 nskb = __vlan_hwaccel_push_inside(nskb);
430 if (!nskb)
431 return -ENOMEM;
432
433 skb = nskb;
434 }
435
436 if (nla_attr_size(skb->len) > USHRT_MAX) {
437 err = -EFBIG;
438 goto out;
439 }
440
441 /* Complete checksum if needed */
442 if (skb->ip_summed == CHECKSUM_PARTIAL &&
443 (err = skb_csum_hwoffload_help(skb, 0)))
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
455 len = upcall_msg_size(upcall_info, hlen - cutlen,
456 OVS_CB(skb)->acts_origlen);
457 user_skb = genlmsg_new(len, GFP_ATOMIC);
458 if (!user_skb) {
459 err = -ENOMEM;
460 goto out;
461 }
462
463 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
464 0, upcall_info->cmd);
465 if (!upcall) {
466 err = -EINVAL;
467 goto out;
468 }
469 upcall->dp_ifindex = dp_ifindex;
470
471 err = ovs_nla_put_key(key, key, OVS_PACKET_ATTR_KEY, false, user_skb);
472 if (err)
473 goto out;
474
475 if (upcall_info->userdata)
476 __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
477 nla_len(upcall_info->userdata),
478 nla_data(upcall_info->userdata));
479
480
481 if (upcall_info->egress_tun_info) {
482 nla = nla_nest_start_noflag(user_skb,
483 OVS_PACKET_ATTR_EGRESS_TUN_KEY);
484 if (!nla) {
485 err = -EMSGSIZE;
486 goto out;
487 }
488 err = ovs_nla_put_tunnel_info(user_skb,
489 upcall_info->egress_tun_info);
490 if (err)
491 goto out;
492
493 nla_nest_end(user_skb, nla);
494 }
495
496 if (upcall_info->actions_len) {
497 nla = nla_nest_start_noflag(user_skb, OVS_PACKET_ATTR_ACTIONS);
498 if (!nla) {
499 err = -EMSGSIZE;
500 goto out;
501 }
502 err = ovs_nla_put_actions(upcall_info->actions,
503 upcall_info->actions_len,
504 user_skb);
505 if (!err)
506 nla_nest_end(user_skb, nla);
507 else
508 nla_nest_cancel(user_skb, nla);
509 }
510
511 /* Add OVS_PACKET_ATTR_MRU */
512 if (upcall_info->mru &&
513 nla_put_u16(user_skb, OVS_PACKET_ATTR_MRU, upcall_info->mru)) {
514 err = -ENOBUFS;
515 goto out;
516 }
517
518 /* Add OVS_PACKET_ATTR_LEN when packet is truncated */
519 if (cutlen > 0 &&
520 nla_put_u32(user_skb, OVS_PACKET_ATTR_LEN, skb->len)) {
521 err = -ENOBUFS;
522 goto out;
523 }
524
525 /* Add OVS_PACKET_ATTR_HASH */
526 hash = skb_get_hash_raw(skb);
527 #ifdef HAVE_SW_HASH
528 if (skb->sw_hash)
529 hash |= OVS_PACKET_HASH_SW_BIT;
530 #endif
531
532 if (skb->l4_hash)
533 hash |= OVS_PACKET_HASH_L4_BIT;
534
535 if (nla_put(user_skb, OVS_PACKET_ATTR_HASH, sizeof (u64), &hash)) {
536 err = -ENOBUFS;
537 goto out;
538 }
539
540 /* Only reserve room for attribute header, packet data is added
541 * in skb_zerocopy()
542 */
543 if (!(nla = nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, 0))) {
544 err = -ENOBUFS;
545 goto out;
546 }
547 nla->nla_len = nla_attr_size(skb->len - cutlen);
548
549 err = skb_zerocopy(user_skb, skb, skb->len - cutlen, hlen);
550 if (err)
551 goto out;
552
553 /* Pad OVS_PACKET_ATTR_PACKET if linear copy was performed */
554 pad_packet(dp, user_skb);
555
556 ((struct nlmsghdr *) user_skb->data)->nlmsg_len = user_skb->len;
557
558 err = genlmsg_unicast(ovs_dp_get_net(dp), user_skb, upcall_info->portid);
559 user_skb = NULL;
560 out:
561 if (err)
562 skb_tx_error(skb);
563 kfree_skb(user_skb);
564 kfree_skb(nskb);
565 return err;
566 }
567
568 static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
569 {
570 struct ovs_header *ovs_header = info->userhdr;
571 struct net *net = sock_net(skb->sk);
572 struct nlattr **a = info->attrs;
573 struct sw_flow_actions *acts;
574 struct sk_buff *packet;
575 struct sw_flow *flow;
576 struct sw_flow_actions *sf_acts;
577 struct datapath *dp;
578 struct vport *input_vport;
579 u16 mru = 0;
580 u64 hash;
581 int len;
582 int err;
583 bool log = !a[OVS_PACKET_ATTR_PROBE];
584
585 err = -EINVAL;
586 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
587 !a[OVS_PACKET_ATTR_ACTIONS])
588 goto err;
589
590 len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
591 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
592 err = -ENOMEM;
593 if (!packet)
594 goto err;
595 skb_reserve(packet, NET_IP_ALIGN);
596
597 nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
598
599 /* Set packet's mru */
600 if (a[OVS_PACKET_ATTR_MRU]) {
601 mru = nla_get_u16(a[OVS_PACKET_ATTR_MRU]);
602 packet->ignore_df = 1;
603 }
604 OVS_CB(packet)->mru = mru;
605
606 if (a[OVS_PACKET_ATTR_HASH]) {
607 hash = nla_get_u64(a[OVS_PACKET_ATTR_HASH]);
608
609 __skb_set_hash(packet, hash & 0xFFFFFFFFULL,
610 !!(hash & OVS_PACKET_HASH_SW_BIT),
611 !!(hash & OVS_PACKET_HASH_L4_BIT));
612 }
613
614 /* Build an sw_flow for sending this packet. */
615 flow = ovs_flow_alloc();
616 err = PTR_ERR(flow);
617 if (IS_ERR(flow))
618 goto err_kfree_skb;
619
620 err = ovs_flow_key_extract_userspace(net, a[OVS_PACKET_ATTR_KEY],
621 packet, &flow->key, log);
622 if (err)
623 goto err_flow_free;
624
625 err = ovs_nla_copy_actions(net, a[OVS_PACKET_ATTR_ACTIONS],
626 &flow->key, &acts, log);
627 if (err)
628 goto err_flow_free;
629
630 rcu_assign_pointer(flow->sf_acts, acts);
631 packet->priority = flow->key.phy.priority;
632 packet->mark = flow->key.phy.skb_mark;
633
634 rcu_read_lock();
635 dp = get_dp_rcu(net, ovs_header->dp_ifindex);
636 err = -ENODEV;
637 if (!dp)
638 goto err_unlock;
639
640 input_vport = ovs_vport_rcu(dp, flow->key.phy.in_port);
641 if (!input_vport)
642 input_vport = ovs_vport_rcu(dp, OVSP_LOCAL);
643
644 if (!input_vport)
645 goto err_unlock;
646
647 packet->dev = input_vport->dev;
648 OVS_CB(packet)->input_vport = input_vport;
649 sf_acts = rcu_dereference(flow->sf_acts);
650
651 local_bh_disable();
652 err = ovs_execute_actions(dp, packet, sf_acts, &flow->key);
653 local_bh_enable();
654 rcu_read_unlock();
655
656 ovs_flow_free(flow, false);
657 return err;
658
659 err_unlock:
660 rcu_read_unlock();
661 err_flow_free:
662 ovs_flow_free(flow, false);
663 err_kfree_skb:
664 kfree_skb(packet);
665 err:
666 return err;
667 }
668
669 static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
670 [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
671 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
672 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
673 [OVS_PACKET_ATTR_PROBE] = { .type = NLA_FLAG },
674 [OVS_PACKET_ATTR_MRU] = { .type = NLA_U16 },
675 [OVS_PACKET_ATTR_HASH] = { .type = NLA_U64 },
676 };
677
678 static struct genl_ops dp_packet_genl_ops[] = {
679 { .cmd = OVS_PACKET_CMD_EXECUTE,
680 #ifdef HAVE_GENL_VALIDATE_FLAGS
681 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
682 #endif
683 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
684 #ifdef HAVE_GENL_OPS_POLICY
685 .policy = packet_policy,
686 #endif
687 .doit = ovs_packet_cmd_execute
688 }
689 };
690
691 static struct genl_family dp_packet_genl_family __ro_after_init = {
692 .hdrsize = sizeof(struct ovs_header),
693 .name = OVS_PACKET_FAMILY,
694 .version = OVS_PACKET_VERSION,
695 .maxattr = OVS_PACKET_ATTR_MAX,
696 #ifndef HAVE_GENL_OPS_POLICY
697 .policy = packet_policy,
698 #endif
699 .netnsok = true,
700 .parallel_ops = true,
701 .ops = dp_packet_genl_ops,
702 .n_ops = ARRAY_SIZE(dp_packet_genl_ops),
703 .module = THIS_MODULE,
704 };
705
706 static void get_dp_stats(const struct datapath *dp, struct ovs_dp_stats *stats,
707 struct ovs_dp_megaflow_stats *mega_stats)
708 {
709 int i;
710
711 memset(mega_stats, 0, sizeof(*mega_stats));
712
713 stats->n_flows = ovs_flow_tbl_count(&dp->table);
714 mega_stats->n_masks = ovs_flow_tbl_num_masks(&dp->table);
715
716 stats->n_hit = stats->n_missed = stats->n_lost = 0;
717
718 for_each_possible_cpu(i) {
719 const struct dp_stats_percpu *percpu_stats;
720 struct dp_stats_percpu local_stats;
721 unsigned int start;
722
723 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
724
725 do {
726 start = u64_stats_fetch_begin_irq(&percpu_stats->syncp);
727 local_stats = *percpu_stats;
728 } while (u64_stats_fetch_retry_irq(&percpu_stats->syncp, start));
729
730 stats->n_hit += local_stats.n_hit;
731 stats->n_missed += local_stats.n_missed;
732 stats->n_lost += local_stats.n_lost;
733 mega_stats->n_mask_hit += local_stats.n_mask_hit;
734 }
735 }
736
737 static bool should_fill_key(const struct sw_flow_id *sfid, uint32_t ufid_flags)
738 {
739 return ovs_identifier_is_ufid(sfid) &&
740 !(ufid_flags & OVS_UFID_F_OMIT_KEY);
741 }
742
743 static bool should_fill_mask(uint32_t ufid_flags)
744 {
745 return !(ufid_flags & OVS_UFID_F_OMIT_MASK);
746 }
747
748 static bool should_fill_actions(uint32_t ufid_flags)
749 {
750 return !(ufid_flags & OVS_UFID_F_OMIT_ACTIONS);
751 }
752
753 static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts,
754 const struct sw_flow_id *sfid,
755 uint32_t ufid_flags)
756 {
757 size_t len = NLMSG_ALIGN(sizeof(struct ovs_header));
758
759 /* OVS_FLOW_ATTR_UFID, or unmasked flow key as fallback
760 * see ovs_nla_put_identifier()
761 */
762 if (sfid && ovs_identifier_is_ufid(sfid))
763 len += nla_total_size(sfid->ufid_len);
764 else
765 len += nla_total_size(ovs_key_attr_size());
766
767 /* OVS_FLOW_ATTR_KEY */
768 if (!sfid || should_fill_key(sfid, ufid_flags))
769 len += nla_total_size(ovs_key_attr_size());
770
771 /* OVS_FLOW_ATTR_MASK */
772 if (should_fill_mask(ufid_flags))
773 len += nla_total_size(ovs_key_attr_size());
774
775 /* OVS_FLOW_ATTR_ACTIONS */
776 if (should_fill_actions(ufid_flags))
777 len += nla_total_size(acts->orig_len);
778
779 return len
780 + nla_total_size_64bit(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
781 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
782 + nla_total_size_64bit(8); /* OVS_FLOW_ATTR_USED */
783 }
784
785 /* Called with ovs_mutex or RCU read lock. */
786 static int ovs_flow_cmd_fill_stats(const struct sw_flow *flow,
787 struct sk_buff *skb)
788 {
789 struct ovs_flow_stats stats;
790 __be16 tcp_flags;
791 unsigned long used;
792
793 ovs_flow_stats_get(flow, &stats, &used, &tcp_flags);
794
795 if (used &&
796 nla_put_u64_64bit(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used),
797 OVS_FLOW_ATTR_PAD))
798 return -EMSGSIZE;
799
800 if (stats.n_packets &&
801 nla_put_64bit(skb, OVS_FLOW_ATTR_STATS,
802 sizeof(struct ovs_flow_stats), &stats,
803 OVS_FLOW_ATTR_PAD))
804 return -EMSGSIZE;
805
806 if ((u8)ntohs(tcp_flags) &&
807 nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, (u8)ntohs(tcp_flags)))
808 return -EMSGSIZE;
809
810 return 0;
811 }
812
813 /* Called with ovs_mutex or RCU read lock. */
814 static int ovs_flow_cmd_fill_actions(const struct sw_flow *flow,
815 struct sk_buff *skb, int skb_orig_len)
816 {
817 struct nlattr *start;
818 int err;
819
820 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
821 * this is the first flow to be dumped into 'skb'. This is unusual for
822 * Netlink but individual action lists can be longer than
823 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
824 * The userspace caller can always fetch the actions separately if it
825 * really wants them. (Most userspace callers in fact don't care.)
826 *
827 * This can only fail for dump operations because the skb is always
828 * properly sized for single flows.
829 */
830 start = nla_nest_start_noflag(skb, OVS_FLOW_ATTR_ACTIONS);
831 if (start) {
832 const struct sw_flow_actions *sf_acts;
833
834 sf_acts = rcu_dereference_ovsl(flow->sf_acts);
835 err = ovs_nla_put_actions(sf_acts->actions,
836 sf_acts->actions_len, skb);
837
838 if (!err)
839 nla_nest_end(skb, start);
840 else {
841 if (skb_orig_len)
842 return err;
843
844 nla_nest_cancel(skb, start);
845 }
846 } else if (skb_orig_len) {
847 return -EMSGSIZE;
848 }
849
850 return 0;
851 }
852
853 /* Called with ovs_mutex or RCU read lock. */
854 static int ovs_flow_cmd_fill_info(const struct sw_flow *flow, int dp_ifindex,
855 struct sk_buff *skb, u32 portid,
856 u32 seq, u32 flags, u8 cmd, u32 ufid_flags)
857 {
858 const int skb_orig_len = skb->len;
859 struct ovs_header *ovs_header;
860 int err;
861
862 ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family,
863 flags, cmd);
864 if (!ovs_header)
865 return -EMSGSIZE;
866
867 ovs_header->dp_ifindex = dp_ifindex;
868
869 err = ovs_nla_put_identifier(flow, skb);
870 if (err)
871 goto error;
872
873 if (should_fill_key(&flow->id, ufid_flags)) {
874 err = ovs_nla_put_masked_key(flow, skb);
875 if (err)
876 goto error;
877 }
878
879 if (should_fill_mask(ufid_flags)) {
880 err = ovs_nla_put_mask(flow, skb);
881 if (err)
882 goto error;
883 }
884
885 err = ovs_flow_cmd_fill_stats(flow, skb);
886 if (err)
887 goto error;
888
889 if (should_fill_actions(ufid_flags)) {
890 err = ovs_flow_cmd_fill_actions(flow, skb, skb_orig_len);
891 if (err)
892 goto error;
893 }
894
895 genlmsg_end(skb, ovs_header);
896 return 0;
897
898 error:
899 genlmsg_cancel(skb, ovs_header);
900 return err;
901 }
902
903 /* May not be called with RCU read lock. */
904 static struct sk_buff *ovs_flow_cmd_alloc_info(const struct sw_flow_actions *acts,
905 const struct sw_flow_id *sfid,
906 struct genl_info *info,
907 bool always,
908 uint32_t ufid_flags)
909 {
910 struct sk_buff *skb;
911 size_t len;
912
913 if (!always && !ovs_must_notify(&dp_flow_genl_family, info,
914 GROUP_ID(&ovs_dp_flow_multicast_group)))
915 return NULL;
916
917 len = ovs_flow_cmd_msg_size(acts, sfid, ufid_flags);
918 skb = genlmsg_new(len, GFP_KERNEL);
919 if (!skb)
920 return ERR_PTR(-ENOMEM);
921
922 return skb;
923 }
924
925 /* Called with ovs_mutex. */
926 static struct sk_buff *ovs_flow_cmd_build_info(const struct sw_flow *flow,
927 int dp_ifindex,
928 struct genl_info *info, u8 cmd,
929 bool always, u32 ufid_flags)
930 {
931 struct sk_buff *skb;
932 int retval;
933
934 skb = ovs_flow_cmd_alloc_info(ovsl_dereference(flow->sf_acts),
935 &flow->id, info, always, ufid_flags);
936 if (IS_ERR_OR_NULL(skb))
937 return skb;
938
939 retval = ovs_flow_cmd_fill_info(flow, dp_ifindex, skb,
940 info->snd_portid, info->snd_seq, 0,
941 cmd, ufid_flags);
942 if (WARN_ON_ONCE(retval < 0)) {
943 kfree_skb(skb);
944 skb = ERR_PTR(retval);
945 }
946 return skb;
947 }
948
949 static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info)
950 {
951 struct net *net = sock_net(skb->sk);
952 struct nlattr **a = info->attrs;
953 struct ovs_header *ovs_header = info->userhdr;
954 struct sw_flow *flow = NULL, *new_flow;
955 struct sw_flow_mask mask;
956 struct sk_buff *reply;
957 struct datapath *dp;
958 struct sw_flow_actions *acts;
959 struct sw_flow_match match;
960 u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
961 int error;
962 bool log = !a[OVS_FLOW_ATTR_PROBE];
963
964 /* Must have key and actions. */
965 error = -EINVAL;
966 if (!a[OVS_FLOW_ATTR_KEY]) {
967 OVS_NLERR(log, "Flow key attr not present in new flow.");
968 goto error;
969 }
970 if (!a[OVS_FLOW_ATTR_ACTIONS]) {
971 OVS_NLERR(log, "Flow actions attr not present in new flow.");
972 goto error;
973 }
974
975 /* Most of the time we need to allocate a new flow, do it before
976 * locking.
977 */
978 new_flow = ovs_flow_alloc();
979 if (IS_ERR(new_flow)) {
980 error = PTR_ERR(new_flow);
981 goto error;
982 }
983
984 /* Extract key. */
985 ovs_match_init(&match, &new_flow->key, false, &mask);
986 error = ovs_nla_get_match(net, &match, a[OVS_FLOW_ATTR_KEY],
987 a[OVS_FLOW_ATTR_MASK], log);
988 if (error)
989 goto err_kfree_flow;
990
991 /* Extract flow identifier. */
992 error = ovs_nla_get_identifier(&new_flow->id, a[OVS_FLOW_ATTR_UFID],
993 &new_flow->key, log);
994 if (error)
995 goto err_kfree_flow;
996
997 /* unmasked key is needed to match when ufid is not used. */
998 if (ovs_identifier_is_key(&new_flow->id))
999 match.key = new_flow->id.unmasked_key;
1000
1001 ovs_flow_mask_key(&new_flow->key, &new_flow->key, true, &mask);
1002
1003 /* Validate actions. */
1004 error = ovs_nla_copy_actions(net, a[OVS_FLOW_ATTR_ACTIONS],
1005 &new_flow->key, &acts, log);
1006 if (error) {
1007 OVS_NLERR(log, "Flow actions may not be safe on all matching packets.");
1008 goto err_kfree_flow;
1009 }
1010
1011 reply = ovs_flow_cmd_alloc_info(acts, &new_flow->id, info, false,
1012 ufid_flags);
1013 if (IS_ERR(reply)) {
1014 error = PTR_ERR(reply);
1015 goto err_kfree_acts;
1016 }
1017
1018 ovs_lock();
1019 dp = get_dp(net, ovs_header->dp_ifindex);
1020 if (unlikely(!dp)) {
1021 error = -ENODEV;
1022 goto err_unlock_ovs;
1023 }
1024
1025 /* Check if this is a duplicate flow */
1026 if (ovs_identifier_is_ufid(&new_flow->id))
1027 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &new_flow->id);
1028 if (!flow)
1029 flow = ovs_flow_tbl_lookup(&dp->table, &new_flow->key);
1030 if (likely(!flow)) {
1031 rcu_assign_pointer(new_flow->sf_acts, acts);
1032
1033 /* Put flow in bucket. */
1034 error = ovs_flow_tbl_insert(&dp->table, new_flow, &mask);
1035 if (unlikely(error)) {
1036 acts = NULL;
1037 goto err_unlock_ovs;
1038 }
1039
1040 if (unlikely(reply)) {
1041 error = ovs_flow_cmd_fill_info(new_flow,
1042 ovs_header->dp_ifindex,
1043 reply, info->snd_portid,
1044 info->snd_seq, 0,
1045 OVS_FLOW_CMD_NEW,
1046 ufid_flags);
1047 BUG_ON(error < 0);
1048 }
1049 ovs_unlock();
1050 } else {
1051 struct sw_flow_actions *old_acts;
1052
1053 /* Bail out if we're not allowed to modify an existing flow.
1054 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
1055 * because Generic Netlink treats the latter as a dump
1056 * request. We also accept NLM_F_EXCL in case that bug ever
1057 * gets fixed.
1058 */
1059 if (unlikely(info->nlhdr->nlmsg_flags & (NLM_F_CREATE
1060 | NLM_F_EXCL))) {
1061 error = -EEXIST;
1062 goto err_unlock_ovs;
1063 }
1064 /* The flow identifier has to be the same for flow updates.
1065 * Look for any overlapping flow.
1066 */
1067 if (unlikely(!ovs_flow_cmp(flow, &match))) {
1068 if (ovs_identifier_is_key(&flow->id))
1069 flow = ovs_flow_tbl_lookup_exact(&dp->table,
1070 &match);
1071 else /* UFID matches but key is different */
1072 flow = NULL;
1073 if (!flow) {
1074 error = -ENOENT;
1075 goto err_unlock_ovs;
1076 }
1077 }
1078 /* Update actions. */
1079 old_acts = ovsl_dereference(flow->sf_acts);
1080 rcu_assign_pointer(flow->sf_acts, acts);
1081
1082 if (unlikely(reply)) {
1083 error = ovs_flow_cmd_fill_info(flow,
1084 ovs_header->dp_ifindex,
1085 reply, info->snd_portid,
1086 info->snd_seq, 0,
1087 OVS_FLOW_CMD_NEW,
1088 ufid_flags);
1089 BUG_ON(error < 0);
1090 }
1091 ovs_unlock();
1092
1093 ovs_nla_free_flow_actions_rcu(old_acts);
1094 ovs_flow_free(new_flow, false);
1095 }
1096
1097 if (reply)
1098 ovs_notify(&dp_flow_genl_family, &ovs_dp_flow_multicast_group, reply, info);
1099 return 0;
1100
1101 err_unlock_ovs:
1102 ovs_unlock();
1103 kfree_skb(reply);
1104 err_kfree_acts:
1105 ovs_nla_free_flow_actions(acts);
1106 err_kfree_flow:
1107 ovs_flow_free(new_flow, false);
1108 error:
1109 return error;
1110 }
1111
1112 /* Factor out action copy to avoid "Wframe-larger-than=1024" warning. */
1113 static noinline_for_stack struct sw_flow_actions *get_flow_actions(struct net *net,
1114 const struct nlattr *a,
1115 const struct sw_flow_key *key,
1116 const struct sw_flow_mask *mask,
1117 bool log)
1118 {
1119 struct sw_flow_actions *acts;
1120 struct sw_flow_key masked_key;
1121 int error;
1122
1123 ovs_flow_mask_key(&masked_key, key, true, mask);
1124 error = ovs_nla_copy_actions(net, a, &masked_key, &acts, log);
1125 if (error) {
1126 OVS_NLERR(log,
1127 "Actions may not be safe on all matching packets");
1128 return ERR_PTR(error);
1129 }
1130
1131 return acts;
1132 }
1133
1134 /* Factor out match-init and action-copy to avoid
1135 * "Wframe-larger-than=1024" warning. Because mask is only
1136 * used to get actions, we new a function to save some
1137 * stack space.
1138 *
1139 * If there are not key and action attrs, we return 0
1140 * directly. In the case, the caller will also not use the
1141 * match as before. If there is action attr, we try to get
1142 * actions and save them to *acts. Before returning from
1143 * the function, we reset the match->mask pointer. Because
1144 * we should not to return match object with dangling reference
1145 * to mask.
1146 * */
1147 static noinline_for_stack int
1148 ovs_nla_init_match_and_action(struct net *net,
1149 struct sw_flow_match *match,
1150 struct sw_flow_key *key,
1151 struct nlattr **a,
1152 struct sw_flow_actions **acts,
1153 bool log)
1154 {
1155 struct sw_flow_mask mask;
1156 int error = 0;
1157
1158 if (a[OVS_FLOW_ATTR_KEY]) {
1159 ovs_match_init(match, key, true, &mask);
1160 error = ovs_nla_get_match(net, match, a[OVS_FLOW_ATTR_KEY],
1161 a[OVS_FLOW_ATTR_MASK], log);
1162 if (error)
1163 goto error;
1164 }
1165
1166 if (a[OVS_FLOW_ATTR_ACTIONS]) {
1167 if (!a[OVS_FLOW_ATTR_KEY]) {
1168 OVS_NLERR(log,
1169 "Flow key attribute not present in set flow.");
1170 error = -EINVAL;
1171 goto error;
1172 }
1173
1174 *acts = get_flow_actions(net, a[OVS_FLOW_ATTR_ACTIONS], key,
1175 &mask, log);
1176 if (IS_ERR(*acts)) {
1177 error = PTR_ERR(*acts);
1178 goto error;
1179 }
1180 }
1181
1182 /* On success, error is 0. */
1183 error:
1184 match->mask = NULL;
1185 return error;
1186 }
1187
1188 static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info)
1189 {
1190 struct net *net = sock_net(skb->sk);
1191 struct nlattr **a = info->attrs;
1192 struct ovs_header *ovs_header = info->userhdr;
1193 struct sw_flow_key key;
1194 struct sw_flow *flow;
1195 struct sk_buff *reply = NULL;
1196 struct datapath *dp;
1197 struct sw_flow_actions *old_acts = NULL, *acts = NULL;
1198 struct sw_flow_match match;
1199 struct sw_flow_id sfid;
1200 u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
1201 int error = 0;
1202 bool log = !a[OVS_FLOW_ATTR_PROBE];
1203 bool ufid_present;
1204
1205 ufid_present = ovs_nla_get_ufid(&sfid, a[OVS_FLOW_ATTR_UFID], log);
1206 if (!a[OVS_FLOW_ATTR_KEY] && !ufid_present) {
1207 OVS_NLERR(log,
1208 "Flow set message rejected, Key attribute missing.");
1209 return -EINVAL;
1210 }
1211
1212 error = ovs_nla_init_match_and_action(net, &match, &key, a,
1213 &acts, log);
1214 if (error)
1215 goto error;
1216
1217 if (acts) {
1218 /* Can allocate before locking if have acts. */
1219 reply = ovs_flow_cmd_alloc_info(acts, &sfid, info, false,
1220 ufid_flags);
1221 if (IS_ERR(reply)) {
1222 error = PTR_ERR(reply);
1223 goto err_kfree_acts;
1224 }
1225 }
1226
1227 ovs_lock();
1228 dp = get_dp(net, ovs_header->dp_ifindex);
1229 if (unlikely(!dp)) {
1230 error = -ENODEV;
1231 goto err_unlock_ovs;
1232 }
1233 /* Check that the flow exists. */
1234 if (ufid_present)
1235 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &sfid);
1236 else
1237 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1238 if (unlikely(!flow)) {
1239 error = -ENOENT;
1240 goto err_unlock_ovs;
1241 }
1242
1243 /* Update actions, if present. */
1244 if (likely(acts)) {
1245 old_acts = ovsl_dereference(flow->sf_acts);
1246 rcu_assign_pointer(flow->sf_acts, acts);
1247
1248 if (unlikely(reply)) {
1249 error = ovs_flow_cmd_fill_info(flow,
1250 ovs_header->dp_ifindex,
1251 reply, info->snd_portid,
1252 info->snd_seq, 0,
1253 OVS_FLOW_CMD_SET,
1254 ufid_flags);
1255 BUG_ON(error < 0);
1256 }
1257 } else {
1258 /* Could not alloc without acts before locking. */
1259 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex,
1260 info, OVS_FLOW_CMD_SET, false,
1261 ufid_flags);
1262
1263 if (unlikely(IS_ERR(reply))) {
1264 error = PTR_ERR(reply);
1265 goto err_unlock_ovs;
1266 }
1267 }
1268
1269 /* Clear stats. */
1270 if (a[OVS_FLOW_ATTR_CLEAR])
1271 ovs_flow_stats_clear(flow);
1272 ovs_unlock();
1273
1274 if (reply)
1275 ovs_notify(&dp_flow_genl_family, &ovs_dp_flow_multicast_group, reply, info);
1276 if (old_acts)
1277 ovs_nla_free_flow_actions_rcu(old_acts);
1278
1279 return 0;
1280
1281 err_unlock_ovs:
1282 ovs_unlock();
1283 kfree_skb(reply);
1284 err_kfree_acts:
1285 ovs_nla_free_flow_actions(acts);
1286 error:
1287 return error;
1288 }
1289
1290 static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1291 {
1292 struct nlattr **a = info->attrs;
1293 struct ovs_header *ovs_header = info->userhdr;
1294 struct net *net = sock_net(skb->sk);
1295 struct sw_flow_key key;
1296 struct sk_buff *reply;
1297 struct sw_flow *flow;
1298 struct datapath *dp;
1299 struct sw_flow_match match;
1300 struct sw_flow_id ufid;
1301 u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
1302 int err = 0;
1303 bool log = !a[OVS_FLOW_ATTR_PROBE];
1304 bool ufid_present;
1305
1306 ufid_present = ovs_nla_get_ufid(&ufid, a[OVS_FLOW_ATTR_UFID], log);
1307 if (a[OVS_FLOW_ATTR_KEY]) {
1308 ovs_match_init(&match, &key, true, NULL);
1309 err = ovs_nla_get_match(net, &match, a[OVS_FLOW_ATTR_KEY], NULL,
1310 log);
1311 } else if (!ufid_present) {
1312 OVS_NLERR(log,
1313 "Flow get message rejected, Key attribute missing.");
1314 err = -EINVAL;
1315 }
1316 if (err)
1317 return err;
1318
1319 ovs_lock();
1320 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1321 if (!dp) {
1322 err = -ENODEV;
1323 goto unlock;
1324 }
1325
1326 if (ufid_present)
1327 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &ufid);
1328 else
1329 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1330 if (!flow) {
1331 err = -ENOENT;
1332 goto unlock;
1333 }
1334
1335 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex, info,
1336 OVS_FLOW_CMD_GET, true, ufid_flags);
1337 if (IS_ERR(reply)) {
1338 err = PTR_ERR(reply);
1339 goto unlock;
1340 }
1341
1342 ovs_unlock();
1343 return genlmsg_reply(reply, info);
1344 unlock:
1345 ovs_unlock();
1346 return err;
1347 }
1348
1349 static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1350 {
1351 struct nlattr **a = info->attrs;
1352 struct ovs_header *ovs_header = info->userhdr;
1353 struct net *net = sock_net(skb->sk);
1354 struct sw_flow_key key;
1355 struct sk_buff *reply;
1356 struct sw_flow *flow = NULL;
1357 struct datapath *dp;
1358 struct sw_flow_match match;
1359 struct sw_flow_id ufid;
1360 u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
1361 int err;
1362 bool log = !a[OVS_FLOW_ATTR_PROBE];
1363 bool ufid_present;
1364
1365 ufid_present = ovs_nla_get_ufid(&ufid, a[OVS_FLOW_ATTR_UFID], log);
1366 if (a[OVS_FLOW_ATTR_KEY]) {
1367 ovs_match_init(&match, &key, true, NULL);
1368 err = ovs_nla_get_match(net, &match, a[OVS_FLOW_ATTR_KEY],
1369 NULL, log);
1370 if (unlikely(err))
1371 return err;
1372 }
1373
1374 ovs_lock();
1375 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1376 if (unlikely(!dp)) {
1377 err = -ENODEV;
1378 goto unlock;
1379 }
1380
1381 if (unlikely(!a[OVS_FLOW_ATTR_KEY] && !ufid_present)) {
1382 err = ovs_flow_tbl_flush(&dp->table);
1383 goto unlock;
1384 }
1385
1386 if (ufid_present)
1387 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &ufid);
1388 else
1389 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1390 if (unlikely(!flow)) {
1391 err = -ENOENT;
1392 goto unlock;
1393 }
1394
1395 ovs_flow_tbl_remove(&dp->table, flow);
1396 ovs_unlock();
1397
1398 reply = ovs_flow_cmd_alloc_info(rcu_dereference_raw(flow->sf_acts),
1399 &flow->id, info, false, ufid_flags);
1400
1401 if (likely(reply)) {
1402 if (!IS_ERR(reply)) {
1403 rcu_read_lock(); /*To keep RCU checker happy. */
1404 err = ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex,
1405 reply, info->snd_portid,
1406 info->snd_seq, 0,
1407 OVS_FLOW_CMD_DEL,
1408 ufid_flags);
1409 rcu_read_unlock();
1410 if (WARN_ON_ONCE(err < 0)) {
1411 kfree_skb(reply);
1412 goto out_free;
1413 }
1414 ovs_notify(&dp_flow_genl_family, &ovs_dp_flow_multicast_group, reply, info);
1415 } else {
1416 genl_set_err(&dp_flow_genl_family, sock_net(skb->sk), 0,
1417 GROUP_ID(&ovs_dp_flow_multicast_group), PTR_ERR(reply));
1418
1419 }
1420 }
1421
1422 out_free:
1423 ovs_flow_free(flow, true);
1424 return 0;
1425 unlock:
1426 ovs_unlock();
1427 return err;
1428 }
1429
1430 static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1431 {
1432 struct nlattr *a[__OVS_FLOW_ATTR_MAX];
1433 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1434 struct table_instance *ti;
1435 struct datapath *dp;
1436 u32 ufid_flags;
1437 int err;
1438
1439 err = genlmsg_parse_deprecated(cb->nlh, &dp_flow_genl_family, a,
1440 OVS_FLOW_ATTR_MAX, flow_policy, NULL);
1441 if (err)
1442 return err;
1443 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
1444
1445 rcu_read_lock();
1446 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
1447 if (!dp) {
1448 rcu_read_unlock();
1449 return -ENODEV;
1450 }
1451
1452 ti = rcu_dereference(dp->table.ti);
1453 for (;;) {
1454 struct sw_flow *flow;
1455 u32 bucket, obj;
1456
1457 bucket = cb->args[0];
1458 obj = cb->args[1];
1459 flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj);
1460 if (!flow)
1461 break;
1462
1463 if (ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex, skb,
1464 NETLINK_CB(cb->skb).portid,
1465 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1466 OVS_FLOW_CMD_GET, ufid_flags) < 0)
1467 break;
1468
1469 cb->args[0] = bucket;
1470 cb->args[1] = obj;
1471 }
1472 rcu_read_unlock();
1473 return skb->len;
1474 }
1475
1476 static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
1477 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
1478 [OVS_FLOW_ATTR_MASK] = { .type = NLA_NESTED },
1479 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
1480 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
1481 [OVS_FLOW_ATTR_PROBE] = { .type = NLA_FLAG },
1482 [OVS_FLOW_ATTR_UFID] = { .type = NLA_UNSPEC, .len = 1 },
1483 [OVS_FLOW_ATTR_UFID_FLAGS] = { .type = NLA_U32 },
1484 };
1485
1486 static const struct genl_ops dp_flow_genl_ops[] = {
1487 { .cmd = OVS_FLOW_CMD_NEW,
1488 #ifdef HAVE_GENL_VALIDATE_FLAGS
1489 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1490 #endif
1491 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1492 #ifdef HAVE_GENL_OPS_POLICY
1493 .policy = flow_policy,
1494 #endif
1495 .doit = ovs_flow_cmd_new
1496 },
1497 { .cmd = OVS_FLOW_CMD_DEL,
1498 #ifdef HAVE_GENL_VALIDATE_FLAGS
1499 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1500 #endif
1501 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1502 #ifdef HAVE_GENL_OPS_POLICY
1503 .policy = flow_policy,
1504 #endif
1505 .doit = ovs_flow_cmd_del
1506 },
1507 { .cmd = OVS_FLOW_CMD_GET,
1508 #ifdef HAVE_GENL_VALIDATE_FLAGS
1509 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1510 #endif
1511 .flags = 0, /* OK for unprivileged users. */
1512 #ifdef HAVE_GENL_OPS_POLICY
1513 .policy = flow_policy,
1514 #endif
1515 .doit = ovs_flow_cmd_get,
1516 .dumpit = ovs_flow_cmd_dump
1517 },
1518 { .cmd = OVS_FLOW_CMD_SET,
1519 #ifdef HAVE_GENL_VALIDATE_FLAGS
1520 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1521 #endif
1522 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1523 #ifdef HAVE_GENL_OPS_POLICY
1524 .policy = flow_policy,
1525 #endif
1526 .doit = ovs_flow_cmd_set,
1527 },
1528 };
1529
1530 static struct genl_family dp_flow_genl_family __ro_after_init = {
1531 .hdrsize = sizeof(struct ovs_header),
1532 .name = OVS_FLOW_FAMILY,
1533 .version = OVS_FLOW_VERSION,
1534 .maxattr = OVS_FLOW_ATTR_MAX,
1535 #ifndef HAVE_GENL_OPS_POLICY
1536 .policy = flow_policy,
1537 #endif
1538 .netnsok = true,
1539 .parallel_ops = true,
1540 .ops = dp_flow_genl_ops,
1541 .n_ops = ARRAY_SIZE(dp_flow_genl_ops),
1542 .mcgrps = &ovs_dp_flow_multicast_group,
1543 .n_mcgrps = 1,
1544 .module = THIS_MODULE,
1545 };
1546
1547 static size_t ovs_dp_cmd_msg_size(void)
1548 {
1549 size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
1550
1551 msgsize += nla_total_size(IFNAMSIZ);
1552 msgsize += nla_total_size_64bit(sizeof(struct ovs_dp_stats));
1553 msgsize += nla_total_size_64bit(sizeof(struct ovs_dp_megaflow_stats));
1554 msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_USER_FEATURES */
1555
1556 return msgsize;
1557 }
1558
1559 /* Called with ovs_mutex. */
1560 static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
1561 u32 portid, u32 seq, u32 flags, u8 cmd)
1562 {
1563 struct ovs_header *ovs_header;
1564 struct ovs_dp_stats dp_stats;
1565 struct ovs_dp_megaflow_stats dp_megaflow_stats;
1566 int err;
1567
1568 ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
1569 flags, cmd);
1570 if (!ovs_header)
1571 goto error;
1572
1573 ovs_header->dp_ifindex = get_dpifindex(dp);
1574
1575 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
1576 if (err)
1577 goto nla_put_failure;
1578
1579 get_dp_stats(dp, &dp_stats, &dp_megaflow_stats);
1580 if (nla_put_64bit(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats),
1581 &dp_stats, OVS_DP_ATTR_PAD))
1582 goto nla_put_failure;
1583
1584 if (nla_put_64bit(skb, OVS_DP_ATTR_MEGAFLOW_STATS,
1585 sizeof(struct ovs_dp_megaflow_stats),
1586 &dp_megaflow_stats, OVS_DP_ATTR_PAD))
1587 goto nla_put_failure;
1588
1589 if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, dp->user_features))
1590 goto nla_put_failure;
1591
1592 genlmsg_end(skb, ovs_header);
1593 return 0;
1594
1595 nla_put_failure:
1596 genlmsg_cancel(skb, ovs_header);
1597 error:
1598 return -EMSGSIZE;
1599 }
1600
1601 static struct sk_buff *ovs_dp_cmd_alloc_info(void)
1602 {
1603 return genlmsg_new(ovs_dp_cmd_msg_size(), GFP_KERNEL);
1604 }
1605
1606 /* Called with rcu_read_lock or ovs_mutex. */
1607 static struct datapath *lookup_datapath(struct net *net,
1608 const struct ovs_header *ovs_header,
1609 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1610 {
1611 struct datapath *dp;
1612
1613 if (!a[OVS_DP_ATTR_NAME])
1614 dp = get_dp(net, ovs_header->dp_ifindex);
1615 else {
1616 struct vport *vport;
1617
1618 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
1619 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
1620 }
1621 return dp ? dp : ERR_PTR(-ENODEV);
1622 }
1623
1624 static void ovs_dp_reset_user_features(struct sk_buff *skb, struct genl_info *info)
1625 {
1626 struct datapath *dp;
1627
1628 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1629 if (IS_ERR(dp))
1630 return;
1631
1632 WARN(dp->user_features, "Dropping previously announced user features\n");
1633 dp->user_features = 0;
1634 }
1635
1636 DEFINE_STATIC_KEY_FALSE(tc_recirc_sharing_support);
1637
1638 static int ovs_dp_change(struct datapath *dp, struct nlattr *a[])
1639 {
1640 u32 user_features = 0;
1641
1642 if (a[OVS_DP_ATTR_USER_FEATURES]) {
1643 user_features = nla_get_u32(a[OVS_DP_ATTR_USER_FEATURES]);
1644
1645 if (user_features & ~(OVS_DP_F_VPORT_PIDS |
1646 OVS_DP_F_UNALIGNED |
1647 OVS_DP_F_TC_RECIRC_SHARING))
1648 return -EOPNOTSUPP;
1649
1650 #if !IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
1651 if (user_features & OVS_DP_F_TC_RECIRC_SHARING)
1652 return -EOPNOTSUPP;
1653 #endif
1654 }
1655
1656 dp->user_features = user_features;
1657
1658 if (dp->user_features & OVS_DP_F_TC_RECIRC_SHARING)
1659 static_branch_enable(&tc_recirc_sharing_support);
1660 else
1661 static_branch_disable(&tc_recirc_sharing_support);
1662
1663 return 0;
1664 }
1665
1666 static int ovs_dp_stats_init(struct datapath *dp)
1667 {
1668 dp->stats_percpu = netdev_alloc_pcpu_stats(struct dp_stats_percpu);
1669 if (!dp->stats_percpu)
1670 return -ENOMEM;
1671
1672 return 0;
1673 }
1674
1675 static int ovs_dp_vport_init(struct datapath *dp)
1676 {
1677 int i;
1678
1679 dp->ports = kmalloc_array(DP_VPORT_HASH_BUCKETS,
1680 sizeof(struct hlist_head),
1681 GFP_KERNEL);
1682 if (!dp->ports)
1683 return -ENOMEM;
1684
1685 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1686 INIT_HLIST_HEAD(&dp->ports[i]);
1687
1688 return 0;
1689 }
1690
1691 static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1692 {
1693 struct nlattr **a = info->attrs;
1694 struct vport_parms parms;
1695 struct sk_buff *reply;
1696 struct datapath *dp;
1697 struct vport *vport;
1698 struct ovs_net *ovs_net;
1699 int err;
1700
1701 err = -EINVAL;
1702 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1703 goto err;
1704
1705 reply = ovs_dp_cmd_alloc_info();
1706 if (!reply)
1707 return -ENOMEM;
1708
1709 err = -ENOMEM;
1710 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1711 if (dp == NULL)
1712 goto err_destroy_reply;
1713
1714 ovs_dp_set_net(dp, sock_net(skb->sk));
1715
1716 /* Allocate table. */
1717 err = ovs_flow_tbl_init(&dp->table);
1718 if (err)
1719 goto err_destroy_dp;
1720
1721 err = ovs_dp_stats_init(dp);
1722 if (err)
1723 goto err_destroy_table;
1724
1725 err = ovs_dp_vport_init(dp);
1726 if (err)
1727 goto err_destroy_stats;
1728
1729 err = ovs_meters_init(dp);
1730 if (err)
1731 goto err_destroy_ports;
1732
1733 /* Set up our datapath device. */
1734 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1735 parms.type = OVS_VPORT_TYPE_INTERNAL;
1736 parms.options = NULL;
1737 parms.dp = dp;
1738 parms.port_no = OVSP_LOCAL;
1739 parms.upcall_portids = a[OVS_DP_ATTR_UPCALL_PID];
1740
1741 err = ovs_dp_change(dp, a);
1742 if (err)
1743 goto err_destroy_meters;
1744
1745 /* So far only local changes have been made, now need the lock. */
1746 ovs_lock();
1747
1748 vport = new_vport(&parms);
1749 if (IS_ERR(vport)) {
1750 err = PTR_ERR(vport);
1751 if (err == -EBUSY)
1752 err = -EEXIST;
1753
1754 if (err == -EEXIST) {
1755 /* An outdated user space instance that does not understand
1756 * the concept of user_features has attempted to create a new
1757 * datapath and is likely to reuse it. Drop all user features.
1758 */
1759 if (info->genlhdr->version < OVS_DP_VER_FEATURES)
1760 ovs_dp_reset_user_features(skb, info);
1761 }
1762
1763 ovs_unlock();
1764 goto err_destroy_meters;
1765 }
1766
1767 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1768 info->snd_seq, 0, OVS_DP_CMD_NEW);
1769 BUG_ON(err < 0);
1770
1771 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
1772 list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
1773
1774 ovs_unlock();
1775
1776 ovs_notify(&dp_datapath_genl_family, &ovs_dp_datapath_multicast_group, reply, info);
1777 return 0;
1778
1779 err_destroy_meters:
1780 ovs_meters_exit(dp);
1781 err_destroy_ports:
1782 kfree(dp->ports);
1783 err_destroy_stats:
1784 free_percpu(dp->stats_percpu);
1785 err_destroy_table:
1786 ovs_flow_tbl_destroy(&dp->table);
1787 err_destroy_dp:
1788 kfree(dp);
1789 err_destroy_reply:
1790 kfree_skb(reply);
1791 err:
1792 return err;
1793 }
1794
1795 /* Called with ovs_mutex. */
1796 static void __dp_destroy(struct datapath *dp)
1797 {
1798 int i;
1799
1800 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1801 struct vport *vport;
1802 struct hlist_node *n;
1803
1804 hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
1805 if (vport->port_no != OVSP_LOCAL)
1806 ovs_dp_detach_port(vport);
1807 }
1808
1809 list_del_rcu(&dp->list_node);
1810
1811 /* OVSP_LOCAL is datapath internal port. We need to make sure that
1812 * all ports in datapath are destroyed first before freeing datapath.
1813 */
1814 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
1815
1816 /* RCU destroy the flow table */
1817 call_rcu(&dp->rcu, destroy_dp_rcu);
1818 }
1819
1820 static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1821 {
1822 struct sk_buff *reply;
1823 struct datapath *dp;
1824 int err;
1825
1826 reply = ovs_dp_cmd_alloc_info();
1827 if (!reply)
1828 return -ENOMEM;
1829
1830 ovs_lock();
1831 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1832 err = PTR_ERR(dp);
1833 if (IS_ERR(dp))
1834 goto err_unlock_free;
1835
1836 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1837 info->snd_seq, 0, OVS_DP_CMD_DEL);
1838 BUG_ON(err < 0);
1839
1840 __dp_destroy(dp);
1841 ovs_unlock();
1842
1843 ovs_notify(&dp_datapath_genl_family, &ovs_dp_datapath_multicast_group, reply, info);
1844 return 0;
1845
1846 err_unlock_free:
1847 ovs_unlock();
1848 kfree_skb(reply);
1849 return err;
1850 }
1851
1852 static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1853 {
1854 struct sk_buff *reply;
1855 struct datapath *dp;
1856 int err;
1857
1858 reply = ovs_dp_cmd_alloc_info();
1859 if (!reply)
1860 return -ENOMEM;
1861
1862 ovs_lock();
1863 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1864 err = PTR_ERR(dp);
1865 if (IS_ERR(dp))
1866 goto err_unlock_free;
1867
1868 err = ovs_dp_change(dp, info->attrs);
1869 if (err)
1870 goto err_unlock_free;
1871
1872 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1873 info->snd_seq, 0, OVS_DP_CMD_GET);
1874 BUG_ON(err < 0);
1875
1876 ovs_unlock();
1877
1878 ovs_notify(&dp_datapath_genl_family, &ovs_dp_datapath_multicast_group, reply, info);
1879 return 0;
1880
1881 err_unlock_free:
1882 ovs_unlock();
1883 kfree_skb(reply);
1884 return err;
1885 }
1886
1887 static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1888 {
1889 struct sk_buff *reply;
1890 struct datapath *dp;
1891 int err;
1892
1893 reply = ovs_dp_cmd_alloc_info();
1894 if (!reply)
1895 return -ENOMEM;
1896
1897 ovs_lock();
1898 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1899 if (IS_ERR(dp)) {
1900 err = PTR_ERR(dp);
1901 goto err_unlock_free;
1902 }
1903 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1904 info->snd_seq, 0, OVS_DP_CMD_GET);
1905 BUG_ON(err < 0);
1906 ovs_unlock();
1907
1908 return genlmsg_reply(reply, info);
1909
1910 err_unlock_free:
1911 ovs_unlock();
1912 kfree_skb(reply);
1913 return err;
1914 }
1915
1916 static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1917 {
1918 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
1919 struct datapath *dp;
1920 int skip = cb->args[0];
1921 int i = 0;
1922
1923 ovs_lock();
1924 list_for_each_entry(dp, &ovs_net->dps, list_node) {
1925 if (i >= skip &&
1926 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
1927 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1928 OVS_DP_CMD_GET) < 0)
1929 break;
1930 i++;
1931 }
1932 ovs_unlock();
1933
1934 cb->args[0] = i;
1935
1936 return skb->len;
1937 }
1938
1939 static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1940 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1941 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1942 [OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 },
1943 };
1944
1945 static const struct genl_ops dp_datapath_genl_ops[] = {
1946 { .cmd = OVS_DP_CMD_NEW,
1947 #ifdef HAVE_GENL_VALIDATE_FLAGS
1948 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1949 #endif
1950 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1951 #ifdef HAVE_GENL_OPS_POLICY
1952 .policy = datapath_policy,
1953 #endif
1954 .doit = ovs_dp_cmd_new
1955 },
1956 { .cmd = OVS_DP_CMD_DEL,
1957 #ifdef HAVE_GENL_VALIDATE_FLAGS
1958 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1959 #endif
1960 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1961 #ifdef HAVE_GENL_OPS_POLICY
1962 .policy = datapath_policy,
1963 #endif
1964 .doit = ovs_dp_cmd_del
1965 },
1966 { .cmd = OVS_DP_CMD_GET,
1967 #ifdef HAVE_GENL_VALIDATE_FLAGS
1968 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1969 #endif
1970 .flags = 0, /* OK for unprivileged users. */
1971 #ifdef HAVE_GENL_OPS_POLICY
1972 .policy = datapath_policy,
1973 #endif
1974 .doit = ovs_dp_cmd_get,
1975 .dumpit = ovs_dp_cmd_dump
1976 },
1977 { .cmd = OVS_DP_CMD_SET,
1978 #ifdef HAVE_GENL_VALIDATE_FLAGS
1979 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1980 #endif
1981 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1982 #ifdef HAVE_GENL_OPS_POLICY
1983 .policy = datapath_policy,
1984 #endif
1985 .doit = ovs_dp_cmd_set,
1986 },
1987 };
1988
1989 static struct genl_family dp_datapath_genl_family __ro_after_init = {
1990 .hdrsize = sizeof(struct ovs_header),
1991 .name = OVS_DATAPATH_FAMILY,
1992 .version = OVS_DATAPATH_VERSION,
1993 .maxattr = OVS_DP_ATTR_MAX,
1994 #ifndef HAVE_GENL_OPS_POLICY
1995 .policy = datapath_policy,
1996 #endif
1997 .netnsok = true,
1998 .parallel_ops = true,
1999 .ops = dp_datapath_genl_ops,
2000 .n_ops = ARRAY_SIZE(dp_datapath_genl_ops),
2001 .mcgrps = &ovs_dp_datapath_multicast_group,
2002 .n_mcgrps = 1,
2003 .module = THIS_MODULE,
2004 };
2005
2006 /* Called with ovs_mutex or RCU read lock. */
2007 static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
2008 struct net *net, u32 portid, u32 seq,
2009 u32 flags, u8 cmd, gfp_t gfp)
2010 {
2011 struct ovs_header *ovs_header;
2012 struct ovs_vport_stats vport_stats;
2013 int err;
2014
2015 ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
2016 flags, cmd);
2017 if (!ovs_header)
2018 return -EMSGSIZE;
2019
2020 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
2021
2022 if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
2023 nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
2024 nla_put_string(skb, OVS_VPORT_ATTR_NAME,
2025 ovs_vport_name(vport)) ||
2026 nla_put_u32(skb, OVS_VPORT_ATTR_IFINDEX, vport->dev->ifindex))
2027 goto nla_put_failure;
2028
2029 #ifdef HAVE_PEERNET2ID_ALLOC
2030 if (!net_eq(net, dev_net(vport->dev))) {
2031 int id = peernet2id_alloc(net, dev_net(vport->dev), gfp);
2032
2033 if (nla_put_s32(skb, OVS_VPORT_ATTR_NETNSID, id))
2034 goto nla_put_failure;
2035 }
2036
2037 #endif
2038 ovs_vport_get_stats(vport, &vport_stats);
2039 if (nla_put_64bit(skb, OVS_VPORT_ATTR_STATS,
2040 sizeof(struct ovs_vport_stats), &vport_stats,
2041 OVS_VPORT_ATTR_PAD))
2042 goto nla_put_failure;
2043
2044 if (ovs_vport_get_upcall_portids(vport, skb))
2045 goto nla_put_failure;
2046
2047 err = ovs_vport_get_options(vport, skb);
2048 if (err == -EMSGSIZE)
2049 goto error;
2050
2051 genlmsg_end(skb, ovs_header);
2052 return 0;
2053
2054 nla_put_failure:
2055 err = -EMSGSIZE;
2056 error:
2057 genlmsg_cancel(skb, ovs_header);
2058 return err;
2059 }
2060
2061 static struct sk_buff *ovs_vport_cmd_alloc_info(void)
2062 {
2063 return nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2064 }
2065
2066 /* Called with ovs_mutex, only via ovs_dp_notify_wq(). */
2067 struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, struct net *net,
2068 u32 portid, u32 seq, u8 cmd)
2069 {
2070 struct sk_buff *skb;
2071 int retval;
2072
2073 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2074 if (!skb)
2075 return ERR_PTR(-ENOMEM);
2076
2077 retval = ovs_vport_cmd_fill_info(vport, skb, net, portid, seq, 0, cmd,
2078 GFP_KERNEL);
2079 BUG_ON(retval < 0);
2080
2081 return skb;
2082 }
2083
2084 /* Called with ovs_mutex or RCU read lock. */
2085 static struct vport *lookup_vport(struct net *net,
2086 const struct ovs_header *ovs_header,
2087 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
2088 {
2089 struct datapath *dp;
2090 struct vport *vport;
2091
2092 if (a[OVS_VPORT_ATTR_IFINDEX])
2093 return ERR_PTR(-EOPNOTSUPP);
2094 if (a[OVS_VPORT_ATTR_NAME]) {
2095 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
2096 if (!vport)
2097 return ERR_PTR(-ENODEV);
2098 if (ovs_header->dp_ifindex &&
2099 ovs_header->dp_ifindex != get_dpifindex(vport->dp))
2100 return ERR_PTR(-ENODEV);
2101 return vport;
2102 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
2103 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
2104
2105 if (port_no >= DP_MAX_PORTS)
2106 return ERR_PTR(-EFBIG);
2107
2108 dp = get_dp(net, ovs_header->dp_ifindex);
2109 if (!dp)
2110 return ERR_PTR(-ENODEV);
2111
2112 vport = ovs_vport_ovsl_rcu(dp, port_no);
2113 if (!vport)
2114 return ERR_PTR(-ENODEV);
2115 return vport;
2116 } else
2117 return ERR_PTR(-EINVAL);
2118
2119 }
2120
2121 static unsigned int ovs_get_max_headroom(struct datapath *dp)
2122 {
2123 unsigned int dev_headroom, max_headroom = 0;
2124 struct net_device *dev;
2125 struct vport *vport;
2126 int i;
2127
2128 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
2129 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
2130 dev = vport->dev;
2131 dev_headroom = netdev_get_fwd_headroom(dev);
2132 if (dev_headroom > max_headroom)
2133 max_headroom = dev_headroom;
2134 }
2135 }
2136
2137 return max_headroom;
2138 }
2139
2140 /* Called with ovs_mutex */
2141 static void ovs_update_headroom(struct datapath *dp, unsigned int new_headroom)
2142 {
2143 struct vport *vport;
2144 int i;
2145
2146 dp->max_headroom = new_headroom;
2147 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
2148 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node)
2149 netdev_set_rx_headroom(vport->dev, new_headroom);
2150 }
2151
2152 static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
2153 {
2154 struct nlattr **a = info->attrs;
2155 struct ovs_header *ovs_header = info->userhdr;
2156 struct vport_parms parms;
2157 struct sk_buff *reply;
2158 struct vport *vport;
2159 struct datapath *dp;
2160 unsigned int new_headroom;
2161 u32 port_no;
2162 int err;
2163
2164 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
2165 !a[OVS_VPORT_ATTR_UPCALL_PID])
2166 return -EINVAL;
2167 if (a[OVS_VPORT_ATTR_IFINDEX])
2168 return -EOPNOTSUPP;
2169
2170 port_no = a[OVS_VPORT_ATTR_PORT_NO]
2171 ? nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]) : 0;
2172 if (port_no >= DP_MAX_PORTS)
2173 return -EFBIG;
2174
2175 reply = ovs_vport_cmd_alloc_info();
2176 if (!reply)
2177 return -ENOMEM;
2178
2179 ovs_lock();
2180 restart:
2181 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
2182 err = -ENODEV;
2183 if (!dp)
2184 goto exit_unlock_free;
2185
2186 if (port_no) {
2187 vport = ovs_vport_ovsl(dp, port_no);
2188 err = -EBUSY;
2189 if (vport)
2190 goto exit_unlock_free;
2191 } else {
2192 for (port_no = 1; ; port_no++) {
2193 if (port_no >= DP_MAX_PORTS) {
2194 err = -EFBIG;
2195 goto exit_unlock_free;
2196 }
2197 vport = ovs_vport_ovsl(dp, port_no);
2198 if (!vport)
2199 break;
2200 }
2201 }
2202
2203 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
2204 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
2205 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
2206 parms.dp = dp;
2207 parms.port_no = port_no;
2208 parms.upcall_portids = a[OVS_VPORT_ATTR_UPCALL_PID];
2209
2210 vport = new_vport(&parms);
2211 err = PTR_ERR(vport);
2212 if (IS_ERR(vport)) {
2213 if (err == -EAGAIN)
2214 goto restart;
2215 goto exit_unlock_free;
2216 }
2217
2218 err = ovs_vport_cmd_fill_info(vport, reply, genl_info_net(info),
2219 info->snd_portid, info->snd_seq, 0,
2220 OVS_VPORT_CMD_NEW, GFP_KERNEL);
2221 BUG_ON(err < 0);
2222
2223 new_headroom = netdev_get_fwd_headroom(vport->dev);
2224
2225 if (new_headroom > dp->max_headroom)
2226 ovs_update_headroom(dp, new_headroom);
2227 else
2228 netdev_set_rx_headroom(vport->dev, dp->max_headroom);
2229
2230 ovs_unlock();
2231
2232 ovs_notify(&dp_vport_genl_family, &ovs_dp_vport_multicast_group, reply, info);
2233 return 0;
2234
2235 exit_unlock_free:
2236 ovs_unlock();
2237 kfree_skb(reply);
2238 return err;
2239 }
2240
2241 static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
2242 {
2243 struct nlattr **a = info->attrs;
2244 struct sk_buff *reply;
2245 struct vport *vport;
2246 int err;
2247
2248 reply = ovs_vport_cmd_alloc_info();
2249 if (!reply)
2250 return -ENOMEM;
2251
2252 ovs_lock();
2253 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
2254 err = PTR_ERR(vport);
2255 if (IS_ERR(vport))
2256 goto exit_unlock_free;
2257
2258 if (a[OVS_VPORT_ATTR_TYPE] &&
2259 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
2260 err = -EINVAL;
2261 goto exit_unlock_free;
2262 }
2263
2264 if (a[OVS_VPORT_ATTR_OPTIONS]) {
2265 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
2266 if (err)
2267 goto exit_unlock_free;
2268 }
2269
2270 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
2271 struct nlattr *ids = a[OVS_VPORT_ATTR_UPCALL_PID];
2272
2273 err = ovs_vport_set_upcall_portids(vport, ids);
2274 if (err)
2275 goto exit_unlock_free;
2276 }
2277
2278 err = ovs_vport_cmd_fill_info(vport, reply, genl_info_net(info),
2279 info->snd_portid, info->snd_seq, 0,
2280 OVS_VPORT_CMD_SET, GFP_KERNEL);
2281 BUG_ON(err < 0);
2282 ovs_unlock();
2283
2284 ovs_notify(&dp_vport_genl_family, &ovs_dp_vport_multicast_group, reply, info);
2285 return 0;
2286
2287 exit_unlock_free:
2288 ovs_unlock();
2289 kfree_skb(reply);
2290 return err;
2291 }
2292
2293 static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
2294 {
2295 bool update_headroom = false;
2296 struct nlattr **a = info->attrs;
2297 struct sk_buff *reply;
2298 struct datapath *dp;
2299 struct vport *vport;
2300 unsigned int new_headroom;
2301 int err;
2302
2303 reply = ovs_vport_cmd_alloc_info();
2304 if (!reply)
2305 return -ENOMEM;
2306
2307 ovs_lock();
2308 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
2309 err = PTR_ERR(vport);
2310 if (IS_ERR(vport))
2311 goto exit_unlock_free;
2312
2313 if (vport->port_no == OVSP_LOCAL) {
2314 err = -EINVAL;
2315 goto exit_unlock_free;
2316 }
2317
2318 err = ovs_vport_cmd_fill_info(vport, reply, genl_info_net(info),
2319 info->snd_portid, info->snd_seq, 0,
2320 OVS_VPORT_CMD_DEL, GFP_KERNEL);
2321 BUG_ON(err < 0);
2322
2323 /* the vport deletion may trigger dp headroom update */
2324 dp = vport->dp;
2325 if (netdev_get_fwd_headroom(vport->dev) == dp->max_headroom)
2326 update_headroom = true;
2327
2328 netdev_reset_rx_headroom(vport->dev);
2329 ovs_dp_detach_port(vport);
2330
2331 if (update_headroom) {
2332 new_headroom = ovs_get_max_headroom(dp);
2333
2334 if (new_headroom < dp->max_headroom)
2335 ovs_update_headroom(dp, new_headroom);
2336 }
2337 ovs_unlock();
2338
2339 ovs_notify(&dp_vport_genl_family, &ovs_dp_vport_multicast_group, reply, info);
2340 return 0;
2341
2342 exit_unlock_free:
2343 ovs_unlock();
2344 kfree_skb(reply);
2345 return err;
2346 }
2347
2348 static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
2349 {
2350 struct nlattr **a = info->attrs;
2351 struct ovs_header *ovs_header = info->userhdr;
2352 struct sk_buff *reply;
2353 struct vport *vport;
2354 int err;
2355
2356 reply = ovs_vport_cmd_alloc_info();
2357 if (!reply)
2358 return -ENOMEM;
2359
2360 rcu_read_lock();
2361 vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
2362 err = PTR_ERR(vport);
2363 if (IS_ERR(vport))
2364 goto exit_unlock_free;
2365 err = ovs_vport_cmd_fill_info(vport, reply, genl_info_net(info),
2366 info->snd_portid, info->snd_seq, 0,
2367 OVS_VPORT_CMD_GET, GFP_ATOMIC);
2368 BUG_ON(err < 0);
2369 rcu_read_unlock();
2370
2371 return genlmsg_reply(reply, info);
2372
2373 exit_unlock_free:
2374 rcu_read_unlock();
2375 kfree_skb(reply);
2376 return err;
2377 }
2378
2379 static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
2380 {
2381 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
2382 struct datapath *dp;
2383 int bucket = cb->args[0], skip = cb->args[1];
2384 int i, j = 0;
2385
2386 rcu_read_lock();
2387 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
2388 if (!dp) {
2389 rcu_read_unlock();
2390 return -ENODEV;
2391 }
2392 for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
2393 struct vport *vport;
2394
2395 j = 0;
2396 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
2397 if (j >= skip &&
2398 ovs_vport_cmd_fill_info(vport, skb,
2399 sock_net(skb->sk),
2400 NETLINK_CB(cb->skb).portid,
2401 cb->nlh->nlmsg_seq,
2402 NLM_F_MULTI,
2403 OVS_VPORT_CMD_GET,
2404 GFP_ATOMIC) < 0)
2405 goto out;
2406
2407 j++;
2408 }
2409 skip = 0;
2410 }
2411 out:
2412 rcu_read_unlock();
2413
2414 cb->args[0] = i;
2415 cb->args[1] = j;
2416
2417 return skb->len;
2418 }
2419
2420 static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
2421 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
2422 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
2423 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
2424 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
2425 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_UNSPEC },
2426 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
2427 [OVS_VPORT_ATTR_IFINDEX] = { .type = NLA_U32 },
2428 [OVS_VPORT_ATTR_NETNSID] = { .type = NLA_S32 },
2429 };
2430
2431 static const struct genl_ops dp_vport_genl_ops[] = {
2432 { .cmd = OVS_VPORT_CMD_NEW,
2433 #ifdef HAVE_GENL_VALIDATE_FLAGS
2434 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2435 #endif
2436 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2437 #ifdef HAVE_GENL_OPS_POLICY
2438 .policy = vport_policy,
2439 #endif
2440 .doit = ovs_vport_cmd_new
2441 },
2442 { .cmd = OVS_VPORT_CMD_DEL,
2443 #ifdef HAVE_GENL_VALIDATE_FLAGS
2444 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2445 #endif
2446 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2447 #ifdef HAVE_GENL_OPS_POLICY
2448 .policy = vport_policy,
2449 #endif
2450 .doit = ovs_vport_cmd_del
2451 },
2452 { .cmd = OVS_VPORT_CMD_GET,
2453 #ifdef HAVE_GENL_VALIDATE_FLAGS
2454 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2455 #endif
2456 .flags = 0, /* OK for unprivileged users. */
2457 #ifdef HAVE_GENL_OPS_POLICY
2458 .policy = vport_policy,
2459 #endif
2460 .doit = ovs_vport_cmd_get,
2461 .dumpit = ovs_vport_cmd_dump
2462 },
2463 { .cmd = OVS_VPORT_CMD_SET,
2464 #ifdef HAVE_GENL_VALIDATE_FLAGS
2465 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2466 #endif
2467 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2468 #ifdef HAVE_GENL_OPS_POLICY
2469 .policy = vport_policy,
2470 #endif
2471 .doit = ovs_vport_cmd_set,
2472 },
2473 };
2474
2475 struct genl_family dp_vport_genl_family __ro_after_init = {
2476 .hdrsize = sizeof(struct ovs_header),
2477 .name = OVS_VPORT_FAMILY,
2478 .version = OVS_VPORT_VERSION,
2479 .maxattr = OVS_VPORT_ATTR_MAX,
2480 #ifndef HAVE_GENL_OPS_POLICY
2481 .policy = vport_policy,
2482 #endif
2483 .netnsok = true,
2484 .parallel_ops = true,
2485 .ops = dp_vport_genl_ops,
2486 .n_ops = ARRAY_SIZE(dp_vport_genl_ops),
2487 .mcgrps = &ovs_dp_vport_multicast_group,
2488 .n_mcgrps = 1,
2489 .module = THIS_MODULE,
2490 };
2491
2492 static struct genl_family *dp_genl_families[] = {
2493 &dp_datapath_genl_family,
2494 &dp_vport_genl_family,
2495 &dp_flow_genl_family,
2496 &dp_packet_genl_family,
2497 &dp_meter_genl_family,
2498 #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
2499 &dp_ct_limit_genl_family,
2500 #endif
2501 };
2502
2503 static void dp_unregister_genl(int n_families)
2504 {
2505 int i;
2506
2507 for (i = 0; i < n_families; i++)
2508 genl_unregister_family(dp_genl_families[i]);
2509 }
2510
2511 static int __init dp_register_genl(void)
2512 {
2513 int err;
2514 int i;
2515
2516 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
2517
2518 err = genl_register_family(dp_genl_families[i]);
2519 if (err)
2520 goto error;
2521 }
2522
2523 return 0;
2524
2525 error:
2526 dp_unregister_genl(i);
2527 return err;
2528 }
2529
2530 static int __net_init ovs_init_net(struct net *net)
2531 {
2532 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2533
2534 INIT_LIST_HEAD(&ovs_net->dps);
2535 INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
2536 ovs_netns_frags_init(net);
2537 ovs_netns_frags6_init(net);
2538 return ovs_ct_init(net);
2539 }
2540
2541 static void __net_exit list_vports_from_net(struct net *net, struct net *dnet,
2542 struct list_head *head)
2543 {
2544 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2545 struct datapath *dp;
2546
2547 list_for_each_entry(dp, &ovs_net->dps, list_node) {
2548 int i;
2549
2550 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
2551 struct vport *vport;
2552
2553 hlist_for_each_entry(vport, &dp->ports[i], dp_hash_node) {
2554
2555 if (vport->ops->type != OVS_VPORT_TYPE_INTERNAL)
2556 continue;
2557
2558 if (dev_net(vport->dev) == dnet)
2559 list_add(&vport->detach_list, head);
2560 }
2561 }
2562 }
2563 }
2564
2565 static void __net_exit ovs_exit_net(struct net *dnet)
2566 {
2567 struct datapath *dp, *dp_next;
2568 struct ovs_net *ovs_net = net_generic(dnet, ovs_net_id);
2569 struct vport *vport, *vport_next;
2570 struct net *net;
2571 LIST_HEAD(head);
2572
2573 ovs_netns_frags6_exit(dnet);
2574 ovs_netns_frags_exit(dnet);
2575 ovs_lock();
2576
2577 ovs_ct_exit(dnet);
2578
2579 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
2580 __dp_destroy(dp);
2581
2582 #ifdef HAVE_NET_RWSEM
2583 down_read(&net_rwsem);
2584 #else
2585 rtnl_lock();
2586 #endif
2587 for_each_net(net)
2588 list_vports_from_net(net, dnet, &head);
2589 #ifdef HAVE_NET_RWSEM
2590 up_read(&net_rwsem);
2591 #else
2592 rtnl_unlock();
2593 #endif
2594
2595 /* Detach all vports from given namespace. */
2596 list_for_each_entry_safe(vport, vport_next, &head, detach_list) {
2597 list_del(&vport->detach_list);
2598 ovs_dp_detach_port(vport);
2599 }
2600
2601 ovs_unlock();
2602
2603 cancel_work_sync(&ovs_net->dp_notify_work);
2604 }
2605
2606 static struct pernet_operations ovs_net_ops = {
2607 .init = ovs_init_net,
2608 .exit = ovs_exit_net,
2609 .id = &ovs_net_id,
2610 .size = sizeof(struct ovs_net),
2611 };
2612
2613 static int __init dp_init(void)
2614 {
2615 int err;
2616
2617 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > sizeof_field(struct sk_buff, cb));
2618
2619 pr_info("Open vSwitch switching datapath %s\n", VERSION);
2620
2621 ovs_nsh_init();
2622 err = action_fifos_init();
2623 if (err)
2624 goto error;
2625
2626 err = ovs_internal_dev_rtnl_link_register();
2627 if (err)
2628 goto error_action_fifos_exit;
2629
2630 err = ovs_flow_init();
2631 if (err)
2632 goto error_unreg_rtnl_link;
2633
2634 err = ovs_vport_init();
2635 if (err)
2636 goto error_flow_exit;
2637
2638 err = register_pernet_device(&ovs_net_ops);
2639 if (err)
2640 goto error_vport_exit;
2641
2642 err = compat_init();
2643 if (err)
2644 goto error_netns_exit;
2645
2646 err = register_netdevice_notifier(&ovs_dp_device_notifier);
2647 if (err)
2648 goto error_compat_exit;
2649
2650 err = ovs_netdev_init();
2651 if (err)
2652 goto error_unreg_notifier;
2653
2654 err = dp_register_genl();
2655 if (err < 0)
2656 goto error_unreg_netdev;
2657
2658 return 0;
2659
2660 error_unreg_netdev:
2661 ovs_netdev_exit();
2662 error_unreg_notifier:
2663 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2664 error_compat_exit:
2665 compat_exit();
2666 error_netns_exit:
2667 unregister_pernet_device(&ovs_net_ops);
2668 error_vport_exit:
2669 ovs_vport_exit();
2670 error_flow_exit:
2671 ovs_flow_exit();
2672 error_unreg_rtnl_link:
2673 ovs_internal_dev_rtnl_link_unregister();
2674 error_action_fifos_exit:
2675 action_fifos_exit();
2676 error:
2677 ovs_nsh_cleanup();
2678 return err;
2679 }
2680
2681 static void dp_cleanup(void)
2682 {
2683 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2684 ovs_netdev_exit();
2685 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2686 compat_exit();
2687 unregister_pernet_device(&ovs_net_ops);
2688 rcu_barrier();
2689 ovs_vport_exit();
2690 ovs_flow_exit();
2691 ovs_internal_dev_rtnl_link_unregister();
2692 action_fifos_exit();
2693 ovs_nsh_cleanup();
2694 }
2695
2696 module_init(dp_init);
2697 module_exit(dp_cleanup);
2698
2699 MODULE_DESCRIPTION("Open vSwitch switching datapath");
2700 MODULE_LICENSE("GPL");
2701 MODULE_VERSION(VERSION);
2702 MODULE_ALIAS_GENL_FAMILY(OVS_DATAPATH_FAMILY);
2703 MODULE_ALIAS_GENL_FAMILY(OVS_VPORT_FAMILY);
2704 MODULE_ALIAS_GENL_FAMILY(OVS_FLOW_FAMILY);
2705 MODULE_ALIAS_GENL_FAMILY(OVS_PACKET_FAMILY);
2706 MODULE_ALIAS_GENL_FAMILY(OVS_METER_FAMILY);
2707 MODULE_ALIAS_GENL_FAMILY(OVS_CT_LIMIT_FAMILY);