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