datapath: Remove RT kernel support.
[sliver-openvswitch.git] / datapath / datapath.c
1 /*
2  * Copyright (c) 2007, 2008, 2009, 2010, 2011 Nicira Networks.
3  * Distributed under the terms of the GNU GPL version 2.
4  *
5  * Significant portions of this file may be copied from parts of the Linux
6  * kernel, by Linus Torvalds and others.
7  */
8
9 /* Functions for managing the dp interface/device. */
10
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
13 #include <linux/init.h>
14 #include <linux/module.h>
15 #include <linux/if_arp.h>
16 #include <linux/if_vlan.h>
17 #include <linux/in.h>
18 #include <linux/ip.h>
19 #include <linux/jhash.h>
20 #include <linux/delay.h>
21 #include <linux/time.h>
22 #include <linux/etherdevice.h>
23 #include <linux/genetlink.h>
24 #include <linux/kernel.h>
25 #include <linux/kthread.h>
26 #include <linux/mutex.h>
27 #include <linux/percpu.h>
28 #include <linux/rcupdate.h>
29 #include <linux/tcp.h>
30 #include <linux/udp.h>
31 #include <linux/version.h>
32 #include <linux/ethtool.h>
33 #include <linux/wait.h>
34 #include <asm/system.h>
35 #include <asm/div64.h>
36 #include <asm/bug.h>
37 #include <linux/highmem.h>
38 #include <linux/netfilter_bridge.h>
39 #include <linux/netfilter_ipv4.h>
40 #include <linux/inetdevice.h>
41 #include <linux/list.h>
42 #include <linux/rculist.h>
43 #include <linux/dmi.h>
44 #include <net/inet_ecn.h>
45 #include <net/genetlink.h>
46
47 #include "openvswitch/datapath-protocol.h"
48 #include "checksum.h"
49 #include "datapath.h"
50 #include "actions.h"
51 #include "flow.h"
52 #include "vlan.h"
53 #include "tunnel.h"
54 #include "vport-internal_dev.h"
55
56 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18) || \
57     LINUX_VERSION_CODE >= KERNEL_VERSION(3,1,0)
58 #error Kernels before 2.6.18 or after 3.0 are not supported by this version of Open vSwitch.
59 #endif
60
61 int (*dp_ioctl_hook)(struct net_device *dev, struct ifreq *rq, int cmd);
62 EXPORT_SYMBOL(dp_ioctl_hook);
63
64 /**
65  * DOC: Locking:
66  *
67  * Writes to device state (add/remove datapath, port, set operations on vports,
68  * etc.) are protected by RTNL.
69  *
70  * Writes to other state (flow table modifications, set miscellaneous datapath
71  * parameters such as drop frags, etc.) are protected by genl_mutex.  The RTNL
72  * lock nests inside genl_mutex.
73  *
74  * Reads are protected by RCU.
75  *
76  * There are a few special cases (mostly stats) that have their own
77  * synchronization but they nest under all of above and don't interact with
78  * each other.
79  */
80
81 /* Global list of datapaths to enable dumping them all out.
82  * Protected by genl_mutex.
83  */
84 static LIST_HEAD(dps);
85
86 static struct vport *new_vport(const struct vport_parms *);
87 static int queue_userspace_packets(struct datapath *, u32 pid, struct sk_buff *,
88                                  const struct dp_upcall_info *);
89
90 /* Must be called with rcu_read_lock, genl_mutex, or RTNL lock. */
91 struct datapath *get_dp(int dp_ifindex)
92 {
93         struct datapath *dp = NULL;
94         struct net_device *dev;
95
96         rcu_read_lock();
97         dev = dev_get_by_index_rcu(&init_net, dp_ifindex);
98         if (dev) {
99                 struct vport *vport = internal_dev_get_vport(dev);
100                 if (vport)
101                         dp = vport->dp;
102         }
103         rcu_read_unlock();
104
105         return dp;
106 }
107 EXPORT_SYMBOL_GPL(get_dp);
108
109 /* Must be called with genl_mutex. */
110 static struct flow_table *get_table_protected(struct datapath *dp)
111 {
112         return rcu_dereference_protected(dp->table, lockdep_genl_is_held());
113 }
114
115 /* Must be called with rcu_read_lock or RTNL lock. */
116 static struct vport *get_vport_protected(struct datapath *dp, u16 port_no)
117 {
118         return rcu_dereference_rtnl(dp->ports[port_no]);
119 }
120
121 /* Must be called with rcu_read_lock or RTNL lock. */
122 const char *dp_name(const struct datapath *dp)
123 {
124         return vport_get_name(rcu_dereference_rtnl(dp->ports[OVSP_LOCAL]));
125 }
126
127 static int get_dpifindex(struct datapath *dp)
128 {
129         struct vport *local;
130         int ifindex;
131
132         rcu_read_lock();
133
134         local = get_vport_protected(dp, OVSP_LOCAL);
135         if (local)
136                 ifindex = vport_get_ifindex(local);
137         else
138                 ifindex = 0;
139
140         rcu_read_unlock();
141
142         return ifindex;
143 }
144
145 static inline size_t br_nlmsg_size(void)
146 {
147         return NLMSG_ALIGN(sizeof(struct ifinfomsg))
148                + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
149                + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
150                + nla_total_size(4) /* IFLA_MASTER */
151                + nla_total_size(4) /* IFLA_MTU */
152                + nla_total_size(1); /* IFLA_OPERSTATE */
153 }
154
155 /* Caller must hold RTNL lock. */
156 static int dp_fill_ifinfo(struct sk_buff *skb,
157                           const struct vport *port,
158                           int event, unsigned int flags)
159 {
160         struct datapath *dp = port->dp;
161         int ifindex = vport_get_ifindex(port);
162         struct ifinfomsg *hdr;
163         struct nlmsghdr *nlh;
164
165         if (ifindex < 0)
166                 return ifindex;
167
168         nlh = nlmsg_put(skb, 0, 0, event, sizeof(*hdr), flags);
169         if (nlh == NULL)
170                 return -EMSGSIZE;
171
172         hdr = nlmsg_data(nlh);
173         hdr->ifi_family = AF_BRIDGE;
174         hdr->__ifi_pad = 0;
175         hdr->ifi_type = ARPHRD_ETHER;
176         hdr->ifi_index = ifindex;
177         hdr->ifi_flags = vport_get_flags(port);
178         hdr->ifi_change = 0;
179
180         NLA_PUT_STRING(skb, IFLA_IFNAME, vport_get_name(port));
181         NLA_PUT_U32(skb, IFLA_MASTER, get_dpifindex(dp));
182         NLA_PUT_U32(skb, IFLA_MTU, vport_get_mtu(port));
183 #ifdef IFLA_OPERSTATE
184         NLA_PUT_U8(skb, IFLA_OPERSTATE,
185                    vport_is_running(port)
186                         ? vport_get_operstate(port)
187                         : IF_OPER_DOWN);
188 #endif
189
190         NLA_PUT(skb, IFLA_ADDRESS, ETH_ALEN, vport_get_addr(port));
191
192         return nlmsg_end(skb, nlh);
193
194 nla_put_failure:
195         nlmsg_cancel(skb, nlh);
196         return -EMSGSIZE;
197 }
198
199 /* Caller must hold RTNL lock. */
200 static void dp_ifinfo_notify(int event, struct vport *port)
201 {
202         struct sk_buff *skb;
203         int err = -ENOBUFS;
204
205         skb = nlmsg_new(br_nlmsg_size(), GFP_KERNEL);
206         if (skb == NULL)
207                 goto errout;
208
209         err = dp_fill_ifinfo(skb, port, event, 0);
210         if (err < 0) {
211                 /* -EMSGSIZE implies BUG in br_nlmsg_size() */
212                 WARN_ON(err == -EMSGSIZE);
213                 kfree_skb(skb);
214                 goto errout;
215         }
216         rtnl_notify(skb, &init_net, 0, RTNLGRP_LINK, NULL, GFP_KERNEL);
217         return;
218 errout:
219         if (err < 0)
220                 rtnl_set_sk_err(&init_net, RTNLGRP_LINK, err);
221 }
222
223 static void release_dp(struct kobject *kobj)
224 {
225         struct datapath *dp = container_of(kobj, struct datapath, ifobj);
226         kfree(dp);
227 }
228
229 static struct kobj_type dp_ktype = {
230         .release = release_dp
231 };
232
233 static void destroy_dp_rcu(struct rcu_head *rcu)
234 {
235         struct datapath *dp = container_of(rcu, struct datapath, rcu);
236
237         flow_tbl_destroy(dp->table);
238         free_percpu(dp->stats_percpu);
239         kobject_put(&dp->ifobj);
240 }
241
242 /* Called with RTNL lock and genl_lock. */
243 static struct vport *new_vport(const struct vport_parms *parms)
244 {
245         struct vport *vport;
246
247         vport = vport_add(parms);
248         if (!IS_ERR(vport)) {
249                 struct datapath *dp = parms->dp;
250
251                 rcu_assign_pointer(dp->ports[parms->port_no], vport);
252                 list_add(&vport->node, &dp->port_list);
253
254                 dp_ifinfo_notify(RTM_NEWLINK, vport);
255         }
256
257         return vport;
258 }
259
260 /* Called with RTNL lock. */
261 void dp_detach_port(struct vport *p)
262 {
263         ASSERT_RTNL();
264
265         if (p->port_no != OVSP_LOCAL)
266                 dp_sysfs_del_if(p);
267         dp_ifinfo_notify(RTM_DELLINK, p);
268
269         /* First drop references to device. */
270         list_del(&p->node);
271         rcu_assign_pointer(p->dp->ports[p->port_no], NULL);
272
273         /* Then destroy it. */
274         vport_del(p);
275 }
276
277 /* Must be called with rcu_read_lock. */
278 void dp_process_received_packet(struct vport *p, struct sk_buff *skb)
279 {
280         struct datapath *dp = p->dp;
281         struct sw_flow *flow;
282         struct dp_stats_percpu *stats;
283         u64 *stats_counter;
284         int error;
285
286         stats = per_cpu_ptr(dp->stats_percpu, smp_processor_id());
287         OVS_CB(skb)->vport = p;
288
289         if (!OVS_CB(skb)->flow) {
290                 struct sw_flow_key key;
291                 int key_len;
292                 bool is_frag;
293
294                 /* Extract flow from 'skb' into 'key'. */
295                 error = flow_extract(skb, p->port_no, &key, &key_len, &is_frag);
296                 if (unlikely(error)) {
297                         kfree_skb(skb);
298                         return;
299                 }
300
301                 if (is_frag && dp->drop_frags) {
302                         consume_skb(skb);
303                         stats_counter = &stats->n_frags;
304                         goto out;
305                 }
306
307                 /* Look up flow. */
308                 flow = flow_tbl_lookup(rcu_dereference(dp->table), &key, key_len);
309                 if (unlikely(!flow)) {
310                         struct dp_upcall_info upcall;
311
312                         upcall.cmd = OVS_PACKET_CMD_MISS;
313                         upcall.key = &key;
314                         dp_upcall(dp, skb, &upcall);
315                         kfree_skb(skb);
316                         stats_counter = &stats->n_missed;
317                         goto out;
318                 }
319
320                 OVS_CB(skb)->flow = flow;
321         }
322
323         stats_counter = &stats->n_hit;
324         flow_used(OVS_CB(skb)->flow, skb);
325         execute_actions(dp, skb);
326
327 out:
328         /* Update datapath statistics. */
329
330         write_seqcount_begin(&stats->seqlock);
331         (*stats_counter)++;
332         write_seqcount_end(&stats->seqlock);
333 }
334
335 static void copy_and_csum_skb(struct sk_buff *skb, void *to)
336 {
337         u16 csum_start, csum_offset;
338         __wsum csum;
339
340         get_skb_csum_pointers(skb, &csum_start, &csum_offset);
341         csum_start -= skb_headroom(skb);
342
343         skb_copy_bits(skb, 0, to, csum_start);
344
345         csum = skb_copy_and_csum_bits(skb, csum_start, to + csum_start,
346                                       skb->len - csum_start, 0);
347         *(__sum16 *)(to + csum_start + csum_offset) = csum_fold(csum);
348 }
349
350 static struct genl_family dp_packet_genl_family = {
351         .id = GENL_ID_GENERATE,
352         .hdrsize = sizeof(struct ovs_header),
353         .name = OVS_PACKET_FAMILY,
354         .version = 1,
355         .maxattr = OVS_PACKET_ATTR_MAX
356 };
357
358 int dp_upcall(struct datapath *dp, struct sk_buff *skb,
359               const struct dp_upcall_info *upcall_info)
360 {
361         struct sk_buff *segs = NULL;
362         struct dp_stats_percpu *stats;
363         u32 pid;
364         int err;
365
366         if (OVS_CB(skb)->flow)
367                 pid = OVS_CB(skb)->flow->upcall_pid;
368         else
369                 pid = OVS_CB(skb)->vport->upcall_pid;
370
371         if (pid == 0) {
372                 err = -ENOTCONN;
373                 goto err;
374         }
375
376         forward_ip_summed(skb, true);
377
378         /* Break apart GSO packets into their component pieces.  Otherwise
379          * userspace may try to stuff a 64kB packet into a 1500-byte MTU. */
380         if (skb_is_gso(skb)) {
381                 segs = skb_gso_segment(skb, NETIF_F_SG | NETIF_F_HW_CSUM);
382                 
383                 if (IS_ERR(segs)) {
384                         err = PTR_ERR(segs);
385                         goto err;
386                 }
387                 skb = segs;
388         }
389
390         err = queue_userspace_packets(dp, pid, skb, upcall_info);
391         if (segs) {
392                 struct sk_buff *next;
393                 /* Free GSO-segments */
394                 do {
395                         next = segs->next;
396                         kfree_skb(segs);
397                 } while ((segs = next) != NULL);
398         }
399
400         if (err)
401                 goto err;
402
403         return 0;
404
405 err:
406         stats = per_cpu_ptr(dp->stats_percpu, smp_processor_id());
407
408         write_seqcount_begin(&stats->seqlock);
409         stats->n_lost++;
410         write_seqcount_end(&stats->seqlock);
411
412         return err;
413 }
414
415 /* Send each packet in the 'skb' list to userspace for 'dp' as directed by
416  * 'upcall_info'.  There will be only one packet unless we broke up a GSO
417  * packet.
418  */
419 static int queue_userspace_packets(struct datapath *dp, u32 pid,
420                                    struct sk_buff *skb,
421                                    const struct dp_upcall_info *upcall_info)
422 {
423         int dp_ifindex;
424
425         dp_ifindex = get_dpifindex(dp);
426         if (!dp_ifindex)
427                 return -ENODEV;
428
429         do {
430                 struct ovs_header *upcall;
431                 struct sk_buff *user_skb; /* to be queued to userspace */
432                 struct nlattr *nla;
433                 unsigned int len;
434                 int err;
435
436                 err = vlan_deaccel_tag(skb);
437                 if (unlikely(err))
438                         return err;
439
440                 if (nla_attr_size(skb->len) > USHRT_MAX)
441                         return -EFBIG;
442
443                 len = sizeof(struct ovs_header);
444                 len += nla_total_size(skb->len);
445                 len += nla_total_size(FLOW_BUFSIZE);
446                 if (upcall_info->cmd == OVS_PACKET_CMD_ACTION)
447                         len += nla_total_size(8);
448
449                 user_skb = genlmsg_new(len, GFP_ATOMIC);
450                 if (!user_skb)
451                         return -ENOMEM;
452
453                 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
454                                          0, upcall_info->cmd);
455                 upcall->dp_ifindex = dp_ifindex;
456
457                 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
458                 flow_to_nlattrs(upcall_info->key, user_skb);
459                 nla_nest_end(user_skb, nla);
460
461                 if (upcall_info->cmd == OVS_PACKET_CMD_ACTION)
462                         nla_put_u64(user_skb, OVS_PACKET_ATTR_USERDATA,
463                                                 upcall_info->userdata);
464
465                 nla = __nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, skb->len);
466                 if (skb->ip_summed == CHECKSUM_PARTIAL)
467                         copy_and_csum_skb(skb, nla_data(nla));
468                 else
469                         skb_copy_bits(skb, 0, nla_data(nla), skb->len);
470
471                 err = genlmsg_unicast(&init_net, user_skb, pid);
472                 if (err)
473                         return err;
474
475         } while ((skb = skb->next));
476
477         return 0;
478 }
479
480 /* Called with genl_mutex. */
481 static int flush_flows(int dp_ifindex)
482 {
483         struct flow_table *old_table;
484         struct flow_table *new_table;
485         struct datapath *dp;
486
487         dp = get_dp(dp_ifindex);
488         if (!dp)
489                 return -ENODEV;
490
491         old_table = get_table_protected(dp);
492         new_table = flow_tbl_alloc(TBL_MIN_BUCKETS);
493         if (!new_table)
494                 return -ENOMEM;
495
496         rcu_assign_pointer(dp->table, new_table);
497
498         flow_tbl_deferred_destroy(old_table);
499         return 0;
500 }
501
502 static int validate_actions(const struct nlattr *attr, int depth);
503
504 static int validate_sample(const struct nlattr *attr, int depth)
505 {
506         static const struct nla_policy sample_policy[OVS_SAMPLE_ATTR_MAX + 1] =
507         {
508                 [OVS_SAMPLE_ATTR_PROBABILITY] = {.type = NLA_U32 },
509                 [OVS_SAMPLE_ATTR_ACTIONS] = {.type = NLA_UNSPEC },
510         };
511         struct nlattr *a[OVS_SAMPLE_ATTR_MAX + 1];
512         int error;
513
514         error = nla_parse_nested(a, OVS_SAMPLE_ATTR_MAX, attr, sample_policy);
515         if (error)
516                 return error;
517
518         if (!a[OVS_SAMPLE_ATTR_PROBABILITY])
519                 return -EINVAL;
520         if (!a[OVS_SAMPLE_ATTR_ACTIONS])
521                 return -EINVAL;
522
523         return validate_actions(a[OVS_SAMPLE_ATTR_ACTIONS], (depth + 1));
524 }
525
526 static int validate_actions(const struct nlattr *attr, int depth)
527 {
528         const struct nlattr *a;
529         int rem, err;
530
531         if (depth >= SAMPLE_ACTION_DEPTH)
532                 return -EOVERFLOW;
533
534         nla_for_each_nested(a, attr, rem) {
535                 static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
536                         [OVS_ACTION_ATTR_OUTPUT] = 4,
537                         [OVS_ACTION_ATTR_USERSPACE] = 8,
538                         [OVS_ACTION_ATTR_PUSH_VLAN] = 2,
539                         [OVS_ACTION_ATTR_POP_VLAN] = 0,
540                         [OVS_ACTION_ATTR_SET_DL_SRC] = ETH_ALEN,
541                         [OVS_ACTION_ATTR_SET_DL_DST] = ETH_ALEN,
542                         [OVS_ACTION_ATTR_SET_NW_SRC] = 4,
543                         [OVS_ACTION_ATTR_SET_NW_DST] = 4,
544                         [OVS_ACTION_ATTR_SET_NW_TOS] = 1,
545                         [OVS_ACTION_ATTR_SET_TP_SRC] = 2,
546                         [OVS_ACTION_ATTR_SET_TP_DST] = 2,
547                         [OVS_ACTION_ATTR_SET_TUNNEL] = 8,
548                         [OVS_ACTION_ATTR_SET_PRIORITY] = 4,
549                         [OVS_ACTION_ATTR_POP_PRIORITY] = 0,
550                 };
551                 int type = nla_type(a);
552
553                 /* Match expected attr len for given attr len except for
554                  * OVS_ACTION_ATTR_SAMPLE, as it has nested actions list which
555                  * is variable size. */
556                 if (type > OVS_ACTION_ATTR_MAX ||
557                    (nla_len(a) != action_lens[type] &&
558                           type != OVS_ACTION_ATTR_SAMPLE))
559                         return -EINVAL;
560
561                 switch (type) {
562                 case OVS_ACTION_ATTR_UNSPEC:
563                         return -EINVAL;
564
565                 case OVS_ACTION_ATTR_USERSPACE:
566                 case OVS_ACTION_ATTR_POP_VLAN:
567                 case OVS_ACTION_ATTR_SET_DL_SRC:
568                 case OVS_ACTION_ATTR_SET_DL_DST:
569                 case OVS_ACTION_ATTR_SET_NW_SRC:
570                 case OVS_ACTION_ATTR_SET_NW_DST:
571                 case OVS_ACTION_ATTR_SET_TP_SRC:
572                 case OVS_ACTION_ATTR_SET_TP_DST:
573                 case OVS_ACTION_ATTR_SET_TUNNEL:
574                 case OVS_ACTION_ATTR_SET_PRIORITY:
575                 case OVS_ACTION_ATTR_POP_PRIORITY:
576                         /* No validation needed. */
577                         break;
578
579                 case OVS_ACTION_ATTR_OUTPUT:
580                         if (nla_get_u32(a) >= DP_MAX_PORTS)
581                                 return -EINVAL;
582                         break;
583
584                 case OVS_ACTION_ATTR_PUSH_VLAN:
585                         if (nla_get_be16(a) & htons(VLAN_CFI_MASK))
586                                 return -EINVAL;
587                         break;
588
589                 case OVS_ACTION_ATTR_SET_NW_TOS:
590                         if (nla_get_u8(a) & INET_ECN_MASK)
591                                 return -EINVAL;
592                         break;
593
594                 case OVS_ACTION_ATTR_SAMPLE:
595                         err = validate_sample(a, depth);
596                         if (err)
597                                 return err;
598                         break;
599
600                 default:
601                         return -EOPNOTSUPP;
602                 }
603         }
604
605         if (rem > 0)
606                 return -EINVAL;
607
608         return 0;
609 }
610 static void clear_stats(struct sw_flow *flow)
611 {
612         flow->used = 0;
613         flow->tcp_flags = 0;
614         flow->packet_count = 0;
615         flow->byte_count = 0;
616 }
617
618 static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
619 {
620         struct ovs_header *ovs_header = info->userhdr;
621         struct nlattr **a = info->attrs;
622         struct sw_flow_actions *acts;
623         struct sk_buff *packet;
624         struct sw_flow *flow;
625         struct datapath *dp;
626         struct ethhdr *eth;
627         bool is_frag;
628         int len;
629         int err;
630         int key_len;
631
632         err = -EINVAL;
633         if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
634             !a[OVS_PACKET_ATTR_ACTIONS] ||
635             nla_len(a[OVS_PACKET_ATTR_PACKET]) < ETH_HLEN)
636                 goto err;
637
638         err = validate_actions(a[OVS_PACKET_ATTR_ACTIONS], 0);
639         if (err)
640                 goto err;
641
642         len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
643         packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
644         err = -ENOMEM;
645         if (!packet)
646                 goto err;
647         skb_reserve(packet, NET_IP_ALIGN);
648
649         memcpy(__skb_put(packet, len), nla_data(a[OVS_PACKET_ATTR_PACKET]), len);
650
651         skb_reset_mac_header(packet);
652         eth = eth_hdr(packet);
653
654         /* Normally, setting the skb 'protocol' field would be handled by a
655          * call to eth_type_trans(), but it assumes there's a sending
656          * device, which we may not have. */
657         if (ntohs(eth->h_proto) >= 1536)
658                 packet->protocol = eth->h_proto;
659         else
660                 packet->protocol = htons(ETH_P_802_2);
661
662         /* Build an sw_flow for sending this packet. */
663         flow = flow_alloc();
664         err = PTR_ERR(flow);
665         if (IS_ERR(flow))
666                 goto err_kfree_skb;
667
668         err = flow_extract(packet, -1, &flow->key, &key_len, &is_frag);
669         if (err)
670                 goto err_flow_put;
671
672         err = flow_metadata_from_nlattrs(&flow->key.eth.in_port,
673                                          &flow->key.eth.tun_id,
674                                          a[OVS_PACKET_ATTR_KEY]);
675         if (err)
676                 goto err_flow_put;
677
678         flow->hash = flow_hash(&flow->key, key_len);
679
680         if (a[OVS_PACKET_ATTR_UPCALL_PID])
681                 flow->upcall_pid = nla_get_u32(a[OVS_PACKET_ATTR_UPCALL_PID]);
682         else
683                 flow->upcall_pid = NETLINK_CB(skb).pid;
684
685         acts = flow_actions_alloc(a[OVS_PACKET_ATTR_ACTIONS]);
686         err = PTR_ERR(acts);
687         if (IS_ERR(acts))
688                 goto err_flow_put;
689         rcu_assign_pointer(flow->sf_acts, acts);
690
691         OVS_CB(packet)->flow = flow;
692
693         rcu_read_lock();
694         dp = get_dp(ovs_header->dp_ifindex);
695         err = -ENODEV;
696         if (!dp)
697                 goto err_unlock;
698
699         if (flow->key.eth.in_port < DP_MAX_PORTS)
700                 OVS_CB(packet)->vport = get_vport_protected(dp,
701                                                         flow->key.eth.in_port);
702
703         local_bh_disable();
704         err = execute_actions(dp, packet);
705         local_bh_enable();
706         rcu_read_unlock();
707
708         flow_put(flow);
709         return err;
710
711 err_unlock:
712         rcu_read_unlock();
713 err_flow_put:
714         flow_put(flow);
715 err_kfree_skb:
716         kfree_skb(packet);
717 err:
718         return err;
719 }
720
721 static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
722         [OVS_PACKET_ATTR_PACKET] = { .type = NLA_UNSPEC },
723         [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
724         [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
725         [OVS_PACKET_ATTR_UPCALL_PID] = { .type = NLA_U32 },
726 };
727
728 static struct genl_ops dp_packet_genl_ops[] = {
729         { .cmd = OVS_PACKET_CMD_EXECUTE,
730           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
731           .policy = packet_policy,
732           .doit = ovs_packet_cmd_execute
733         }
734 };
735
736 static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats)
737 {
738         int i;
739         struct flow_table *table = get_table_protected(dp);
740
741         stats->n_flows = flow_tbl_count(table);
742
743         stats->n_frags = stats->n_hit = stats->n_missed = stats->n_lost = 0;
744         for_each_possible_cpu(i) {
745                 const struct dp_stats_percpu *percpu_stats;
746                 struct dp_stats_percpu local_stats;
747                 unsigned seqcount;
748
749                 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
750
751                 do {
752                         seqcount = read_seqcount_begin(&percpu_stats->seqlock);
753                         local_stats = *percpu_stats;
754                 } while (read_seqcount_retry(&percpu_stats->seqlock, seqcount));
755
756                 stats->n_frags += local_stats.n_frags;
757                 stats->n_hit += local_stats.n_hit;
758                 stats->n_missed += local_stats.n_missed;
759                 stats->n_lost += local_stats.n_lost;
760         }
761 }
762
763 static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
764         [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
765         [OVS_FLOW_ATTR_UPCALL_PID] = { .type = NLA_U32 },
766         [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
767         [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
768 };
769
770 static struct genl_family dp_flow_genl_family = {
771         .id = GENL_ID_GENERATE,
772         .hdrsize = sizeof(struct ovs_header),
773         .name = OVS_FLOW_FAMILY,
774         .version = 1,
775         .maxattr = OVS_FLOW_ATTR_MAX
776 };
777
778 static struct genl_multicast_group dp_flow_multicast_group = {
779         .name = OVS_FLOW_MCGROUP
780 };
781
782 /* Called with genl_lock. */
783 static int ovs_flow_cmd_fill_info(struct sw_flow *flow, struct datapath *dp,
784                                   struct sk_buff *skb, u32 pid, u32 seq, u32 flags, u8 cmd)
785 {
786         const int skb_orig_len = skb->len;
787         const struct sw_flow_actions *sf_acts;
788         struct ovs_flow_stats stats;
789         struct ovs_header *ovs_header;
790         struct nlattr *nla;
791         unsigned long used;
792         u8 tcp_flags;
793         int err;
794
795         sf_acts = rcu_dereference_protected(flow->sf_acts,
796                                             lockdep_genl_is_held());
797
798         ovs_header = genlmsg_put(skb, pid, seq, &dp_flow_genl_family, flags, cmd);
799         if (!ovs_header)
800                 return -EMSGSIZE;
801
802         ovs_header->dp_ifindex = get_dpifindex(dp);
803
804         nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
805         if (!nla)
806                 goto nla_put_failure;
807         err = flow_to_nlattrs(&flow->key, skb);
808         if (err)
809                 goto error;
810         nla_nest_end(skb, nla);
811
812         NLA_PUT_U32(skb, OVS_FLOW_ATTR_UPCALL_PID, flow->upcall_pid);
813
814         spin_lock_bh(&flow->lock);
815         used = flow->used;
816         stats.n_packets = flow->packet_count;
817         stats.n_bytes = flow->byte_count;
818         tcp_flags = flow->tcp_flags;
819         spin_unlock_bh(&flow->lock);
820
821         if (used)
822                 NLA_PUT_U64(skb, OVS_FLOW_ATTR_USED, flow_used_time(used));
823
824         if (stats.n_packets)
825                 NLA_PUT(skb, OVS_FLOW_ATTR_STATS, sizeof(struct ovs_flow_stats), &stats);
826
827         if (tcp_flags)
828                 NLA_PUT_U8(skb, OVS_FLOW_ATTR_TCP_FLAGS, tcp_flags);
829
830         /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
831          * this is the first flow to be dumped into 'skb'.  This is unusual for
832          * Netlink but individual action lists can be longer than
833          * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
834          * The userspace caller can always fetch the actions separately if it
835          * really wants them.  (Most userspace callers in fact don't care.)
836          *
837          * This can only fail for dump operations because the skb is always
838          * properly sized for single flows.
839          */
840         err = nla_put(skb, OVS_FLOW_ATTR_ACTIONS, sf_acts->actions_len,
841                       sf_acts->actions);
842         if (err < 0 && skb_orig_len)
843                 goto error;
844
845         return genlmsg_end(skb, ovs_header);
846
847 nla_put_failure:
848         err = -EMSGSIZE;
849 error:
850         genlmsg_cancel(skb, ovs_header);
851         return err;
852 }
853
854 static struct sk_buff *ovs_flow_cmd_alloc_info(struct sw_flow *flow)
855 {
856         const struct sw_flow_actions *sf_acts;
857         int len;
858
859         sf_acts = rcu_dereference_protected(flow->sf_acts,
860                                             lockdep_genl_is_held());
861
862         len = nla_total_size(FLOW_BUFSIZE); /* OVS_FLOW_ATTR_KEY */
863         len += nla_total_size(sf_acts->actions_len); /* OVS_FLOW_ATTR_ACTIONS */
864         len += nla_total_size(sizeof(struct ovs_flow_stats)); /* OVS_FLOW_ATTR_STATS */
865         len += nla_total_size(1); /* OVS_FLOW_ATTR_TCP_FLAGS */
866         len += nla_total_size(8); /* OVS_FLOW_ATTR_USED */
867         return genlmsg_new(NLMSG_ALIGN(sizeof(struct ovs_header)) + len, GFP_KERNEL);
868 }
869
870 static struct sk_buff *ovs_flow_cmd_build_info(struct sw_flow *flow, struct datapath *dp,
871                                                u32 pid, u32 seq, u8 cmd)
872 {
873         struct sk_buff *skb;
874         int retval;
875
876         skb = ovs_flow_cmd_alloc_info(flow);
877         if (!skb)
878                 return ERR_PTR(-ENOMEM);
879
880         retval = ovs_flow_cmd_fill_info(flow, dp, skb, pid, seq, 0, cmd);
881         BUG_ON(retval < 0);
882         return skb;
883 }
884
885 static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info)
886 {
887         struct nlattr **a = info->attrs;
888         struct ovs_header *ovs_header = info->userhdr;
889         struct sw_flow_key key;
890         struct sw_flow *flow;
891         struct sk_buff *reply;
892         struct datapath *dp;
893         struct flow_table *table;
894         int error;
895         int key_len;
896
897         /* Extract key. */
898         error = -EINVAL;
899         if (!a[OVS_FLOW_ATTR_KEY])
900                 goto error;
901         error = flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
902         if (error)
903                 goto error;
904
905         /* Validate actions. */
906         if (a[OVS_FLOW_ATTR_ACTIONS]) {
907                 error = validate_actions(a[OVS_FLOW_ATTR_ACTIONS], 0);
908                 if (error)
909                         goto error;
910         } else if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW) {
911                 error = -EINVAL;
912                 goto error;
913         }
914
915         dp = get_dp(ovs_header->dp_ifindex);
916         error = -ENODEV;
917         if (!dp)
918                 goto error;
919
920         table = get_table_protected(dp);
921         flow = flow_tbl_lookup(table, &key, key_len);
922         if (!flow) {
923                 struct sw_flow_actions *acts;
924
925                 /* Bail out if we're not allowed to create a new flow. */
926                 error = -ENOENT;
927                 if (info->genlhdr->cmd == OVS_FLOW_CMD_SET)
928                         goto error;
929
930                 /* Expand table, if necessary, to make room. */
931                 if (flow_tbl_need_to_expand(table)) {
932                         struct flow_table *new_table;
933
934                         new_table = flow_tbl_expand(table);
935                         if (!IS_ERR(new_table)) {
936                                 rcu_assign_pointer(dp->table, new_table);
937                                 flow_tbl_deferred_destroy(table);
938                                 table = get_table_protected(dp);
939                         }
940                 }
941
942                 /* Allocate flow. */
943                 flow = flow_alloc();
944                 if (IS_ERR(flow)) {
945                         error = PTR_ERR(flow);
946                         goto error;
947                 }
948                 flow->key = key;
949                 clear_stats(flow);
950
951                 if (a[OVS_FLOW_ATTR_UPCALL_PID])
952                         flow->upcall_pid = nla_get_u32(a[OVS_FLOW_ATTR_UPCALL_PID]);
953                 else
954                         flow->upcall_pid = NETLINK_CB(skb).pid;
955
956                 /* Obtain actions. */
957                 acts = flow_actions_alloc(a[OVS_FLOW_ATTR_ACTIONS]);
958                 error = PTR_ERR(acts);
959                 if (IS_ERR(acts))
960                         goto error_free_flow;
961                 rcu_assign_pointer(flow->sf_acts, acts);
962
963                 /* Put flow in bucket. */
964                 flow->hash = flow_hash(&key, key_len);
965                 flow_tbl_insert(table, flow);
966
967                 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_pid,
968                                                 info->snd_seq, OVS_FLOW_CMD_NEW);
969         } else {
970                 /* We found a matching flow. */
971                 struct sw_flow_actions *old_acts;
972
973                 /* Bail out if we're not allowed to modify an existing flow.
974                  * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
975                  * because Generic Netlink treats the latter as a dump
976                  * request.  We also accept NLM_F_EXCL in case that bug ever
977                  * gets fixed.
978                  */
979                 error = -EEXIST;
980                 if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW &&
981                     info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL))
982                         goto error;
983
984                 /* Update actions. */
985                 old_acts = rcu_dereference_protected(flow->sf_acts,
986                                                      lockdep_genl_is_held());
987                 if (a[OVS_FLOW_ATTR_ACTIONS] &&
988                     (old_acts->actions_len != nla_len(a[OVS_FLOW_ATTR_ACTIONS]) ||
989                      memcmp(old_acts->actions, nla_data(a[OVS_FLOW_ATTR_ACTIONS]),
990                             old_acts->actions_len))) {
991                         struct sw_flow_actions *new_acts;
992
993                         new_acts = flow_actions_alloc(a[OVS_FLOW_ATTR_ACTIONS]);
994                         error = PTR_ERR(new_acts);
995                         if (IS_ERR(new_acts))
996                                 goto error;
997
998                         rcu_assign_pointer(flow->sf_acts, new_acts);
999                         flow_deferred_free_acts(old_acts);
1000                 }
1001
1002                 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_pid,
1003                                                 info->snd_seq, OVS_FLOW_CMD_NEW);
1004
1005                 if (a[OVS_FLOW_ATTR_UPCALL_PID])
1006                         flow->upcall_pid = nla_get_u32(a[OVS_FLOW_ATTR_UPCALL_PID]);
1007
1008                 /* Clear stats. */
1009                 if (a[OVS_FLOW_ATTR_CLEAR]) {
1010                         spin_lock_bh(&flow->lock);
1011                         clear_stats(flow);
1012                         spin_unlock_bh(&flow->lock);
1013                 }
1014         }
1015
1016         if (!IS_ERR(reply))
1017                 genl_notify(reply, genl_info_net(info), info->snd_pid,
1018                             dp_flow_multicast_group.id, info->nlhdr, GFP_KERNEL);
1019         else
1020                 netlink_set_err(INIT_NET_GENL_SOCK, 0,
1021                                 dp_flow_multicast_group.id, PTR_ERR(reply));
1022         return 0;
1023
1024 error_free_flow:
1025         flow_put(flow);
1026 error:
1027         return error;
1028 }
1029
1030 static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1031 {
1032         struct nlattr **a = info->attrs;
1033         struct ovs_header *ovs_header = info->userhdr;
1034         struct sw_flow_key key;
1035         struct sk_buff *reply;
1036         struct sw_flow *flow;
1037         struct datapath *dp;
1038         struct flow_table *table;
1039         int err;
1040         int key_len;
1041
1042         if (!a[OVS_FLOW_ATTR_KEY])
1043                 return -EINVAL;
1044         err = flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
1045         if (err)
1046                 return err;
1047
1048         dp = get_dp(ovs_header->dp_ifindex);
1049         if (!dp)
1050                 return -ENODEV;
1051
1052         table = get_table_protected(dp);
1053         flow = flow_tbl_lookup(table, &key, key_len);
1054         if (!flow)
1055                 return -ENOENT;
1056
1057         reply = ovs_flow_cmd_build_info(flow, dp, info->snd_pid, info->snd_seq, OVS_FLOW_CMD_NEW);
1058         if (IS_ERR(reply))
1059                 return PTR_ERR(reply);
1060
1061         return genlmsg_reply(reply, info);
1062 }
1063
1064 static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1065 {
1066         struct nlattr **a = info->attrs;
1067         struct ovs_header *ovs_header = info->userhdr;
1068         struct sw_flow_key key;
1069         struct sk_buff *reply;
1070         struct sw_flow *flow;
1071         struct datapath *dp;
1072         struct flow_table *table;
1073         int err;
1074         int key_len;
1075
1076         if (!a[OVS_FLOW_ATTR_KEY])
1077                 return flush_flows(ovs_header->dp_ifindex);
1078         err = flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
1079         if (err)
1080                 return err;
1081
1082         dp = get_dp(ovs_header->dp_ifindex);
1083         if (!dp)
1084                 return -ENODEV;
1085
1086         table = get_table_protected(dp);
1087         flow = flow_tbl_lookup(table, &key, key_len);
1088         if (!flow)
1089                 return -ENOENT;
1090
1091         reply = ovs_flow_cmd_alloc_info(flow);
1092         if (!reply)
1093                 return -ENOMEM;
1094
1095         flow_tbl_remove(table, flow);
1096
1097         err = ovs_flow_cmd_fill_info(flow, dp, reply, info->snd_pid,
1098                                      info->snd_seq, 0, OVS_FLOW_CMD_DEL);
1099         BUG_ON(err < 0);
1100
1101         flow_deferred_free(flow);
1102
1103         genl_notify(reply, genl_info_net(info), info->snd_pid,
1104                     dp_flow_multicast_group.id, info->nlhdr, GFP_KERNEL);
1105         return 0;
1106 }
1107
1108 static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1109 {
1110         struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1111         struct datapath *dp;
1112
1113         dp = get_dp(ovs_header->dp_ifindex);
1114         if (!dp)
1115                 return -ENODEV;
1116
1117         for (;;) {
1118                 struct sw_flow *flow;
1119                 u32 bucket, obj;
1120
1121                 bucket = cb->args[0];
1122                 obj = cb->args[1];
1123                 flow = flow_tbl_next(get_table_protected(dp), &bucket, &obj);
1124                 if (!flow)
1125                         break;
1126
1127                 if (ovs_flow_cmd_fill_info(flow, dp, skb, NETLINK_CB(cb->skb).pid,
1128                                            cb->nlh->nlmsg_seq, NLM_F_MULTI,
1129                                            OVS_FLOW_CMD_NEW) < 0)
1130                         break;
1131
1132                 cb->args[0] = bucket;
1133                 cb->args[1] = obj;
1134         }
1135         return skb->len;
1136 }
1137
1138 static struct genl_ops dp_flow_genl_ops[] = {
1139         { .cmd = OVS_FLOW_CMD_NEW,
1140           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1141           .policy = flow_policy,
1142           .doit = ovs_flow_cmd_new_or_set
1143         },
1144         { .cmd = OVS_FLOW_CMD_DEL,
1145           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1146           .policy = flow_policy,
1147           .doit = ovs_flow_cmd_del
1148         },
1149         { .cmd = OVS_FLOW_CMD_GET,
1150           .flags = 0,               /* OK for unprivileged users. */
1151           .policy = flow_policy,
1152           .doit = ovs_flow_cmd_get,
1153           .dumpit = ovs_flow_cmd_dump
1154         },
1155         { .cmd = OVS_FLOW_CMD_SET,
1156           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1157           .policy = flow_policy,
1158           .doit = ovs_flow_cmd_new_or_set,
1159         },
1160 };
1161
1162 static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1163 #ifdef HAVE_NLA_NUL_STRING
1164         [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1165 #endif
1166         [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1167         [OVS_DP_ATTR_IPV4_FRAGS] = { .type = NLA_U32 },
1168 };
1169
1170 static struct genl_family dp_datapath_genl_family = {
1171         .id = GENL_ID_GENERATE,
1172         .hdrsize = sizeof(struct ovs_header),
1173         .name = OVS_DATAPATH_FAMILY,
1174         .version = 1,
1175         .maxattr = OVS_DP_ATTR_MAX
1176 };
1177
1178 static struct genl_multicast_group dp_datapath_multicast_group = {
1179         .name = OVS_DATAPATH_MCGROUP
1180 };
1181
1182 static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
1183                                 u32 pid, u32 seq, u32 flags, u8 cmd)
1184 {
1185         struct ovs_header *ovs_header;
1186         struct nlattr *nla;
1187         int err;
1188
1189         ovs_header = genlmsg_put(skb, pid, seq, &dp_datapath_genl_family,
1190                                    flags, cmd);
1191         if (!ovs_header)
1192                 goto error;
1193
1194         ovs_header->dp_ifindex = get_dpifindex(dp);
1195
1196         rcu_read_lock();
1197         err = nla_put_string(skb, OVS_DP_ATTR_NAME, dp_name(dp));
1198         rcu_read_unlock();
1199         if (err)
1200                 goto nla_put_failure;
1201
1202         nla = nla_reserve(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats));
1203         if (!nla)
1204                 goto nla_put_failure;
1205         get_dp_stats(dp, nla_data(nla));
1206
1207         NLA_PUT_U32(skb, OVS_DP_ATTR_IPV4_FRAGS,
1208                     dp->drop_frags ? OVS_DP_FRAG_DROP : OVS_DP_FRAG_ZERO);
1209
1210         return genlmsg_end(skb, ovs_header);
1211
1212 nla_put_failure:
1213         genlmsg_cancel(skb, ovs_header);
1214 error:
1215         return -EMSGSIZE;
1216 }
1217
1218 static struct sk_buff *ovs_dp_cmd_build_info(struct datapath *dp, u32 pid,
1219                                              u32 seq, u8 cmd)
1220 {
1221         struct sk_buff *skb;
1222         int retval;
1223
1224         skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1225         if (!skb)
1226                 return ERR_PTR(-ENOMEM);
1227
1228         retval = ovs_dp_cmd_fill_info(dp, skb, pid, seq, 0, cmd);
1229         if (retval < 0) {
1230                 kfree_skb(skb);
1231                 return ERR_PTR(retval);
1232         }
1233         return skb;
1234 }
1235
1236 static int ovs_dp_cmd_validate(struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1237 {
1238         if (a[OVS_DP_ATTR_IPV4_FRAGS]) {
1239                 u32 frags = nla_get_u32(a[OVS_DP_ATTR_IPV4_FRAGS]);
1240
1241                 if (frags != OVS_DP_FRAG_ZERO && frags != OVS_DP_FRAG_DROP)
1242                         return -EINVAL;
1243         }
1244
1245         return CHECK_NUL_STRING(a[OVS_DP_ATTR_NAME], IFNAMSIZ - 1);
1246 }
1247
1248 /* Called with genl_mutex and optionally with RTNL lock also. */
1249 static struct datapath *lookup_datapath(struct ovs_header *ovs_header, struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1250 {
1251         struct datapath *dp;
1252
1253         if (!a[OVS_DP_ATTR_NAME])
1254                 dp = get_dp(ovs_header->dp_ifindex);
1255         else {
1256                 struct vport *vport;
1257
1258                 rcu_read_lock();
1259                 vport = vport_locate(nla_data(a[OVS_DP_ATTR_NAME]));
1260                 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
1261                 rcu_read_unlock();
1262         }
1263         return dp ? dp : ERR_PTR(-ENODEV);
1264 }
1265
1266 /* Called with genl_mutex. */
1267 static void change_datapath(struct datapath *dp, struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1268 {
1269         if (a[OVS_DP_ATTR_IPV4_FRAGS])
1270                 dp->drop_frags = nla_get_u32(a[OVS_DP_ATTR_IPV4_FRAGS]) == OVS_DP_FRAG_DROP;
1271 }
1272
1273 static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1274 {
1275         struct nlattr **a = info->attrs;
1276         struct vport_parms parms;
1277         struct sk_buff *reply;
1278         struct datapath *dp;
1279         struct vport *vport;
1280         int err;
1281
1282         err = -EINVAL;
1283         if (!a[OVS_DP_ATTR_NAME])
1284                 goto err;
1285
1286         err = ovs_dp_cmd_validate(a);
1287         if (err)
1288                 goto err;
1289
1290         rtnl_lock();
1291         err = -ENODEV;
1292         if (!try_module_get(THIS_MODULE))
1293                 goto err_unlock_rtnl;
1294
1295         err = -ENOMEM;
1296         dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1297         if (dp == NULL)
1298                 goto err_put_module;
1299         INIT_LIST_HEAD(&dp->port_list);
1300
1301         /* Initialize kobject for bridge.  This will be added as
1302          * /sys/class/net/<devname>/brif later, if sysfs is enabled. */
1303         dp->ifobj.kset = NULL;
1304         kobject_init(&dp->ifobj, &dp_ktype);
1305
1306         /* Allocate table. */
1307         err = -ENOMEM;
1308         rcu_assign_pointer(dp->table, flow_tbl_alloc(TBL_MIN_BUCKETS));
1309         if (!dp->table)
1310                 goto err_free_dp;
1311
1312         dp->drop_frags = 0;
1313         dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
1314         if (!dp->stats_percpu) {
1315                 err = -ENOMEM;
1316                 goto err_destroy_table;
1317         }
1318
1319         change_datapath(dp, a);
1320
1321         /* Set up our datapath device. */
1322         parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1323         parms.type = OVS_VPORT_TYPE_INTERNAL;
1324         parms.options = NULL;
1325         parms.dp = dp;
1326         parms.port_no = OVSP_LOCAL;
1327         if (a[OVS_DP_ATTR_UPCALL_PID])
1328                 parms.upcall_pid = nla_get_u32(a[OVS_DP_ATTR_UPCALL_PID]);
1329         else
1330                 parms.upcall_pid = NETLINK_CB(skb).pid;
1331
1332         vport = new_vport(&parms);
1333         if (IS_ERR(vport)) {
1334                 err = PTR_ERR(vport);
1335                 if (err == -EBUSY)
1336                         err = -EEXIST;
1337
1338                 goto err_destroy_percpu;
1339         }
1340
1341         reply = ovs_dp_cmd_build_info(dp, info->snd_pid, info->snd_seq, OVS_DP_CMD_NEW);
1342         err = PTR_ERR(reply);
1343         if (IS_ERR(reply))
1344                 goto err_destroy_local_port;
1345
1346         list_add_tail(&dp->list_node, &dps);
1347         dp_sysfs_add_dp(dp);
1348
1349         rtnl_unlock();
1350
1351         genl_notify(reply, genl_info_net(info), info->snd_pid,
1352                     dp_datapath_multicast_group.id, info->nlhdr, GFP_KERNEL);
1353         return 0;
1354
1355 err_destroy_local_port:
1356         dp_detach_port(get_vport_protected(dp, OVSP_LOCAL));
1357 err_destroy_percpu:
1358         free_percpu(dp->stats_percpu);
1359 err_destroy_table:
1360         flow_tbl_destroy(get_table_protected(dp));
1361 err_free_dp:
1362         kfree(dp);
1363 err_put_module:
1364         module_put(THIS_MODULE);
1365 err_unlock_rtnl:
1366         rtnl_unlock();
1367 err:
1368         return err;
1369 }
1370
1371 static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1372 {
1373         struct vport *vport, *next_vport;
1374         struct sk_buff *reply;
1375         struct datapath *dp;
1376         int err;
1377
1378         err = ovs_dp_cmd_validate(info->attrs);
1379         if (err)
1380                 goto exit;
1381
1382         rtnl_lock();
1383         dp = lookup_datapath(info->userhdr, info->attrs);
1384         err = PTR_ERR(dp);
1385         if (IS_ERR(dp))
1386                 goto exit_unlock;
1387
1388         reply = ovs_dp_cmd_build_info(dp, info->snd_pid, info->snd_seq, OVS_DP_CMD_DEL);
1389         err = PTR_ERR(reply);
1390         if (IS_ERR(reply))
1391                 goto exit_unlock;
1392
1393         list_for_each_entry_safe (vport, next_vport, &dp->port_list, node)
1394                 if (vport->port_no != OVSP_LOCAL)
1395                         dp_detach_port(vport);
1396
1397         dp_sysfs_del_dp(dp);
1398         list_del(&dp->list_node);
1399         dp_detach_port(get_vport_protected(dp, OVSP_LOCAL));
1400
1401         /* rtnl_unlock() will wait until all the references to devices that
1402          * are pending unregistration have been dropped.  We do it here to
1403          * ensure that any internal devices (which contain DP pointers) are
1404          * fully destroyed before freeing the datapath.
1405          */
1406         rtnl_unlock();
1407
1408         call_rcu(&dp->rcu, destroy_dp_rcu);
1409         module_put(THIS_MODULE);
1410
1411         genl_notify(reply, genl_info_net(info), info->snd_pid,
1412                     dp_datapath_multicast_group.id, info->nlhdr, GFP_KERNEL);
1413
1414         return 0;
1415
1416 exit_unlock:
1417         rtnl_unlock();
1418 exit:
1419         return err;
1420 }
1421
1422 static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1423 {
1424         struct sk_buff *reply;
1425         struct datapath *dp;
1426         int err;
1427
1428         err = ovs_dp_cmd_validate(info->attrs);
1429         if (err)
1430                 return err;
1431
1432         dp = lookup_datapath(info->userhdr, info->attrs);
1433         if (IS_ERR(dp))
1434                 return PTR_ERR(dp);
1435
1436         change_datapath(dp, info->attrs);
1437
1438         reply = ovs_dp_cmd_build_info(dp, info->snd_pid, info->snd_seq, OVS_DP_CMD_NEW);
1439         if (IS_ERR(reply)) {
1440                 err = PTR_ERR(reply);
1441                 netlink_set_err(INIT_NET_GENL_SOCK, 0,
1442                                 dp_datapath_multicast_group.id, err);
1443                 return 0;
1444         }
1445
1446         genl_notify(reply, genl_info_net(info), info->snd_pid,
1447                     dp_datapath_multicast_group.id, info->nlhdr, GFP_KERNEL);
1448         return 0;
1449 }
1450
1451 static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1452 {
1453         struct sk_buff *reply;
1454         struct datapath *dp;
1455         int err;
1456
1457         err = ovs_dp_cmd_validate(info->attrs);
1458         if (err)
1459                 return err;
1460
1461         dp = lookup_datapath(info->userhdr, info->attrs);
1462         if (IS_ERR(dp))
1463                 return PTR_ERR(dp);
1464
1465         reply = ovs_dp_cmd_build_info(dp, info->snd_pid, info->snd_seq, OVS_DP_CMD_NEW);
1466         if (IS_ERR(reply))
1467                 return PTR_ERR(reply);
1468
1469         return genlmsg_reply(reply, info);
1470 }
1471
1472 static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1473 {
1474         struct datapath *dp;
1475         int skip = cb->args[0];
1476         int i = 0;
1477
1478         list_for_each_entry (dp, &dps, list_node) {
1479                 if (i < skip)
1480                         continue;
1481                 if (ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).pid,
1482                                          cb->nlh->nlmsg_seq, NLM_F_MULTI,
1483                                          OVS_DP_CMD_NEW) < 0)
1484                         break;
1485                 i++;
1486         }
1487
1488         cb->args[0] = i;
1489
1490         return skb->len;
1491 }
1492
1493 static struct genl_ops dp_datapath_genl_ops[] = {
1494         { .cmd = OVS_DP_CMD_NEW,
1495           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1496           .policy = datapath_policy,
1497           .doit = ovs_dp_cmd_new
1498         },
1499         { .cmd = OVS_DP_CMD_DEL,
1500           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1501           .policy = datapath_policy,
1502           .doit = ovs_dp_cmd_del
1503         },
1504         { .cmd = OVS_DP_CMD_GET,
1505           .flags = 0,               /* OK for unprivileged users. */
1506           .policy = datapath_policy,
1507           .doit = ovs_dp_cmd_get,
1508           .dumpit = ovs_dp_cmd_dump
1509         },
1510         { .cmd = OVS_DP_CMD_SET,
1511           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1512           .policy = datapath_policy,
1513           .doit = ovs_dp_cmd_set,
1514         },
1515 };
1516
1517 static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
1518 #ifdef HAVE_NLA_NUL_STRING
1519         [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1520         [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
1521         [OVS_VPORT_ATTR_ADDRESS] = { .len = ETH_ALEN },
1522 #else
1523         [OVS_VPORT_ATTR_STATS] = { .minlen = sizeof(struct ovs_vport_stats) },
1524         [OVS_VPORT_ATTR_ADDRESS] = { .minlen = ETH_ALEN },
1525 #endif
1526         [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
1527         [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
1528         [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1529         [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
1530 };
1531
1532 static struct genl_family dp_vport_genl_family = {
1533         .id = GENL_ID_GENERATE,
1534         .hdrsize = sizeof(struct ovs_header),
1535         .name = OVS_VPORT_FAMILY,
1536         .version = 1,
1537         .maxattr = OVS_VPORT_ATTR_MAX
1538 };
1539
1540 struct genl_multicast_group dp_vport_multicast_group = {
1541         .name = OVS_VPORT_MCGROUP
1542 };
1543
1544 /* Called with RTNL lock or RCU read lock. */
1545 static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
1546                                    u32 pid, u32 seq, u32 flags, u8 cmd)
1547 {
1548         struct ovs_header *ovs_header;
1549         struct nlattr *nla;
1550         int err;
1551
1552         ovs_header = genlmsg_put(skb, pid, seq, &dp_vport_genl_family,
1553                                  flags, cmd);
1554         if (!ovs_header)
1555                 return -EMSGSIZE;
1556
1557         ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1558
1559         NLA_PUT_U32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no);
1560         NLA_PUT_U32(skb, OVS_VPORT_ATTR_TYPE, vport_get_type(vport));
1561         NLA_PUT_STRING(skb, OVS_VPORT_ATTR_NAME, vport_get_name(vport));
1562         NLA_PUT_U32(skb, OVS_VPORT_ATTR_UPCALL_PID, vport->upcall_pid);
1563
1564         nla = nla_reserve(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats));
1565         if (!nla)
1566                 goto nla_put_failure;
1567
1568         vport_get_stats(vport, nla_data(nla));
1569
1570         NLA_PUT(skb, OVS_VPORT_ATTR_ADDRESS, ETH_ALEN, vport_get_addr(vport));
1571
1572         err = vport_get_options(vport, skb);
1573         if (err == -EMSGSIZE)
1574                 goto error;
1575
1576         return genlmsg_end(skb, ovs_header);
1577
1578 nla_put_failure:
1579         err = -EMSGSIZE;
1580 error:
1581         genlmsg_cancel(skb, ovs_header);
1582         return err;
1583 }
1584
1585 /* Called with RTNL lock or RCU read lock. */
1586 struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 pid,
1587                                          u32 seq, u8 cmd)
1588 {
1589         struct sk_buff *skb;
1590         int retval;
1591
1592         skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1593         if (!skb)
1594                 return ERR_PTR(-ENOMEM);
1595
1596         retval = ovs_vport_cmd_fill_info(vport, skb, pid, seq, 0, cmd);
1597         if (retval < 0) {
1598                 kfree_skb(skb);
1599                 return ERR_PTR(retval);
1600         }
1601         return skb;
1602 }
1603
1604 static int ovs_vport_cmd_validate(struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1605 {
1606         return CHECK_NUL_STRING(a[OVS_VPORT_ATTR_NAME], IFNAMSIZ - 1);
1607 }
1608
1609 /* Called with RTNL lock or RCU read lock. */
1610 static struct vport *lookup_vport(struct ovs_header *ovs_header,
1611                                   struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1612 {
1613         struct datapath *dp;
1614         struct vport *vport;
1615
1616         if (a[OVS_VPORT_ATTR_NAME]) {
1617                 vport = vport_locate(nla_data(a[OVS_VPORT_ATTR_NAME]));
1618                 if (!vport)
1619                         return ERR_PTR(-ENODEV);
1620                 return vport;
1621         } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1622                 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1623
1624                 if (port_no >= DP_MAX_PORTS)
1625                         return ERR_PTR(-EFBIG);
1626
1627                 dp = get_dp(ovs_header->dp_ifindex);
1628                 if (!dp)
1629                         return ERR_PTR(-ENODEV);
1630
1631                 vport = get_vport_protected(dp, port_no);
1632                 if (!vport)
1633                         return ERR_PTR(-ENOENT);
1634                 return vport;
1635         } else
1636                 return ERR_PTR(-EINVAL);
1637 }
1638
1639 /* Called with RTNL lock. */
1640 static int change_vport(struct vport *vport, struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1641 {
1642         int err = 0;
1643
1644         if (a[OVS_VPORT_ATTR_STATS])
1645                 vport_set_stats(vport, nla_data(a[OVS_VPORT_ATTR_STATS]));
1646
1647         if (a[OVS_VPORT_ATTR_ADDRESS])
1648                 err = vport_set_addr(vport, nla_data(a[OVS_VPORT_ATTR_ADDRESS]));
1649
1650         return err;
1651 }
1652
1653 static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1654 {
1655         struct nlattr **a = info->attrs;
1656         struct ovs_header *ovs_header = info->userhdr;
1657         struct vport_parms parms;
1658         struct sk_buff *reply;
1659         struct vport *vport;
1660         struct datapath *dp;
1661         u32 port_no;
1662         int err;
1663
1664         err = -EINVAL;
1665         if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE])
1666                 goto exit;
1667
1668         err = ovs_vport_cmd_validate(a);
1669         if (err)
1670                 goto exit;
1671
1672         rtnl_lock();
1673         dp = get_dp(ovs_header->dp_ifindex);
1674         err = -ENODEV;
1675         if (!dp)
1676                 goto exit_unlock;
1677
1678         if (a[OVS_VPORT_ATTR_PORT_NO]) {
1679                 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1680
1681                 err = -EFBIG;
1682                 if (port_no >= DP_MAX_PORTS)
1683                         goto exit_unlock;
1684
1685                 vport = get_vport_protected(dp, port_no);
1686                 err = -EBUSY;
1687                 if (vport)
1688                         goto exit_unlock;
1689         } else {
1690                 for (port_no = 1; ; port_no++) {
1691                         if (port_no >= DP_MAX_PORTS) {
1692                                 err = -EFBIG;
1693                                 goto exit_unlock;
1694                         }
1695                         vport = get_vport_protected(dp, port_no);
1696                         if (!vport)
1697                                 break;
1698                 }
1699         }
1700
1701         parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
1702         parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1703         parms.options = a[OVS_VPORT_ATTR_OPTIONS];
1704         parms.dp = dp;
1705         parms.port_no = port_no;
1706         if (a[OVS_VPORT_ATTR_UPCALL_PID])
1707                 parms.upcall_pid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
1708         else
1709                 parms.upcall_pid = NETLINK_CB(skb).pid;
1710
1711         vport = new_vport(&parms);
1712         err = PTR_ERR(vport);
1713         if (IS_ERR(vport))
1714                 goto exit_unlock;
1715
1716         dp_sysfs_add_if(vport);
1717
1718         err = change_vport(vport, a);
1719         if (!err) {
1720                 reply = ovs_vport_cmd_build_info(vport, info->snd_pid,
1721                                                  info->snd_seq, OVS_VPORT_CMD_NEW);
1722                 if (IS_ERR(reply))
1723                         err = PTR_ERR(reply);
1724         }
1725         if (err) {
1726                 dp_detach_port(vport);
1727                 goto exit_unlock;
1728         }
1729         genl_notify(reply, genl_info_net(info), info->snd_pid,
1730                     dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1731
1732
1733 exit_unlock:
1734         rtnl_unlock();
1735 exit:
1736         return err;
1737 }
1738
1739 static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
1740 {
1741         struct nlattr **a = info->attrs;
1742         struct sk_buff *reply;
1743         struct vport *vport;
1744         int err;
1745
1746         err = ovs_vport_cmd_validate(a);
1747         if (err)
1748                 goto exit;
1749
1750         rtnl_lock();
1751         vport = lookup_vport(info->userhdr, a);
1752         err = PTR_ERR(vport);
1753         if (IS_ERR(vport))
1754                 goto exit_unlock;
1755
1756         err = 0;
1757         if (a[OVS_VPORT_ATTR_OPTIONS])
1758                 err = vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
1759         if (!err)
1760                 err = change_vport(vport, a);
1761         if (!err && a[OVS_VPORT_ATTR_UPCALL_PID])
1762                 vport->upcall_pid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
1763
1764         reply = ovs_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
1765                                          OVS_VPORT_CMD_NEW);
1766         if (IS_ERR(reply)) {
1767                 err = PTR_ERR(reply);
1768                 netlink_set_err(INIT_NET_GENL_SOCK, 0,
1769                                 dp_vport_multicast_group.id, err);
1770                 return 0;
1771         }
1772
1773         genl_notify(reply, genl_info_net(info), info->snd_pid,
1774                     dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1775
1776 exit_unlock:
1777         rtnl_unlock();
1778 exit:
1779         return err;
1780 }
1781
1782 static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
1783 {
1784         struct nlattr **a = info->attrs;
1785         struct sk_buff *reply;
1786         struct vport *vport;
1787         int err;
1788
1789         err = ovs_vport_cmd_validate(a);
1790         if (err)
1791                 goto exit;
1792
1793         rtnl_lock();
1794         vport = lookup_vport(info->userhdr, a);
1795         err = PTR_ERR(vport);
1796         if (IS_ERR(vport))
1797                 goto exit_unlock;
1798
1799         if (vport->port_no == OVSP_LOCAL) {
1800                 err = -EINVAL;
1801                 goto exit_unlock;
1802         }
1803
1804         reply = ovs_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
1805                                          OVS_VPORT_CMD_DEL);
1806         err = PTR_ERR(reply);
1807         if (IS_ERR(reply))
1808                 goto exit_unlock;
1809
1810         dp_detach_port(vport);
1811
1812         genl_notify(reply, genl_info_net(info), info->snd_pid,
1813                     dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1814
1815 exit_unlock:
1816         rtnl_unlock();
1817 exit:
1818         return err;
1819 }
1820
1821 static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
1822 {
1823         struct nlattr **a = info->attrs;
1824         struct ovs_header *ovs_header = info->userhdr;
1825         struct sk_buff *reply;
1826         struct vport *vport;
1827         int err;
1828
1829         err = ovs_vport_cmd_validate(a);
1830         if (err)
1831                 goto exit;
1832
1833         rcu_read_lock();
1834         vport = lookup_vport(ovs_header, a);
1835         err = PTR_ERR(vport);
1836         if (IS_ERR(vport))
1837                 goto exit_unlock;
1838
1839         reply = ovs_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
1840                                          OVS_VPORT_CMD_NEW);
1841         err = PTR_ERR(reply);
1842         if (IS_ERR(reply))
1843                 goto exit_unlock;
1844
1845         rcu_read_unlock();
1846
1847         return genlmsg_reply(reply, info);
1848
1849 exit_unlock:
1850         rcu_read_unlock();
1851 exit:
1852         return err;
1853 }
1854
1855 static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1856 {
1857         struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1858         struct datapath *dp;
1859         u32 port_no;
1860         int retval;
1861
1862         dp = get_dp(ovs_header->dp_ifindex);
1863         if (!dp)
1864                 return -ENODEV;
1865
1866         rcu_read_lock();
1867         for (port_no = cb->args[0]; port_no < DP_MAX_PORTS; port_no++) {
1868                 struct vport *vport;
1869
1870                 vport = get_vport_protected(dp, port_no);
1871                 if (!vport)
1872                         continue;
1873
1874                 if (ovs_vport_cmd_fill_info(vport, skb, NETLINK_CB(cb->skb).pid,
1875                                             cb->nlh->nlmsg_seq, NLM_F_MULTI,
1876                                             OVS_VPORT_CMD_NEW) < 0)
1877                         break;
1878         }
1879         rcu_read_unlock();
1880
1881         cb->args[0] = port_no;
1882         retval = skb->len;
1883
1884         return retval;
1885 }
1886
1887 static struct genl_ops dp_vport_genl_ops[] = {
1888         { .cmd = OVS_VPORT_CMD_NEW,
1889           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1890           .policy = vport_policy,
1891           .doit = ovs_vport_cmd_new
1892         },
1893         { .cmd = OVS_VPORT_CMD_DEL,
1894           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1895           .policy = vport_policy,
1896           .doit = ovs_vport_cmd_del
1897         },
1898         { .cmd = OVS_VPORT_CMD_GET,
1899           .flags = 0,               /* OK for unprivileged users. */
1900           .policy = vport_policy,
1901           .doit = ovs_vport_cmd_get,
1902           .dumpit = ovs_vport_cmd_dump
1903         },
1904         { .cmd = OVS_VPORT_CMD_SET,
1905           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1906           .policy = vport_policy,
1907           .doit = ovs_vport_cmd_set,
1908         },
1909 };
1910
1911 struct genl_family_and_ops {
1912         struct genl_family *family;
1913         struct genl_ops *ops;
1914         int n_ops;
1915         struct genl_multicast_group *group;
1916 };
1917
1918 static const struct genl_family_and_ops dp_genl_families[] = {
1919         { &dp_datapath_genl_family,
1920           dp_datapath_genl_ops, ARRAY_SIZE(dp_datapath_genl_ops),
1921           &dp_datapath_multicast_group },
1922         { &dp_vport_genl_family,
1923           dp_vport_genl_ops, ARRAY_SIZE(dp_vport_genl_ops),
1924           &dp_vport_multicast_group },
1925         { &dp_flow_genl_family,
1926           dp_flow_genl_ops, ARRAY_SIZE(dp_flow_genl_ops),
1927           &dp_flow_multicast_group },
1928         { &dp_packet_genl_family,
1929           dp_packet_genl_ops, ARRAY_SIZE(dp_packet_genl_ops),
1930           NULL },
1931 };
1932
1933 static void dp_unregister_genl(int n_families)
1934 {
1935         int i;
1936
1937         for (i = 0; i < n_families; i++)
1938                 genl_unregister_family(dp_genl_families[i].family);
1939 }
1940
1941 static int dp_register_genl(void)
1942 {
1943         int n_registered;
1944         int err;
1945         int i;
1946
1947         n_registered = 0;
1948         for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
1949                 const struct genl_family_and_ops *f = &dp_genl_families[i];
1950
1951                 err = genl_register_family_with_ops(f->family, f->ops,
1952                                                     f->n_ops);
1953                 if (err)
1954                         goto error;
1955                 n_registered++;
1956
1957                 if (f->group) {
1958                         err = genl_register_mc_group(f->family, f->group);
1959                         if (err)
1960                                 goto error;
1961                 }
1962         }
1963
1964         return 0;
1965
1966 error:
1967         dp_unregister_genl(n_registered);
1968         return err;
1969 }
1970
1971 static int __init dp_init(void)
1972 {
1973         struct sk_buff *dummy_skb;
1974         int err;
1975
1976         BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > sizeof(dummy_skb->cb));
1977
1978         printk("Open vSwitch %s, built "__DATE__" "__TIME__"\n", VERSION BUILDNR);
1979
1980         err = tnl_init();
1981         if (err)
1982                 goto error;
1983
1984         err = flow_init();
1985         if (err)
1986                 goto error_tnl_exit;
1987
1988         err = vport_init();
1989         if (err)
1990                 goto error_flow_exit;
1991
1992         err = register_netdevice_notifier(&dp_device_notifier);
1993         if (err)
1994                 goto error_vport_exit;
1995
1996         err = dp_register_genl();
1997         if (err < 0)
1998                 goto error_unreg_notifier;
1999
2000         return 0;
2001
2002 error_unreg_notifier:
2003         unregister_netdevice_notifier(&dp_device_notifier);
2004 error_vport_exit:
2005         vport_exit();
2006 error_flow_exit:
2007         flow_exit();
2008 error_tnl_exit:
2009         tnl_exit();
2010 error:
2011         return err;
2012 }
2013
2014 static void dp_cleanup(void)
2015 {
2016         rcu_barrier();
2017         dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2018         unregister_netdevice_notifier(&dp_device_notifier);
2019         vport_exit();
2020         flow_exit();
2021         tnl_exit();
2022 }
2023
2024 module_init(dp_init);
2025 module_exit(dp_cleanup);
2026
2027 MODULE_DESCRIPTION("Open vSwitch switching datapath");
2028 MODULE_LICENSE("GPL");