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