1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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/ethtool.h>
40#include <linux/wait.h>
41#include <asm/div64.h>
42#include <linux/highmem.h>
43#include <linux/netfilter_bridge.h>
44#include <linux/netfilter_ipv4.h>
45#include <linux/inetdevice.h>
46#include <linux/list.h>
47#include <linux/openvswitch.h>
48#include <linux/rculist.h>
49#include <linux/dmi.h>
50#include <net/genetlink.h>
51#include <net/net_namespace.h>
52#include <net/netns/generic.h>
53
54#include "datapath.h"
55#include "flow.h"
56#include "flow_table.h"
57#include "flow_netlink.h"
58#include "vport-internal_dev.h"
59#include "vport-netdev.h"
60
61int ovs_net_id __read_mostly;
62
63static struct genl_family dp_packet_genl_family;
64static struct genl_family dp_flow_genl_family;
65static struct genl_family dp_datapath_genl_family;
66
67static const struct genl_multicast_group ovs_dp_flow_multicast_group = {
68 .name = OVS_FLOW_MCGROUP,
69};
70
71static const struct genl_multicast_group ovs_dp_datapath_multicast_group = {
72 .name = OVS_DATAPATH_MCGROUP,
73};
74
75static const struct genl_multicast_group ovs_dp_vport_multicast_group = {
76 .name = OVS_VPORT_MCGROUP,
77};
78
79
80
81static bool ovs_must_notify(struct genl_family *family, struct genl_info *info,
82 unsigned int group)
83{
84 return info->nlhdr->nlmsg_flags & NLM_F_ECHO ||
85 genl_has_listeners(family, genl_info_net(info)->genl_sock,
86 group);
87}
88
89static void ovs_notify(struct genl_family *family,
90 struct sk_buff *skb, struct genl_info *info)
91{
92 genl_notify(family, skb, genl_info_net(info), info->snd_portid,
93 0, info->nlhdr, GFP_KERNEL);
94}
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113static DEFINE_MUTEX(ovs_mutex);
114
115void ovs_lock(void)
116{
117 mutex_lock(&ovs_mutex);
118}
119
120void ovs_unlock(void)
121{
122 mutex_unlock(&ovs_mutex);
123}
124
125#ifdef CONFIG_LOCKDEP
126int lockdep_ovsl_is_held(void)
127{
128 if (debug_locks)
129 return lockdep_is_held(&ovs_mutex);
130 else
131 return 1;
132}
133#endif
134
135static struct vport *new_vport(const struct vport_parms *);
136static int queue_gso_packets(struct datapath *dp, struct sk_buff *,
137 const struct dp_upcall_info *);
138static int queue_userspace_packet(struct datapath *dp, struct sk_buff *,
139 const struct dp_upcall_info *);
140
141
142static struct datapath *get_dp(struct net *net, int dp_ifindex)
143{
144 struct datapath *dp = NULL;
145 struct net_device *dev;
146
147 rcu_read_lock();
148 dev = dev_get_by_index_rcu(net, dp_ifindex);
149 if (dev) {
150 struct vport *vport = ovs_internal_dev_get_vport(dev);
151 if (vport)
152 dp = vport->dp;
153 }
154 rcu_read_unlock();
155
156 return dp;
157}
158
159
160const char *ovs_dp_name(const struct datapath *dp)
161{
162 struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL);
163 return vport->ops->get_name(vport);
164}
165
166static int get_dpifindex(struct datapath *dp)
167{
168 struct vport *local;
169 int ifindex;
170
171 rcu_read_lock();
172
173 local = ovs_vport_rcu(dp, OVSP_LOCAL);
174 if (local)
175 ifindex = netdev_vport_priv(local)->dev->ifindex;
176 else
177 ifindex = 0;
178
179 rcu_read_unlock();
180
181 return ifindex;
182}
183
184static void destroy_dp_rcu(struct rcu_head *rcu)
185{
186 struct datapath *dp = container_of(rcu, struct datapath, rcu);
187
188 free_percpu(dp->stats_percpu);
189 release_net(ovs_dp_get_net(dp));
190 kfree(dp->ports);
191 kfree(dp);
192}
193
194static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
195 u16 port_no)
196{
197 return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
198}
199
200
201struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
202{
203 struct vport *vport;
204 struct hlist_head *head;
205
206 head = vport_hash_bucket(dp, port_no);
207 hlist_for_each_entry_rcu(vport, head, dp_hash_node) {
208 if (vport->port_no == port_no)
209 return vport;
210 }
211 return NULL;
212}
213
214
215static struct vport *new_vport(const struct vport_parms *parms)
216{
217 struct vport *vport;
218
219 vport = ovs_vport_add(parms);
220 if (!IS_ERR(vport)) {
221 struct datapath *dp = parms->dp;
222 struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
223
224 hlist_add_head_rcu(&vport->dp_hash_node, head);
225 }
226 return vport;
227}
228
229void ovs_dp_detach_port(struct vport *p)
230{
231 ASSERT_OVSL();
232
233
234 hlist_del_rcu(&p->dp_hash_node);
235
236
237 ovs_vport_del(p);
238}
239
240
241void ovs_dp_process_packet(struct sk_buff *skb, struct sw_flow_key *key)
242{
243 const struct vport *p = OVS_CB(skb)->input_vport;
244 struct datapath *dp = p->dp;
245 struct sw_flow *flow;
246 struct dp_stats_percpu *stats;
247 u64 *stats_counter;
248 u32 n_mask_hit;
249
250 stats = this_cpu_ptr(dp->stats_percpu);
251
252
253 flow = ovs_flow_tbl_lookup_stats(&dp->table, key, &n_mask_hit);
254 if (unlikely(!flow)) {
255 struct dp_upcall_info upcall;
256 int error;
257
258 upcall.cmd = OVS_PACKET_CMD_MISS;
259 upcall.key = key;
260 upcall.userdata = NULL;
261 upcall.portid = ovs_vport_find_upcall_portid(p, skb);
262 error = ovs_dp_upcall(dp, skb, &upcall);
263 if (unlikely(error))
264 kfree_skb(skb);
265 else
266 consume_skb(skb);
267 stats_counter = &stats->n_missed;
268 goto out;
269 }
270
271 OVS_CB(skb)->flow = flow;
272
273 ovs_flow_stats_update(OVS_CB(skb)->flow, key->tp.flags, skb);
274 ovs_execute_actions(dp, skb, key);
275 stats_counter = &stats->n_hit;
276
277out:
278
279 u64_stats_update_begin(&stats->syncp);
280 (*stats_counter)++;
281 stats->n_mask_hit += n_mask_hit;
282 u64_stats_update_end(&stats->syncp);
283}
284
285int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
286 const struct dp_upcall_info *upcall_info)
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, upcall_info);
298 else
299 err = queue_gso_packets(dp, skb, upcall_info);
300 if (err)
301 goto err;
302
303 return 0;
304
305err:
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
315static int queue_gso_packets(struct datapath *dp, struct sk_buff *skb,
316 const struct dp_upcall_info *upcall_info)
317{
318 unsigned short gso_type = skb_shinfo(skb)->gso_type;
319 struct dp_upcall_info later_info;
320 struct sw_flow_key later_key;
321 struct sk_buff *segs, *nskb;
322 int err;
323
324 segs = __skb_gso_segment(skb, NETIF_F_SG, false);
325 if (IS_ERR(segs))
326 return PTR_ERR(segs);
327 if (segs == NULL)
328 return -EINVAL;
329
330
331 skb = segs;
332 do {
333 err = queue_userspace_packet(dp, skb, upcall_info);
334 if (err)
335 break;
336
337 if (skb == segs && gso_type & SKB_GSO_UDP) {
338
339
340
341
342 later_key = *upcall_info->key;
343 later_key.ip.frag = OVS_FRAG_TYPE_LATER;
344
345 later_info = *upcall_info;
346 later_info.key = &later_key;
347 upcall_info = &later_info;
348 }
349 } while ((skb = skb->next));
350
351
352 skb = segs;
353 do {
354 nskb = skb->next;
355 if (err)
356 kfree_skb(skb);
357 else
358 consume_skb(skb);
359 } while ((skb = nskb));
360 return err;
361}
362
363static size_t key_attr_size(void)
364{
365 return nla_total_size(4)
366 + nla_total_size(0)
367 + nla_total_size(8)
368 + nla_total_size(4)
369 + nla_total_size(4)
370 + nla_total_size(1)
371 + nla_total_size(1)
372 + nla_total_size(0)
373 + nla_total_size(0)
374 + nla_total_size(0)
375 + nla_total_size(256)
376 + nla_total_size(4)
377 + nla_total_size(4)
378 + nla_total_size(12)
379 + nla_total_size(2)
380 + nla_total_size(4)
381 + nla_total_size(0)
382 + nla_total_size(2)
383 + nla_total_size(40)
384 + nla_total_size(2)
385 + nla_total_size(28);
386}
387
388static size_t upcall_msg_size(const struct nlattr *userdata,
389 unsigned int hdrlen)
390{
391 size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
392 + nla_total_size(hdrlen)
393 + nla_total_size(key_attr_size());
394
395
396 if (userdata)
397 size += NLA_ALIGN(userdata->nla_len);
398
399 return size;
400}
401
402static int queue_userspace_packet(struct datapath *dp, struct sk_buff *skb,
403 const struct dp_upcall_info *upcall_info)
404{
405 struct ovs_header *upcall;
406 struct sk_buff *nskb = NULL;
407 struct sk_buff *user_skb = NULL;
408 struct nlattr *nla;
409 struct genl_info info = {
410 .dst_sk = ovs_dp_get_net(dp)->genl_sock,
411 .snd_portid = upcall_info->portid,
412 };
413 size_t len;
414 unsigned int hlen;
415 int err, dp_ifindex;
416
417 dp_ifindex = get_dpifindex(dp);
418 if (!dp_ifindex)
419 return -ENODEV;
420
421 if (vlan_tx_tag_present(skb)) {
422 nskb = skb_clone(skb, GFP_ATOMIC);
423 if (!nskb)
424 return -ENOMEM;
425
426 nskb = __vlan_put_tag(nskb, nskb->vlan_proto, vlan_tx_tag_get(nskb));
427 if (!nskb)
428 return -ENOMEM;
429
430 nskb->vlan_tci = 0;
431 skb = nskb;
432 }
433
434 if (nla_attr_size(skb->len) > USHRT_MAX) {
435 err = -EFBIG;
436 goto out;
437 }
438
439
440 if (skb->ip_summed == CHECKSUM_PARTIAL &&
441 (err = skb_checksum_help(skb)))
442 goto out;
443
444
445
446
447
448 if (dp->user_features & OVS_DP_F_UNALIGNED)
449 hlen = skb_zerocopy_headlen(skb);
450 else
451 hlen = skb->len;
452
453 len = upcall_msg_size(upcall_info->userdata, hlen);
454 user_skb = genlmsg_new_unicast(len, &info, GFP_ATOMIC);
455 if (!user_skb) {
456 err = -ENOMEM;
457 goto out;
458 }
459
460 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
461 0, upcall_info->cmd);
462 upcall->dp_ifindex = dp_ifindex;
463
464 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
465 err = ovs_nla_put_flow(upcall_info->key, upcall_info->key, user_skb);
466 BUG_ON(err);
467 nla_nest_end(user_skb, nla);
468
469 if (upcall_info->userdata)
470 __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
471 nla_len(upcall_info->userdata),
472 nla_data(upcall_info->userdata));
473
474
475
476 if (!(nla = nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, 0))) {
477 err = -ENOBUFS;
478 goto out;
479 }
480 nla->nla_len = nla_attr_size(skb->len);
481
482 err = skb_zerocopy(user_skb, skb, skb->len, hlen);
483 if (err)
484 goto out;
485
486
487 if (!(dp->user_features & OVS_DP_F_UNALIGNED)) {
488 size_t plen = NLA_ALIGN(user_skb->len) - user_skb->len;
489
490 if (plen > 0)
491 memset(skb_put(user_skb, plen), 0, plen);
492 }
493
494 ((struct nlmsghdr *) user_skb->data)->nlmsg_len = user_skb->len;
495
496 err = genlmsg_unicast(ovs_dp_get_net(dp), user_skb, upcall_info->portid);
497 user_skb = NULL;
498out:
499 if (err)
500 skb_tx_error(skb);
501 kfree_skb(user_skb);
502 kfree_skb(nskb);
503 return err;
504}
505
506static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
507{
508 struct ovs_header *ovs_header = info->userhdr;
509 struct nlattr **a = info->attrs;
510 struct sw_flow_actions *acts;
511 struct sk_buff *packet;
512 struct sw_flow *flow;
513 struct datapath *dp;
514 struct ethhdr *eth;
515 struct vport *input_vport;
516 int len;
517 int err;
518
519 err = -EINVAL;
520 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
521 !a[OVS_PACKET_ATTR_ACTIONS])
522 goto err;
523
524 len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
525 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
526 err = -ENOMEM;
527 if (!packet)
528 goto err;
529 skb_reserve(packet, NET_IP_ALIGN);
530
531 nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
532
533 skb_reset_mac_header(packet);
534 eth = eth_hdr(packet);
535
536
537
538
539 if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN)
540 packet->protocol = eth->h_proto;
541 else
542 packet->protocol = htons(ETH_P_802_2);
543
544
545 flow = ovs_flow_alloc();
546 err = PTR_ERR(flow);
547 if (IS_ERR(flow))
548 goto err_kfree_skb;
549
550 err = ovs_flow_key_extract_userspace(a[OVS_PACKET_ATTR_KEY], packet,
551 &flow->key);
552 if (err)
553 goto err_flow_free;
554
555 acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_PACKET_ATTR_ACTIONS]));
556 err = PTR_ERR(acts);
557 if (IS_ERR(acts))
558 goto err_flow_free;
559
560 err = ovs_nla_copy_actions(a[OVS_PACKET_ATTR_ACTIONS],
561 &flow->key, 0, &acts);
562 if (err)
563 goto err_flow_free;
564
565 rcu_assign_pointer(flow->sf_acts, acts);
566
567 OVS_CB(packet)->egress_tun_info = NULL;
568 OVS_CB(packet)->flow = flow;
569 packet->priority = flow->key.phy.priority;
570 packet->mark = flow->key.phy.skb_mark;
571
572 rcu_read_lock();
573 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
574 err = -ENODEV;
575 if (!dp)
576 goto err_unlock;
577
578 input_vport = ovs_vport_rcu(dp, flow->key.phy.in_port);
579 if (!input_vport)
580 input_vport = ovs_vport_rcu(dp, OVSP_LOCAL);
581
582 if (!input_vport)
583 goto err_unlock;
584
585 OVS_CB(packet)->input_vport = input_vport;
586
587 local_bh_disable();
588 err = ovs_execute_actions(dp, packet, &flow->key);
589 local_bh_enable();
590 rcu_read_unlock();
591
592 ovs_flow_free(flow, false);
593 return err;
594
595err_unlock:
596 rcu_read_unlock();
597err_flow_free:
598 ovs_flow_free(flow, false);
599err_kfree_skb:
600 kfree_skb(packet);
601err:
602 return err;
603}
604
605static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
606 [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
607 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
608 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
609};
610
611static const struct genl_ops dp_packet_genl_ops[] = {
612 { .cmd = OVS_PACKET_CMD_EXECUTE,
613 .flags = GENL_ADMIN_PERM,
614 .policy = packet_policy,
615 .doit = ovs_packet_cmd_execute
616 }
617};
618
619static struct genl_family dp_packet_genl_family = {
620 .id = GENL_ID_GENERATE,
621 .hdrsize = sizeof(struct ovs_header),
622 .name = OVS_PACKET_FAMILY,
623 .version = OVS_PACKET_VERSION,
624 .maxattr = OVS_PACKET_ATTR_MAX,
625 .netnsok = true,
626 .parallel_ops = true,
627 .ops = dp_packet_genl_ops,
628 .n_ops = ARRAY_SIZE(dp_packet_genl_ops),
629};
630
631static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats,
632 struct ovs_dp_megaflow_stats *mega_stats)
633{
634 int i;
635
636 memset(mega_stats, 0, sizeof(*mega_stats));
637
638 stats->n_flows = ovs_flow_tbl_count(&dp->table);
639 mega_stats->n_masks = ovs_flow_tbl_num_masks(&dp->table);
640
641 stats->n_hit = stats->n_missed = stats->n_lost = 0;
642
643 for_each_possible_cpu(i) {
644 const struct dp_stats_percpu *percpu_stats;
645 struct dp_stats_percpu local_stats;
646 unsigned int start;
647
648 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
649
650 do {
651 start = u64_stats_fetch_begin_irq(&percpu_stats->syncp);
652 local_stats = *percpu_stats;
653 } while (u64_stats_fetch_retry_irq(&percpu_stats->syncp, start));
654
655 stats->n_hit += local_stats.n_hit;
656 stats->n_missed += local_stats.n_missed;
657 stats->n_lost += local_stats.n_lost;
658 mega_stats->n_mask_hit += local_stats.n_mask_hit;
659 }
660}
661
662static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts)
663{
664 return NLMSG_ALIGN(sizeof(struct ovs_header))
665 + nla_total_size(key_attr_size())
666 + nla_total_size(key_attr_size())
667 + nla_total_size(sizeof(struct ovs_flow_stats))
668 + nla_total_size(1)
669 + nla_total_size(8)
670 + nla_total_size(acts->actions_len);
671}
672
673
674static int ovs_flow_cmd_fill_info(const struct sw_flow *flow, int dp_ifindex,
675 struct sk_buff *skb, u32 portid,
676 u32 seq, u32 flags, u8 cmd)
677{
678 const int skb_orig_len = skb->len;
679 struct nlattr *start;
680 struct ovs_flow_stats stats;
681 __be16 tcp_flags;
682 unsigned long used;
683 struct ovs_header *ovs_header;
684 struct nlattr *nla;
685 int err;
686
687 ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd);
688 if (!ovs_header)
689 return -EMSGSIZE;
690
691 ovs_header->dp_ifindex = dp_ifindex;
692
693
694 nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
695 if (!nla)
696 goto nla_put_failure;
697
698 err = ovs_nla_put_flow(&flow->unmasked_key, &flow->unmasked_key, skb);
699 if (err)
700 goto error;
701 nla_nest_end(skb, nla);
702
703 nla = nla_nest_start(skb, OVS_FLOW_ATTR_MASK);
704 if (!nla)
705 goto nla_put_failure;
706
707 err = ovs_nla_put_flow(&flow->key, &flow->mask->key, skb);
708 if (err)
709 goto error;
710
711 nla_nest_end(skb, nla);
712
713 ovs_flow_stats_get(flow, &stats, &used, &tcp_flags);
714
715 if (used &&
716 nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
717 goto nla_put_failure;
718
719 if (stats.n_packets &&
720 nla_put(skb, OVS_FLOW_ATTR_STATS, sizeof(struct ovs_flow_stats), &stats))
721 goto nla_put_failure;
722
723 if ((u8)ntohs(tcp_flags) &&
724 nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, (u8)ntohs(tcp_flags)))
725 goto nla_put_failure;
726
727
728
729
730
731
732
733
734
735
736
737 start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
738 if (start) {
739 const struct sw_flow_actions *sf_acts;
740
741 sf_acts = rcu_dereference_ovsl(flow->sf_acts);
742 err = ovs_nla_put_actions(sf_acts->actions,
743 sf_acts->actions_len, skb);
744
745 if (!err)
746 nla_nest_end(skb, start);
747 else {
748 if (skb_orig_len)
749 goto error;
750
751 nla_nest_cancel(skb, start);
752 }
753 } else if (skb_orig_len)
754 goto nla_put_failure;
755
756 return genlmsg_end(skb, ovs_header);
757
758nla_put_failure:
759 err = -EMSGSIZE;
760error:
761 genlmsg_cancel(skb, ovs_header);
762 return err;
763}
764
765
766static struct sk_buff *ovs_flow_cmd_alloc_info(const struct sw_flow_actions *acts,
767 struct genl_info *info,
768 bool always)
769{
770 struct sk_buff *skb;
771
772 if (!always && !ovs_must_notify(&dp_flow_genl_family, info, 0))
773 return NULL;
774
775 skb = genlmsg_new_unicast(ovs_flow_cmd_msg_size(acts), info, GFP_KERNEL);
776 if (!skb)
777 return ERR_PTR(-ENOMEM);
778
779 return skb;
780}
781
782
783static struct sk_buff *ovs_flow_cmd_build_info(const struct sw_flow *flow,
784 int dp_ifindex,
785 struct genl_info *info, u8 cmd,
786 bool always)
787{
788 struct sk_buff *skb;
789 int retval;
790
791 skb = ovs_flow_cmd_alloc_info(ovsl_dereference(flow->sf_acts), info,
792 always);
793 if (IS_ERR_OR_NULL(skb))
794 return skb;
795
796 retval = ovs_flow_cmd_fill_info(flow, dp_ifindex, skb,
797 info->snd_portid, info->snd_seq, 0,
798 cmd);
799 BUG_ON(retval < 0);
800 return skb;
801}
802
803static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info)
804{
805 struct nlattr **a = info->attrs;
806 struct ovs_header *ovs_header = info->userhdr;
807 struct sw_flow *flow, *new_flow;
808 struct sw_flow_mask mask;
809 struct sk_buff *reply;
810 struct datapath *dp;
811 struct sw_flow_actions *acts;
812 struct sw_flow_match match;
813 int error;
814
815
816 error = -EINVAL;
817 if (!a[OVS_FLOW_ATTR_KEY])
818 goto error;
819 if (!a[OVS_FLOW_ATTR_ACTIONS])
820 goto error;
821
822
823
824
825 new_flow = ovs_flow_alloc();
826 if (IS_ERR(new_flow)) {
827 error = PTR_ERR(new_flow);
828 goto error;
829 }
830
831
832 ovs_match_init(&match, &new_flow->unmasked_key, &mask);
833 error = ovs_nla_get_match(&match,
834 a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
835 if (error)
836 goto err_kfree_flow;
837
838 ovs_flow_mask_key(&new_flow->key, &new_flow->unmasked_key, &mask);
839
840
841 acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_FLOW_ATTR_ACTIONS]));
842 error = PTR_ERR(acts);
843 if (IS_ERR(acts))
844 goto err_kfree_flow;
845
846 error = ovs_nla_copy_actions(a[OVS_FLOW_ATTR_ACTIONS], &new_flow->key,
847 0, &acts);
848 if (error) {
849 OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
850 goto err_kfree_acts;
851 }
852
853 reply = ovs_flow_cmd_alloc_info(acts, info, false);
854 if (IS_ERR(reply)) {
855 error = PTR_ERR(reply);
856 goto err_kfree_acts;
857 }
858
859 ovs_lock();
860 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
861 if (unlikely(!dp)) {
862 error = -ENODEV;
863 goto err_unlock_ovs;
864 }
865
866 flow = ovs_flow_tbl_lookup(&dp->table, &new_flow->unmasked_key);
867 if (likely(!flow)) {
868 rcu_assign_pointer(new_flow->sf_acts, acts);
869
870
871 error = ovs_flow_tbl_insert(&dp->table, new_flow, &mask);
872 if (unlikely(error)) {
873 acts = NULL;
874 goto err_unlock_ovs;
875 }
876
877 if (unlikely(reply)) {
878 error = ovs_flow_cmd_fill_info(new_flow,
879 ovs_header->dp_ifindex,
880 reply, info->snd_portid,
881 info->snd_seq, 0,
882 OVS_FLOW_CMD_NEW);
883 BUG_ON(error < 0);
884 }
885 ovs_unlock();
886 } else {
887 struct sw_flow_actions *old_acts;
888
889
890
891
892
893
894
895 if (unlikely(info->nlhdr->nlmsg_flags & (NLM_F_CREATE
896 | NLM_F_EXCL))) {
897 error = -EEXIST;
898 goto err_unlock_ovs;
899 }
900
901 if (unlikely(!ovs_flow_cmp_unmasked_key(flow, &match))) {
902 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
903 if (!flow) {
904 error = -ENOENT;
905 goto err_unlock_ovs;
906 }
907 }
908
909 old_acts = ovsl_dereference(flow->sf_acts);
910 rcu_assign_pointer(flow->sf_acts, acts);
911
912 if (unlikely(reply)) {
913 error = ovs_flow_cmd_fill_info(flow,
914 ovs_header->dp_ifindex,
915 reply, info->snd_portid,
916 info->snd_seq, 0,
917 OVS_FLOW_CMD_NEW);
918 BUG_ON(error < 0);
919 }
920 ovs_unlock();
921
922 ovs_nla_free_flow_actions(old_acts);
923 ovs_flow_free(new_flow, false);
924 }
925
926 if (reply)
927 ovs_notify(&dp_flow_genl_family, reply, info);
928 return 0;
929
930err_unlock_ovs:
931 ovs_unlock();
932 kfree_skb(reply);
933err_kfree_acts:
934 kfree(acts);
935err_kfree_flow:
936 ovs_flow_free(new_flow, false);
937error:
938 return error;
939}
940
941static struct sw_flow_actions *get_flow_actions(const struct nlattr *a,
942 const struct sw_flow_key *key,
943 const struct sw_flow_mask *mask)
944{
945 struct sw_flow_actions *acts;
946 struct sw_flow_key masked_key;
947 int error;
948
949 acts = ovs_nla_alloc_flow_actions(nla_len(a));
950 if (IS_ERR(acts))
951 return acts;
952
953 ovs_flow_mask_key(&masked_key, key, mask);
954 error = ovs_nla_copy_actions(a, &masked_key, 0, &acts);
955 if (error) {
956 OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
957 kfree(acts);
958 return ERR_PTR(error);
959 }
960
961 return acts;
962}
963
964static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info)
965{
966 struct nlattr **a = info->attrs;
967 struct ovs_header *ovs_header = info->userhdr;
968 struct sw_flow_key key;
969 struct sw_flow *flow;
970 struct sw_flow_mask mask;
971 struct sk_buff *reply = NULL;
972 struct datapath *dp;
973 struct sw_flow_actions *old_acts = NULL, *acts = NULL;
974 struct sw_flow_match match;
975 int error;
976
977
978 error = -EINVAL;
979 if (!a[OVS_FLOW_ATTR_KEY])
980 goto error;
981
982 ovs_match_init(&match, &key, &mask);
983 error = ovs_nla_get_match(&match,
984 a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
985 if (error)
986 goto error;
987
988
989 if (a[OVS_FLOW_ATTR_ACTIONS]) {
990 acts = get_flow_actions(a[OVS_FLOW_ATTR_ACTIONS], &key, &mask);
991 if (IS_ERR(acts)) {
992 error = PTR_ERR(acts);
993 goto error;
994 }
995 }
996
997
998 if (acts) {
999 reply = ovs_flow_cmd_alloc_info(acts, info, false);
1000 if (IS_ERR(reply)) {
1001 error = PTR_ERR(reply);
1002 goto err_kfree_acts;
1003 }
1004 }
1005
1006 ovs_lock();
1007 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1008 if (unlikely(!dp)) {
1009 error = -ENODEV;
1010 goto err_unlock_ovs;
1011 }
1012
1013 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1014 if (unlikely(!flow)) {
1015 error = -ENOENT;
1016 goto err_unlock_ovs;
1017 }
1018
1019
1020 if (likely(acts)) {
1021 old_acts = ovsl_dereference(flow->sf_acts);
1022 rcu_assign_pointer(flow->sf_acts, acts);
1023
1024 if (unlikely(reply)) {
1025 error = ovs_flow_cmd_fill_info(flow,
1026 ovs_header->dp_ifindex,
1027 reply, info->snd_portid,
1028 info->snd_seq, 0,
1029 OVS_FLOW_CMD_NEW);
1030 BUG_ON(error < 0);
1031 }
1032 } else {
1033
1034 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex,
1035 info, OVS_FLOW_CMD_NEW, false);
1036 if (unlikely(IS_ERR(reply))) {
1037 error = PTR_ERR(reply);
1038 goto err_unlock_ovs;
1039 }
1040 }
1041
1042
1043 if (a[OVS_FLOW_ATTR_CLEAR])
1044 ovs_flow_stats_clear(flow);
1045 ovs_unlock();
1046
1047 if (reply)
1048 ovs_notify(&dp_flow_genl_family, reply, info);
1049 if (old_acts)
1050 ovs_nla_free_flow_actions(old_acts);
1051
1052 return 0;
1053
1054err_unlock_ovs:
1055 ovs_unlock();
1056 kfree_skb(reply);
1057err_kfree_acts:
1058 kfree(acts);
1059error:
1060 return error;
1061}
1062
1063static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1064{
1065 struct nlattr **a = info->attrs;
1066 struct ovs_header *ovs_header = info->userhdr;
1067 struct sw_flow_key key;
1068 struct sk_buff *reply;
1069 struct sw_flow *flow;
1070 struct datapath *dp;
1071 struct sw_flow_match match;
1072 int err;
1073
1074 if (!a[OVS_FLOW_ATTR_KEY]) {
1075 OVS_NLERR("Flow get message rejected, Key attribute missing.\n");
1076 return -EINVAL;
1077 }
1078
1079 ovs_match_init(&match, &key, NULL);
1080 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
1081 if (err)
1082 return err;
1083
1084 ovs_lock();
1085 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1086 if (!dp) {
1087 err = -ENODEV;
1088 goto unlock;
1089 }
1090
1091 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1092 if (!flow) {
1093 err = -ENOENT;
1094 goto unlock;
1095 }
1096
1097 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex, info,
1098 OVS_FLOW_CMD_NEW, true);
1099 if (IS_ERR(reply)) {
1100 err = PTR_ERR(reply);
1101 goto unlock;
1102 }
1103
1104 ovs_unlock();
1105 return genlmsg_reply(reply, info);
1106unlock:
1107 ovs_unlock();
1108 return err;
1109}
1110
1111static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1112{
1113 struct nlattr **a = info->attrs;
1114 struct ovs_header *ovs_header = info->userhdr;
1115 struct sw_flow_key key;
1116 struct sk_buff *reply;
1117 struct sw_flow *flow;
1118 struct datapath *dp;
1119 struct sw_flow_match match;
1120 int err;
1121
1122 if (likely(a[OVS_FLOW_ATTR_KEY])) {
1123 ovs_match_init(&match, &key, NULL);
1124 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
1125 if (unlikely(err))
1126 return err;
1127 }
1128
1129 ovs_lock();
1130 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1131 if (unlikely(!dp)) {
1132 err = -ENODEV;
1133 goto unlock;
1134 }
1135
1136 if (unlikely(!a[OVS_FLOW_ATTR_KEY])) {
1137 err = ovs_flow_tbl_flush(&dp->table);
1138 goto unlock;
1139 }
1140
1141 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1142 if (unlikely(!flow)) {
1143 err = -ENOENT;
1144 goto unlock;
1145 }
1146
1147 ovs_flow_tbl_remove(&dp->table, flow);
1148 ovs_unlock();
1149
1150 reply = ovs_flow_cmd_alloc_info((const struct sw_flow_actions __force *) flow->sf_acts,
1151 info, false);
1152 if (likely(reply)) {
1153 if (likely(!IS_ERR(reply))) {
1154 rcu_read_lock();
1155 err = ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex,
1156 reply, info->snd_portid,
1157 info->snd_seq, 0,
1158 OVS_FLOW_CMD_DEL);
1159 rcu_read_unlock();
1160 BUG_ON(err < 0);
1161
1162 ovs_notify(&dp_flow_genl_family, reply, info);
1163 } else {
1164 netlink_set_err(sock_net(skb->sk)->genl_sock, 0, 0, PTR_ERR(reply));
1165 }
1166 }
1167
1168 ovs_flow_free(flow, true);
1169 return 0;
1170unlock:
1171 ovs_unlock();
1172 return err;
1173}
1174
1175static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1176{
1177 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1178 struct table_instance *ti;
1179 struct datapath *dp;
1180
1181 rcu_read_lock();
1182 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1183 if (!dp) {
1184 rcu_read_unlock();
1185 return -ENODEV;
1186 }
1187
1188 ti = rcu_dereference(dp->table.ti);
1189 for (;;) {
1190 struct sw_flow *flow;
1191 u32 bucket, obj;
1192
1193 bucket = cb->args[0];
1194 obj = cb->args[1];
1195 flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj);
1196 if (!flow)
1197 break;
1198
1199 if (ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex, skb,
1200 NETLINK_CB(cb->skb).portid,
1201 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1202 OVS_FLOW_CMD_NEW) < 0)
1203 break;
1204
1205 cb->args[0] = bucket;
1206 cb->args[1] = obj;
1207 }
1208 rcu_read_unlock();
1209 return skb->len;
1210}
1211
1212static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
1213 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
1214 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
1215 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
1216};
1217
1218static const struct genl_ops dp_flow_genl_ops[] = {
1219 { .cmd = OVS_FLOW_CMD_NEW,
1220 .flags = GENL_ADMIN_PERM,
1221 .policy = flow_policy,
1222 .doit = ovs_flow_cmd_new
1223 },
1224 { .cmd = OVS_FLOW_CMD_DEL,
1225 .flags = GENL_ADMIN_PERM,
1226 .policy = flow_policy,
1227 .doit = ovs_flow_cmd_del
1228 },
1229 { .cmd = OVS_FLOW_CMD_GET,
1230 .flags = 0,
1231 .policy = flow_policy,
1232 .doit = ovs_flow_cmd_get,
1233 .dumpit = ovs_flow_cmd_dump
1234 },
1235 { .cmd = OVS_FLOW_CMD_SET,
1236 .flags = GENL_ADMIN_PERM,
1237 .policy = flow_policy,
1238 .doit = ovs_flow_cmd_set,
1239 },
1240};
1241
1242static struct genl_family dp_flow_genl_family = {
1243 .id = GENL_ID_GENERATE,
1244 .hdrsize = sizeof(struct ovs_header),
1245 .name = OVS_FLOW_FAMILY,
1246 .version = OVS_FLOW_VERSION,
1247 .maxattr = OVS_FLOW_ATTR_MAX,
1248 .netnsok = true,
1249 .parallel_ops = true,
1250 .ops = dp_flow_genl_ops,
1251 .n_ops = ARRAY_SIZE(dp_flow_genl_ops),
1252 .mcgrps = &ovs_dp_flow_multicast_group,
1253 .n_mcgrps = 1,
1254};
1255
1256static size_t ovs_dp_cmd_msg_size(void)
1257{
1258 size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
1259
1260 msgsize += nla_total_size(IFNAMSIZ);
1261 msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
1262 msgsize += nla_total_size(sizeof(struct ovs_dp_megaflow_stats));
1263 msgsize += nla_total_size(sizeof(u32));
1264
1265 return msgsize;
1266}
1267
1268
1269static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
1270 u32 portid, u32 seq, u32 flags, u8 cmd)
1271{
1272 struct ovs_header *ovs_header;
1273 struct ovs_dp_stats dp_stats;
1274 struct ovs_dp_megaflow_stats dp_megaflow_stats;
1275 int err;
1276
1277 ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
1278 flags, cmd);
1279 if (!ovs_header)
1280 goto error;
1281
1282 ovs_header->dp_ifindex = get_dpifindex(dp);
1283
1284 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
1285 if (err)
1286 goto nla_put_failure;
1287
1288 get_dp_stats(dp, &dp_stats, &dp_megaflow_stats);
1289 if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats),
1290 &dp_stats))
1291 goto nla_put_failure;
1292
1293 if (nla_put(skb, OVS_DP_ATTR_MEGAFLOW_STATS,
1294 sizeof(struct ovs_dp_megaflow_stats),
1295 &dp_megaflow_stats))
1296 goto nla_put_failure;
1297
1298 if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, dp->user_features))
1299 goto nla_put_failure;
1300
1301 return genlmsg_end(skb, ovs_header);
1302
1303nla_put_failure:
1304 genlmsg_cancel(skb, ovs_header);
1305error:
1306 return -EMSGSIZE;
1307}
1308
1309static struct sk_buff *ovs_dp_cmd_alloc_info(struct genl_info *info)
1310{
1311 return genlmsg_new_unicast(ovs_dp_cmd_msg_size(), info, GFP_KERNEL);
1312}
1313
1314
1315static struct datapath *lookup_datapath(struct net *net,
1316 struct ovs_header *ovs_header,
1317 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1318{
1319 struct datapath *dp;
1320
1321 if (!a[OVS_DP_ATTR_NAME])
1322 dp = get_dp(net, ovs_header->dp_ifindex);
1323 else {
1324 struct vport *vport;
1325
1326 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
1327 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
1328 }
1329 return dp ? dp : ERR_PTR(-ENODEV);
1330}
1331
1332static void ovs_dp_reset_user_features(struct sk_buff *skb, struct genl_info *info)
1333{
1334 struct datapath *dp;
1335
1336 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1337 if (IS_ERR(dp))
1338 return;
1339
1340 WARN(dp->user_features, "Dropping previously announced user features\n");
1341 dp->user_features = 0;
1342}
1343
1344static void ovs_dp_change(struct datapath *dp, struct nlattr **a)
1345{
1346 if (a[OVS_DP_ATTR_USER_FEATURES])
1347 dp->user_features = nla_get_u32(a[OVS_DP_ATTR_USER_FEATURES]);
1348}
1349
1350static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1351{
1352 struct nlattr **a = info->attrs;
1353 struct vport_parms parms;
1354 struct sk_buff *reply;
1355 struct datapath *dp;
1356 struct vport *vport;
1357 struct ovs_net *ovs_net;
1358 int err, i;
1359
1360 err = -EINVAL;
1361 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1362 goto err;
1363
1364 reply = ovs_dp_cmd_alloc_info(info);
1365 if (!reply)
1366 return -ENOMEM;
1367
1368 err = -ENOMEM;
1369 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1370 if (dp == NULL)
1371 goto err_free_reply;
1372
1373 ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
1374
1375
1376 err = ovs_flow_tbl_init(&dp->table);
1377 if (err)
1378 goto err_free_dp;
1379
1380 dp->stats_percpu = netdev_alloc_pcpu_stats(struct dp_stats_percpu);
1381 if (!dp->stats_percpu) {
1382 err = -ENOMEM;
1383 goto err_destroy_table;
1384 }
1385
1386 dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
1387 GFP_KERNEL);
1388 if (!dp->ports) {
1389 err = -ENOMEM;
1390 goto err_destroy_percpu;
1391 }
1392
1393 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1394 INIT_HLIST_HEAD(&dp->ports[i]);
1395
1396
1397 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1398 parms.type = OVS_VPORT_TYPE_INTERNAL;
1399 parms.options = NULL;
1400 parms.dp = dp;
1401 parms.port_no = OVSP_LOCAL;
1402 parms.upcall_portids = a[OVS_DP_ATTR_UPCALL_PID];
1403
1404 ovs_dp_change(dp, a);
1405
1406
1407 ovs_lock();
1408
1409 vport = new_vport(&parms);
1410 if (IS_ERR(vport)) {
1411 err = PTR_ERR(vport);
1412 if (err == -EBUSY)
1413 err = -EEXIST;
1414
1415 if (err == -EEXIST) {
1416
1417
1418
1419
1420 if (info->genlhdr->version < OVS_DP_VER_FEATURES)
1421 ovs_dp_reset_user_features(skb, info);
1422 }
1423
1424 goto err_destroy_ports_array;
1425 }
1426
1427 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1428 info->snd_seq, 0, OVS_DP_CMD_NEW);
1429 BUG_ON(err < 0);
1430
1431 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
1432 list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
1433
1434 ovs_unlock();
1435
1436 ovs_notify(&dp_datapath_genl_family, reply, info);
1437 return 0;
1438
1439err_destroy_ports_array:
1440 ovs_unlock();
1441 kfree(dp->ports);
1442err_destroy_percpu:
1443 free_percpu(dp->stats_percpu);
1444err_destroy_table:
1445 ovs_flow_tbl_destroy(&dp->table, false);
1446err_free_dp:
1447 release_net(ovs_dp_get_net(dp));
1448 kfree(dp);
1449err_free_reply:
1450 kfree_skb(reply);
1451err:
1452 return err;
1453}
1454
1455
1456static void __dp_destroy(struct datapath *dp)
1457{
1458 int i;
1459
1460 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1461 struct vport *vport;
1462 struct hlist_node *n;
1463
1464 hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
1465 if (vport->port_no != OVSP_LOCAL)
1466 ovs_dp_detach_port(vport);
1467 }
1468
1469 list_del_rcu(&dp->list_node);
1470
1471
1472
1473
1474 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
1475
1476
1477 ovs_flow_tbl_destroy(&dp->table, true);
1478
1479 call_rcu(&dp->rcu, destroy_dp_rcu);
1480}
1481
1482static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1483{
1484 struct sk_buff *reply;
1485 struct datapath *dp;
1486 int err;
1487
1488 reply = ovs_dp_cmd_alloc_info(info);
1489 if (!reply)
1490 return -ENOMEM;
1491
1492 ovs_lock();
1493 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1494 err = PTR_ERR(dp);
1495 if (IS_ERR(dp))
1496 goto err_unlock_free;
1497
1498 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1499 info->snd_seq, 0, OVS_DP_CMD_DEL);
1500 BUG_ON(err < 0);
1501
1502 __dp_destroy(dp);
1503 ovs_unlock();
1504
1505 ovs_notify(&dp_datapath_genl_family, reply, info);
1506
1507 return 0;
1508
1509err_unlock_free:
1510 ovs_unlock();
1511 kfree_skb(reply);
1512 return err;
1513}
1514
1515static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1516{
1517 struct sk_buff *reply;
1518 struct datapath *dp;
1519 int err;
1520
1521 reply = ovs_dp_cmd_alloc_info(info);
1522 if (!reply)
1523 return -ENOMEM;
1524
1525 ovs_lock();
1526 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1527 err = PTR_ERR(dp);
1528 if (IS_ERR(dp))
1529 goto err_unlock_free;
1530
1531 ovs_dp_change(dp, info->attrs);
1532
1533 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1534 info->snd_seq, 0, OVS_DP_CMD_NEW);
1535 BUG_ON(err < 0);
1536
1537 ovs_unlock();
1538 ovs_notify(&dp_datapath_genl_family, reply, info);
1539
1540 return 0;
1541
1542err_unlock_free:
1543 ovs_unlock();
1544 kfree_skb(reply);
1545 return err;
1546}
1547
1548static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1549{
1550 struct sk_buff *reply;
1551 struct datapath *dp;
1552 int err;
1553
1554 reply = ovs_dp_cmd_alloc_info(info);
1555 if (!reply)
1556 return -ENOMEM;
1557
1558 ovs_lock();
1559 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1560 if (IS_ERR(dp)) {
1561 err = PTR_ERR(dp);
1562 goto err_unlock_free;
1563 }
1564 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1565 info->snd_seq, 0, OVS_DP_CMD_NEW);
1566 BUG_ON(err < 0);
1567 ovs_unlock();
1568
1569 return genlmsg_reply(reply, info);
1570
1571err_unlock_free:
1572 ovs_unlock();
1573 kfree_skb(reply);
1574 return err;
1575}
1576
1577static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1578{
1579 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
1580 struct datapath *dp;
1581 int skip = cb->args[0];
1582 int i = 0;
1583
1584 ovs_lock();
1585 list_for_each_entry(dp, &ovs_net->dps, list_node) {
1586 if (i >= skip &&
1587 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
1588 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1589 OVS_DP_CMD_NEW) < 0)
1590 break;
1591 i++;
1592 }
1593 ovs_unlock();
1594
1595 cb->args[0] = i;
1596
1597 return skb->len;
1598}
1599
1600static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1601 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1602 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1603 [OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 },
1604};
1605
1606static const struct genl_ops dp_datapath_genl_ops[] = {
1607 { .cmd = OVS_DP_CMD_NEW,
1608 .flags = GENL_ADMIN_PERM,
1609 .policy = datapath_policy,
1610 .doit = ovs_dp_cmd_new
1611 },
1612 { .cmd = OVS_DP_CMD_DEL,
1613 .flags = GENL_ADMIN_PERM,
1614 .policy = datapath_policy,
1615 .doit = ovs_dp_cmd_del
1616 },
1617 { .cmd = OVS_DP_CMD_GET,
1618 .flags = 0,
1619 .policy = datapath_policy,
1620 .doit = ovs_dp_cmd_get,
1621 .dumpit = ovs_dp_cmd_dump
1622 },
1623 { .cmd = OVS_DP_CMD_SET,
1624 .flags = GENL_ADMIN_PERM,
1625 .policy = datapath_policy,
1626 .doit = ovs_dp_cmd_set,
1627 },
1628};
1629
1630static struct genl_family dp_datapath_genl_family = {
1631 .id = GENL_ID_GENERATE,
1632 .hdrsize = sizeof(struct ovs_header),
1633 .name = OVS_DATAPATH_FAMILY,
1634 .version = OVS_DATAPATH_VERSION,
1635 .maxattr = OVS_DP_ATTR_MAX,
1636 .netnsok = true,
1637 .parallel_ops = true,
1638 .ops = dp_datapath_genl_ops,
1639 .n_ops = ARRAY_SIZE(dp_datapath_genl_ops),
1640 .mcgrps = &ovs_dp_datapath_multicast_group,
1641 .n_mcgrps = 1,
1642};
1643
1644
1645static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
1646 u32 portid, u32 seq, u32 flags, u8 cmd)
1647{
1648 struct ovs_header *ovs_header;
1649 struct ovs_vport_stats vport_stats;
1650 int err;
1651
1652 ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
1653 flags, cmd);
1654 if (!ovs_header)
1655 return -EMSGSIZE;
1656
1657 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1658
1659 if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1660 nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
1661 nla_put_string(skb, OVS_VPORT_ATTR_NAME,
1662 vport->ops->get_name(vport)))
1663 goto nla_put_failure;
1664
1665 ovs_vport_get_stats(vport, &vport_stats);
1666 if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
1667 &vport_stats))
1668 goto nla_put_failure;
1669
1670 if (ovs_vport_get_upcall_portids(vport, skb))
1671 goto nla_put_failure;
1672
1673 err = ovs_vport_get_options(vport, skb);
1674 if (err == -EMSGSIZE)
1675 goto error;
1676
1677 return genlmsg_end(skb, ovs_header);
1678
1679nla_put_failure:
1680 err = -EMSGSIZE;
1681error:
1682 genlmsg_cancel(skb, ovs_header);
1683 return err;
1684}
1685
1686static struct sk_buff *ovs_vport_cmd_alloc_info(void)
1687{
1688 return nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1689}
1690
1691
1692struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
1693 u32 seq, u8 cmd)
1694{
1695 struct sk_buff *skb;
1696 int retval;
1697
1698 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1699 if (!skb)
1700 return ERR_PTR(-ENOMEM);
1701
1702 retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
1703 BUG_ON(retval < 0);
1704
1705 return skb;
1706}
1707
1708
1709static struct vport *lookup_vport(struct net *net,
1710 struct ovs_header *ovs_header,
1711 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1712{
1713 struct datapath *dp;
1714 struct vport *vport;
1715
1716 if (a[OVS_VPORT_ATTR_NAME]) {
1717 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
1718 if (!vport)
1719 return ERR_PTR(-ENODEV);
1720 if (ovs_header->dp_ifindex &&
1721 ovs_header->dp_ifindex != get_dpifindex(vport->dp))
1722 return ERR_PTR(-ENODEV);
1723 return vport;
1724 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1725 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1726
1727 if (port_no >= DP_MAX_PORTS)
1728 return ERR_PTR(-EFBIG);
1729
1730 dp = get_dp(net, ovs_header->dp_ifindex);
1731 if (!dp)
1732 return ERR_PTR(-ENODEV);
1733
1734 vport = ovs_vport_ovsl_rcu(dp, port_no);
1735 if (!vport)
1736 return ERR_PTR(-ENODEV);
1737 return vport;
1738 } else
1739 return ERR_PTR(-EINVAL);
1740}
1741
1742static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1743{
1744 struct nlattr **a = info->attrs;
1745 struct ovs_header *ovs_header = info->userhdr;
1746 struct vport_parms parms;
1747 struct sk_buff *reply;
1748 struct vport *vport;
1749 struct datapath *dp;
1750 u32 port_no;
1751 int err;
1752
1753 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
1754 !a[OVS_VPORT_ATTR_UPCALL_PID])
1755 return -EINVAL;
1756
1757 port_no = a[OVS_VPORT_ATTR_PORT_NO]
1758 ? nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]) : 0;
1759 if (port_no >= DP_MAX_PORTS)
1760 return -EFBIG;
1761
1762 reply = ovs_vport_cmd_alloc_info();
1763 if (!reply)
1764 return -ENOMEM;
1765
1766 ovs_lock();
1767 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1768 err = -ENODEV;
1769 if (!dp)
1770 goto exit_unlock_free;
1771
1772 if (port_no) {
1773 vport = ovs_vport_ovsl(dp, port_no);
1774 err = -EBUSY;
1775 if (vport)
1776 goto exit_unlock_free;
1777 } else {
1778 for (port_no = 1; ; port_no++) {
1779 if (port_no >= DP_MAX_PORTS) {
1780 err = -EFBIG;
1781 goto exit_unlock_free;
1782 }
1783 vport = ovs_vport_ovsl(dp, port_no);
1784 if (!vport)
1785 break;
1786 }
1787 }
1788
1789 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
1790 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1791 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
1792 parms.dp = dp;
1793 parms.port_no = port_no;
1794 parms.upcall_portids = a[OVS_VPORT_ATTR_UPCALL_PID];
1795
1796 vport = new_vport(&parms);
1797 err = PTR_ERR(vport);
1798 if (IS_ERR(vport))
1799 goto exit_unlock_free;
1800
1801 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1802 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1803 BUG_ON(err < 0);
1804 ovs_unlock();
1805
1806 ovs_notify(&dp_vport_genl_family, reply, info);
1807 return 0;
1808
1809exit_unlock_free:
1810 ovs_unlock();
1811 kfree_skb(reply);
1812 return err;
1813}
1814
1815static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
1816{
1817 struct nlattr **a = info->attrs;
1818 struct sk_buff *reply;
1819 struct vport *vport;
1820 int err;
1821
1822 reply = ovs_vport_cmd_alloc_info();
1823 if (!reply)
1824 return -ENOMEM;
1825
1826 ovs_lock();
1827 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
1828 err = PTR_ERR(vport);
1829 if (IS_ERR(vport))
1830 goto exit_unlock_free;
1831
1832 if (a[OVS_VPORT_ATTR_TYPE] &&
1833 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
1834 err = -EINVAL;
1835 goto exit_unlock_free;
1836 }
1837
1838 if (a[OVS_VPORT_ATTR_OPTIONS]) {
1839 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
1840 if (err)
1841 goto exit_unlock_free;
1842 }
1843
1844
1845 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
1846 struct nlattr *ids = a[OVS_VPORT_ATTR_UPCALL_PID];
1847
1848 err = ovs_vport_set_upcall_portids(vport, ids);
1849 if (err)
1850 goto exit_unlock_free;
1851 }
1852
1853 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1854 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1855 BUG_ON(err < 0);
1856
1857 ovs_unlock();
1858 ovs_notify(&dp_vport_genl_family, reply, info);
1859 return 0;
1860
1861exit_unlock_free:
1862 ovs_unlock();
1863 kfree_skb(reply);
1864 return err;
1865}
1866
1867static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
1868{
1869 struct nlattr **a = info->attrs;
1870 struct sk_buff *reply;
1871 struct vport *vport;
1872 int err;
1873
1874 reply = ovs_vport_cmd_alloc_info();
1875 if (!reply)
1876 return -ENOMEM;
1877
1878 ovs_lock();
1879 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
1880 err = PTR_ERR(vport);
1881 if (IS_ERR(vport))
1882 goto exit_unlock_free;
1883
1884 if (vport->port_no == OVSP_LOCAL) {
1885 err = -EINVAL;
1886 goto exit_unlock_free;
1887 }
1888
1889 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1890 info->snd_seq, 0, OVS_VPORT_CMD_DEL);
1891 BUG_ON(err < 0);
1892 ovs_dp_detach_port(vport);
1893 ovs_unlock();
1894
1895 ovs_notify(&dp_vport_genl_family, reply, info);
1896 return 0;
1897
1898exit_unlock_free:
1899 ovs_unlock();
1900 kfree_skb(reply);
1901 return err;
1902}
1903
1904static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
1905{
1906 struct nlattr **a = info->attrs;
1907 struct ovs_header *ovs_header = info->userhdr;
1908 struct sk_buff *reply;
1909 struct vport *vport;
1910 int err;
1911
1912 reply = ovs_vport_cmd_alloc_info();
1913 if (!reply)
1914 return -ENOMEM;
1915
1916 rcu_read_lock();
1917 vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
1918 err = PTR_ERR(vport);
1919 if (IS_ERR(vport))
1920 goto exit_unlock_free;
1921 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1922 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1923 BUG_ON(err < 0);
1924 rcu_read_unlock();
1925
1926 return genlmsg_reply(reply, info);
1927
1928exit_unlock_free:
1929 rcu_read_unlock();
1930 kfree_skb(reply);
1931 return err;
1932}
1933
1934static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1935{
1936 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1937 struct datapath *dp;
1938 int bucket = cb->args[0], skip = cb->args[1];
1939 int i, j = 0;
1940
1941 rcu_read_lock();
1942 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1943 if (!dp) {
1944 rcu_read_unlock();
1945 return -ENODEV;
1946 }
1947 for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
1948 struct vport *vport;
1949
1950 j = 0;
1951 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
1952 if (j >= skip &&
1953 ovs_vport_cmd_fill_info(vport, skb,
1954 NETLINK_CB(cb->skb).portid,
1955 cb->nlh->nlmsg_seq,
1956 NLM_F_MULTI,
1957 OVS_VPORT_CMD_NEW) < 0)
1958 goto out;
1959
1960 j++;
1961 }
1962 skip = 0;
1963 }
1964out:
1965 rcu_read_unlock();
1966
1967 cb->args[0] = i;
1968 cb->args[1] = j;
1969
1970 return skb->len;
1971}
1972
1973static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
1974 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1975 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
1976 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
1977 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
1978 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1979 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
1980};
1981
1982static const struct genl_ops dp_vport_genl_ops[] = {
1983 { .cmd = OVS_VPORT_CMD_NEW,
1984 .flags = GENL_ADMIN_PERM,
1985 .policy = vport_policy,
1986 .doit = ovs_vport_cmd_new
1987 },
1988 { .cmd = OVS_VPORT_CMD_DEL,
1989 .flags = GENL_ADMIN_PERM,
1990 .policy = vport_policy,
1991 .doit = ovs_vport_cmd_del
1992 },
1993 { .cmd = OVS_VPORT_CMD_GET,
1994 .flags = 0,
1995 .policy = vport_policy,
1996 .doit = ovs_vport_cmd_get,
1997 .dumpit = ovs_vport_cmd_dump
1998 },
1999 { .cmd = OVS_VPORT_CMD_SET,
2000 .flags = GENL_ADMIN_PERM,
2001 .policy = vport_policy,
2002 .doit = ovs_vport_cmd_set,
2003 },
2004};
2005
2006struct genl_family dp_vport_genl_family = {
2007 .id = GENL_ID_GENERATE,
2008 .hdrsize = sizeof(struct ovs_header),
2009 .name = OVS_VPORT_FAMILY,
2010 .version = OVS_VPORT_VERSION,
2011 .maxattr = OVS_VPORT_ATTR_MAX,
2012 .netnsok = true,
2013 .parallel_ops = true,
2014 .ops = dp_vport_genl_ops,
2015 .n_ops = ARRAY_SIZE(dp_vport_genl_ops),
2016 .mcgrps = &ovs_dp_vport_multicast_group,
2017 .n_mcgrps = 1,
2018};
2019
2020static struct genl_family * const dp_genl_families[] = {
2021 &dp_datapath_genl_family,
2022 &dp_vport_genl_family,
2023 &dp_flow_genl_family,
2024 &dp_packet_genl_family,
2025};
2026
2027static void dp_unregister_genl(int n_families)
2028{
2029 int i;
2030
2031 for (i = 0; i < n_families; i++)
2032 genl_unregister_family(dp_genl_families[i]);
2033}
2034
2035static int dp_register_genl(void)
2036{
2037 int err;
2038 int i;
2039
2040 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
2041
2042 err = genl_register_family(dp_genl_families[i]);
2043 if (err)
2044 goto error;
2045 }
2046
2047 return 0;
2048
2049error:
2050 dp_unregister_genl(i);
2051 return err;
2052}
2053
2054static int __net_init ovs_init_net(struct net *net)
2055{
2056 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2057
2058 INIT_LIST_HEAD(&ovs_net->dps);
2059 INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
2060 return 0;
2061}
2062
2063static void __net_exit ovs_exit_net(struct net *net)
2064{
2065 struct datapath *dp, *dp_next;
2066 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2067
2068 ovs_lock();
2069 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
2070 __dp_destroy(dp);
2071 ovs_unlock();
2072
2073 cancel_work_sync(&ovs_net->dp_notify_work);
2074}
2075
2076static struct pernet_operations ovs_net_ops = {
2077 .init = ovs_init_net,
2078 .exit = ovs_exit_net,
2079 .id = &ovs_net_id,
2080 .size = sizeof(struct ovs_net),
2081};
2082
2083static int __init dp_init(void)
2084{
2085 int err;
2086
2087 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
2088
2089 pr_info("Open vSwitch switching datapath\n");
2090
2091 err = action_fifos_init();
2092 if (err)
2093 goto error;
2094
2095 err = ovs_internal_dev_rtnl_link_register();
2096 if (err)
2097 goto error_action_fifos_exit;
2098
2099 err = ovs_flow_init();
2100 if (err)
2101 goto error_unreg_rtnl_link;
2102
2103 err = ovs_vport_init();
2104 if (err)
2105 goto error_flow_exit;
2106
2107 err = register_pernet_device(&ovs_net_ops);
2108 if (err)
2109 goto error_vport_exit;
2110
2111 err = register_netdevice_notifier(&ovs_dp_device_notifier);
2112 if (err)
2113 goto error_netns_exit;
2114
2115 err = dp_register_genl();
2116 if (err < 0)
2117 goto error_unreg_notifier;
2118
2119 return 0;
2120
2121error_unreg_notifier:
2122 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2123error_netns_exit:
2124 unregister_pernet_device(&ovs_net_ops);
2125error_vport_exit:
2126 ovs_vport_exit();
2127error_flow_exit:
2128 ovs_flow_exit();
2129error_unreg_rtnl_link:
2130 ovs_internal_dev_rtnl_link_unregister();
2131error_action_fifos_exit:
2132 action_fifos_exit();
2133error:
2134 return err;
2135}
2136
2137static void dp_cleanup(void)
2138{
2139 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2140 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2141 unregister_pernet_device(&ovs_net_ops);
2142 rcu_barrier();
2143 ovs_vport_exit();
2144 ovs_flow_exit();
2145 ovs_internal_dev_rtnl_link_unregister();
2146 action_fifos_exit();
2147}
2148
2149module_init(dp_init);
2150module_exit(dp_cleanup);
2151
2152MODULE_DESCRIPTION("Open vSwitch switching datapath");
2153MODULE_LICENSE("GPL");
2154