openvswitch: Simplify mega-flow APIs.
[deliverable/linux.git] / net / openvswitch / datapath.c
CommitLineData
ccb1352e 1/*
03f0d916 2 * Copyright (c) 2007-2013 Nicira, Inc.
ccb1352e
JG
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of version 2 of the GNU General Public
6 * License as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 * 02110-1301, USA
17 */
18
19#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20
21#include <linux/init.h>
22#include <linux/module.h>
23#include <linux/if_arp.h>
24#include <linux/if_vlan.h>
25#include <linux/in.h>
26#include <linux/ip.h>
27#include <linux/jhash.h>
28#include <linux/delay.h>
29#include <linux/time.h>
30#include <linux/etherdevice.h>
31#include <linux/genetlink.h>
32#include <linux/kernel.h>
33#include <linux/kthread.h>
34#include <linux/mutex.h>
35#include <linux/percpu.h>
36#include <linux/rcupdate.h>
37#include <linux/tcp.h>
38#include <linux/udp.h>
ccb1352e
JG
39#include <linux/ethtool.h>
40#include <linux/wait.h>
ccb1352e
JG
41#include <asm/div64.h>
42#include <linux/highmem.h>
43#include <linux/netfilter_bridge.h>
44#include <linux/netfilter_ipv4.h>
45#include <linux/inetdevice.h>
46#include <linux/list.h>
8e4e1713 47#include <linux/lockdep.h>
ccb1352e
JG
48#include <linux/openvswitch.h>
49#include <linux/rculist.h>
50#include <linux/dmi.h>
51#include <linux/workqueue.h>
52#include <net/genetlink.h>
46df7b81
PS
53#include <net/net_namespace.h>
54#include <net/netns/generic.h>
ccb1352e
JG
55
56#include "datapath.h"
57#include "flow.h"
e6445719 58#include "flow_netlink.h"
ccb1352e 59#include "vport-internal_dev.h"
cff63a52 60#include "vport-netdev.h"
ccb1352e 61
8e4e1713
PS
62int ovs_net_id __read_mostly;
63
ed661185
TG
64static void ovs_notify(struct sk_buff *skb, struct genl_info *info,
65 struct genl_multicast_group *grp)
66{
67 genl_notify(skb, genl_info_net(info), info->snd_portid,
68 grp->id, info->nlhdr, GFP_KERNEL);
69}
70
ccb1352e
JG
71/**
72 * DOC: Locking:
73 *
8e4e1713
PS
74 * All writes e.g. Writes to device state (add/remove datapath, port, set
75 * operations on vports, etc.), Writes to other state (flow table
76 * modifications, set miscellaneous datapath parameters, etc.) are protected
77 * by ovs_lock.
ccb1352e
JG
78 *
79 * Reads are protected by RCU.
80 *
81 * There are a few special cases (mostly stats) that have their own
82 * synchronization but they nest under all of above and don't interact with
83 * each other.
8e4e1713
PS
84 *
85 * The RTNL lock nests inside ovs_mutex.
ccb1352e
JG
86 */
87
8e4e1713
PS
88static DEFINE_MUTEX(ovs_mutex);
89
90void ovs_lock(void)
91{
92 mutex_lock(&ovs_mutex);
93}
94
95void ovs_unlock(void)
96{
97 mutex_unlock(&ovs_mutex);
98}
99
100#ifdef CONFIG_LOCKDEP
101int lockdep_ovsl_is_held(void)
102{
103 if (debug_locks)
104 return lockdep_is_held(&ovs_mutex);
105 else
106 return 1;
107}
108#endif
109
ccb1352e 110static struct vport *new_vport(const struct vport_parms *);
46df7b81 111static int queue_gso_packets(struct net *, int dp_ifindex, struct sk_buff *,
ccb1352e 112 const struct dp_upcall_info *);
46df7b81
PS
113static int queue_userspace_packet(struct net *, int dp_ifindex,
114 struct sk_buff *,
ccb1352e
JG
115 const struct dp_upcall_info *);
116
8e4e1713 117/* Must be called with rcu_read_lock or ovs_mutex. */
46df7b81 118static struct datapath *get_dp(struct net *net, int dp_ifindex)
ccb1352e
JG
119{
120 struct datapath *dp = NULL;
121 struct net_device *dev;
122
123 rcu_read_lock();
46df7b81 124 dev = dev_get_by_index_rcu(net, dp_ifindex);
ccb1352e
JG
125 if (dev) {
126 struct vport *vport = ovs_internal_dev_get_vport(dev);
127 if (vport)
128 dp = vport->dp;
129 }
130 rcu_read_unlock();
131
132 return dp;
133}
134
8e4e1713 135/* Must be called with rcu_read_lock or ovs_mutex. */
ccb1352e
JG
136const char *ovs_dp_name(const struct datapath *dp)
137{
8e4e1713 138 struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL);
ccb1352e
JG
139 return vport->ops->get_name(vport);
140}
141
142static int get_dpifindex(struct datapath *dp)
143{
144 struct vport *local;
145 int ifindex;
146
147 rcu_read_lock();
148
15eac2a7 149 local = ovs_vport_rcu(dp, OVSP_LOCAL);
ccb1352e 150 if (local)
cff63a52 151 ifindex = netdev_vport_priv(local)->dev->ifindex;
ccb1352e
JG
152 else
153 ifindex = 0;
154
155 rcu_read_unlock();
156
157 return ifindex;
158}
159
160static void destroy_dp_rcu(struct rcu_head *rcu)
161{
162 struct datapath *dp = container_of(rcu, struct datapath, rcu);
163
618ed0c8 164 ovs_flow_tbl_destroy(&dp->table);
ccb1352e 165 free_percpu(dp->stats_percpu);
46df7b81 166 release_net(ovs_dp_get_net(dp));
15eac2a7 167 kfree(dp->ports);
ccb1352e
JG
168 kfree(dp);
169}
170
15eac2a7
PS
171static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
172 u16 port_no)
173{
174 return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
175}
176
177struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
178{
179 struct vport *vport;
15eac2a7
PS
180 struct hlist_head *head;
181
182 head = vport_hash_bucket(dp, port_no);
b67bfe0d 183 hlist_for_each_entry_rcu(vport, head, dp_hash_node) {
15eac2a7
PS
184 if (vport->port_no == port_no)
185 return vport;
186 }
187 return NULL;
188}
189
8e4e1713 190/* Called with ovs_mutex. */
ccb1352e
JG
191static struct vport *new_vport(const struct vport_parms *parms)
192{
193 struct vport *vport;
194
195 vport = ovs_vport_add(parms);
196 if (!IS_ERR(vport)) {
197 struct datapath *dp = parms->dp;
15eac2a7 198 struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
ccb1352e 199
15eac2a7 200 hlist_add_head_rcu(&vport->dp_hash_node, head);
ccb1352e 201 }
ccb1352e
JG
202 return vport;
203}
204
ccb1352e
JG
205void ovs_dp_detach_port(struct vport *p)
206{
8e4e1713 207 ASSERT_OVSL();
ccb1352e
JG
208
209 /* First drop references to device. */
15eac2a7 210 hlist_del_rcu(&p->dp_hash_node);
ccb1352e
JG
211
212 /* Then destroy it. */
213 ovs_vport_del(p);
214}
215
216/* Must be called with rcu_read_lock. */
217void ovs_dp_process_received_packet(struct vport *p, struct sk_buff *skb)
218{
219 struct datapath *dp = p->dp;
220 struct sw_flow *flow;
221 struct dp_stats_percpu *stats;
222 struct sw_flow_key key;
223 u64 *stats_counter;
224 int error;
ccb1352e 225
404f2f10 226 stats = this_cpu_ptr(dp->stats_percpu);
ccb1352e
JG
227
228 /* Extract flow from 'skb' into 'key'. */
03f0d916 229 error = ovs_flow_extract(skb, p->port_no, &key);
ccb1352e
JG
230 if (unlikely(error)) {
231 kfree_skb(skb);
232 return;
233 }
234
235 /* Look up flow. */
b637e498 236 flow = ovs_flow_tbl_lookup(&dp->table, &key);
ccb1352e
JG
237 if (unlikely(!flow)) {
238 struct dp_upcall_info upcall;
239
240 upcall.cmd = OVS_PACKET_CMD_MISS;
241 upcall.key = &key;
242 upcall.userdata = NULL;
15e47304 243 upcall.portid = p->upcall_portid;
ccb1352e
JG
244 ovs_dp_upcall(dp, skb, &upcall);
245 consume_skb(skb);
246 stats_counter = &stats->n_missed;
247 goto out;
248 }
249
250 OVS_CB(skb)->flow = flow;
03f0d916 251 OVS_CB(skb)->pkt_key = &key;
ccb1352e
JG
252
253 stats_counter = &stats->n_hit;
254 ovs_flow_used(OVS_CB(skb)->flow, skb);
255 ovs_execute_actions(dp, skb);
256
257out:
258 /* Update datapath statistics. */
259 u64_stats_update_begin(&stats->sync);
260 (*stats_counter)++;
261 u64_stats_update_end(&stats->sync);
262}
263
264static struct genl_family dp_packet_genl_family = {
265 .id = GENL_ID_GENERATE,
266 .hdrsize = sizeof(struct ovs_header),
267 .name = OVS_PACKET_FAMILY,
268 .version = OVS_PACKET_VERSION,
46df7b81 269 .maxattr = OVS_PACKET_ATTR_MAX,
3a4e0d6a
PS
270 .netnsok = true,
271 .parallel_ops = true,
ccb1352e
JG
272};
273
274int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
46df7b81 275 const struct dp_upcall_info *upcall_info)
ccb1352e
JG
276{
277 struct dp_stats_percpu *stats;
278 int dp_ifindex;
279 int err;
280
15e47304 281 if (upcall_info->portid == 0) {
ccb1352e
JG
282 err = -ENOTCONN;
283 goto err;
284 }
285
286 dp_ifindex = get_dpifindex(dp);
287 if (!dp_ifindex) {
288 err = -ENODEV;
289 goto err;
290 }
291
292 if (!skb_is_gso(skb))
46df7b81 293 err = queue_userspace_packet(ovs_dp_get_net(dp), dp_ifindex, skb, upcall_info);
ccb1352e 294 else
46df7b81 295 err = queue_gso_packets(ovs_dp_get_net(dp), dp_ifindex, skb, upcall_info);
ccb1352e
JG
296 if (err)
297 goto err;
298
299 return 0;
300
301err:
404f2f10 302 stats = this_cpu_ptr(dp->stats_percpu);
ccb1352e
JG
303
304 u64_stats_update_begin(&stats->sync);
305 stats->n_lost++;
306 u64_stats_update_end(&stats->sync);
307
308 return err;
309}
310
46df7b81
PS
311static int queue_gso_packets(struct net *net, int dp_ifindex,
312 struct sk_buff *skb,
ccb1352e
JG
313 const struct dp_upcall_info *upcall_info)
314{
a1b5d0dd 315 unsigned short gso_type = skb_shinfo(skb)->gso_type;
ccb1352e
JG
316 struct dp_upcall_info later_info;
317 struct sw_flow_key later_key;
318 struct sk_buff *segs, *nskb;
319 int err;
320
12b0004d 321 segs = __skb_gso_segment(skb, NETIF_F_SG | NETIF_F_HW_CSUM, false);
92e5dfc3
PS
322 if (IS_ERR(segs))
323 return PTR_ERR(segs);
ccb1352e
JG
324
325 /* Queue all of the segments. */
326 skb = segs;
327 do {
46df7b81 328 err = queue_userspace_packet(net, dp_ifindex, skb, upcall_info);
ccb1352e
JG
329 if (err)
330 break;
331
a1b5d0dd 332 if (skb == segs && gso_type & SKB_GSO_UDP) {
ccb1352e
JG
333 /* The initial flow key extracted by ovs_flow_extract()
334 * in this case is for a first fragment, so we need to
335 * properly mark later fragments.
336 */
337 later_key = *upcall_info->key;
338 later_key.ip.frag = OVS_FRAG_TYPE_LATER;
339
340 later_info = *upcall_info;
341 later_info.key = &later_key;
342 upcall_info = &later_info;
343 }
344 } while ((skb = skb->next));
345
346 /* Free all of the segments. */
347 skb = segs;
348 do {
349 nskb = skb->next;
350 if (err)
351 kfree_skb(skb);
352 else
353 consume_skb(skb);
354 } while ((skb = nskb));
355 return err;
356}
357
c3ff8cfe
TG
358static size_t key_attr_size(void)
359{
360 return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */
7d5437c7
PS
361 + nla_total_size(0) /* OVS_KEY_ATTR_TUNNEL */
362 + nla_total_size(8) /* OVS_TUNNEL_KEY_ATTR_ID */
363 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_SRC */
364 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_DST */
365 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TOS */
366 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TTL */
367 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT */
368 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_CSUM */
c3ff8cfe
TG
369 + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */
370 + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
371 + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
372 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
373 + nla_total_size(4) /* OVS_KEY_ATTR_8021Q */
374 + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
375 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
376 + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
377 + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
378 + nla_total_size(28); /* OVS_KEY_ATTR_ND */
379}
380
381static size_t upcall_msg_size(const struct sk_buff *skb,
382 const struct nlattr *userdata)
383{
384 size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
385 + nla_total_size(skb->len) /* OVS_PACKET_ATTR_PACKET */
386 + nla_total_size(key_attr_size()); /* OVS_PACKET_ATTR_KEY */
387
388 /* OVS_PACKET_ATTR_USERDATA */
389 if (userdata)
390 size += NLA_ALIGN(userdata->nla_len);
391
392 return size;
393}
394
46df7b81
PS
395static int queue_userspace_packet(struct net *net, int dp_ifindex,
396 struct sk_buff *skb,
ccb1352e
JG
397 const struct dp_upcall_info *upcall_info)
398{
399 struct ovs_header *upcall;
400 struct sk_buff *nskb = NULL;
401 struct sk_buff *user_skb; /* to be queued to userspace */
402 struct nlattr *nla;
ccb1352e
JG
403 int err;
404
405 if (vlan_tx_tag_present(skb)) {
406 nskb = skb_clone(skb, GFP_ATOMIC);
407 if (!nskb)
408 return -ENOMEM;
409
86a9bad3 410 nskb = __vlan_put_tag(nskb, nskb->vlan_proto, vlan_tx_tag_get(nskb));
8aa51d64 411 if (!nskb)
ccb1352e
JG
412 return -ENOMEM;
413
414 nskb->vlan_tci = 0;
415 skb = nskb;
416 }
417
418 if (nla_attr_size(skb->len) > USHRT_MAX) {
419 err = -EFBIG;
420 goto out;
421 }
422
c3ff8cfe 423 user_skb = genlmsg_new(upcall_msg_size(skb, upcall_info->userdata), GFP_ATOMIC);
ccb1352e
JG
424 if (!user_skb) {
425 err = -ENOMEM;
426 goto out;
427 }
428
429 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
430 0, upcall_info->cmd);
431 upcall->dp_ifindex = dp_ifindex;
432
433 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
e6445719 434 ovs_nla_put_flow(upcall_info->key, upcall_info->key, user_skb);
ccb1352e
JG
435 nla_nest_end(user_skb, nla);
436
437 if (upcall_info->userdata)
4490108b
BP
438 __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
439 nla_len(upcall_info->userdata),
440 nla_data(upcall_info->userdata));
ccb1352e
JG
441
442 nla = __nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, skb->len);
443
444 skb_copy_and_csum_dev(skb, nla_data(nla));
445
a15ff76c 446 genlmsg_end(user_skb, upcall);
15e47304 447 err = genlmsg_unicast(net, user_skb, upcall_info->portid);
ccb1352e
JG
448
449out:
450 kfree_skb(nskb);
451 return err;
452}
453
ccb1352e
JG
454static void clear_stats(struct sw_flow *flow)
455{
456 flow->used = 0;
457 flow->tcp_flags = 0;
458 flow->packet_count = 0;
459 flow->byte_count = 0;
460}
461
462static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
463{
464 struct ovs_header *ovs_header = info->userhdr;
465 struct nlattr **a = info->attrs;
466 struct sw_flow_actions *acts;
467 struct sk_buff *packet;
468 struct sw_flow *flow;
469 struct datapath *dp;
470 struct ethhdr *eth;
471 int len;
472 int err;
ccb1352e
JG
473
474 err = -EINVAL;
475 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
dded45fc 476 !a[OVS_PACKET_ATTR_ACTIONS])
ccb1352e
JG
477 goto err;
478
479 len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
480 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
481 err = -ENOMEM;
482 if (!packet)
483 goto err;
484 skb_reserve(packet, NET_IP_ALIGN);
485
32686a9d 486 nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
ccb1352e
JG
487
488 skb_reset_mac_header(packet);
489 eth = eth_hdr(packet);
490
491 /* Normally, setting the skb 'protocol' field would be handled by a
492 * call to eth_type_trans(), but it assumes there's a sending
493 * device, which we may not have. */
e5c5d22e 494 if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN)
ccb1352e
JG
495 packet->protocol = eth->h_proto;
496 else
497 packet->protocol = htons(ETH_P_802_2);
498
499 /* Build an sw_flow for sending this packet. */
500 flow = ovs_flow_alloc();
501 err = PTR_ERR(flow);
502 if (IS_ERR(flow))
503 goto err_kfree_skb;
504
03f0d916 505 err = ovs_flow_extract(packet, -1, &flow->key);
ccb1352e
JG
506 if (err)
507 goto err_flow_free;
508
e6445719 509 err = ovs_nla_get_flow_metadata(flow, a[OVS_PACKET_ATTR_KEY]);
ccb1352e
JG
510 if (err)
511 goto err_flow_free;
e6445719 512 acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_PACKET_ATTR_ACTIONS]));
ccb1352e
JG
513 err = PTR_ERR(acts);
514 if (IS_ERR(acts))
515 goto err_flow_free;
74f84a57 516
e6445719
PS
517 err = ovs_nla_copy_actions(a[OVS_PACKET_ATTR_ACTIONS],
518 &flow->key, 0, &acts);
ccb1352e 519 rcu_assign_pointer(flow->sf_acts, acts);
74f84a57
PS
520 if (err)
521 goto err_flow_free;
ccb1352e
JG
522
523 OVS_CB(packet)->flow = flow;
03f0d916 524 OVS_CB(packet)->pkt_key = &flow->key;
ccb1352e 525 packet->priority = flow->key.phy.priority;
39c7caeb 526 packet->mark = flow->key.phy.skb_mark;
ccb1352e
JG
527
528 rcu_read_lock();
46df7b81 529 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
ccb1352e
JG
530 err = -ENODEV;
531 if (!dp)
532 goto err_unlock;
533
534 local_bh_disable();
535 err = ovs_execute_actions(dp, packet);
536 local_bh_enable();
537 rcu_read_unlock();
538
03f0d916 539 ovs_flow_free(flow, false);
ccb1352e
JG
540 return err;
541
542err_unlock:
543 rcu_read_unlock();
544err_flow_free:
03f0d916 545 ovs_flow_free(flow, false);
ccb1352e
JG
546err_kfree_skb:
547 kfree_skb(packet);
548err:
549 return err;
550}
551
552static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
dded45fc 553 [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
ccb1352e
JG
554 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
555 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
556};
557
558static struct genl_ops dp_packet_genl_ops[] = {
559 { .cmd = OVS_PACKET_CMD_EXECUTE,
560 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
561 .policy = packet_policy,
562 .doit = ovs_packet_cmd_execute
563 }
564};
565
566static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats)
567{
568 int i;
ccb1352e 569
b637e498 570 stats->n_flows = ovs_flow_tbl_count(&dp->table);
ccb1352e
JG
571
572 stats->n_hit = stats->n_missed = stats->n_lost = 0;
573 for_each_possible_cpu(i) {
574 const struct dp_stats_percpu *percpu_stats;
575 struct dp_stats_percpu local_stats;
576 unsigned int start;
577
578 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
579
580 do {
581 start = u64_stats_fetch_begin_bh(&percpu_stats->sync);
582 local_stats = *percpu_stats;
583 } while (u64_stats_fetch_retry_bh(&percpu_stats->sync, start));
584
585 stats->n_hit += local_stats.n_hit;
586 stats->n_missed += local_stats.n_missed;
587 stats->n_lost += local_stats.n_lost;
588 }
589}
590
591static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
592 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
593 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
594 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
595};
596
597static struct genl_family dp_flow_genl_family = {
598 .id = GENL_ID_GENERATE,
599 .hdrsize = sizeof(struct ovs_header),
600 .name = OVS_FLOW_FAMILY,
601 .version = OVS_FLOW_VERSION,
46df7b81 602 .maxattr = OVS_FLOW_ATTR_MAX,
3a4e0d6a
PS
603 .netnsok = true,
604 .parallel_ops = true,
ccb1352e
JG
605};
606
607static struct genl_multicast_group ovs_dp_flow_multicast_group = {
608 .name = OVS_FLOW_MCGROUP
609};
610
c3ff8cfe
TG
611static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts)
612{
613 return NLMSG_ALIGN(sizeof(struct ovs_header))
614 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_KEY */
03f0d916 615 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_MASK */
c3ff8cfe
TG
616 + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
617 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
618 + nla_total_size(8) /* OVS_FLOW_ATTR_USED */
619 + nla_total_size(acts->actions_len); /* OVS_FLOW_ATTR_ACTIONS */
620}
621
8e4e1713 622/* Called with ovs_mutex. */
ccb1352e 623static int ovs_flow_cmd_fill_info(struct sw_flow *flow, struct datapath *dp,
15e47304 624 struct sk_buff *skb, u32 portid,
ccb1352e
JG
625 u32 seq, u32 flags, u8 cmd)
626{
627 const int skb_orig_len = skb->len;
74f84a57 628 struct nlattr *start;
ccb1352e
JG
629 struct ovs_flow_stats stats;
630 struct ovs_header *ovs_header;
631 struct nlattr *nla;
632 unsigned long used;
633 u8 tcp_flags;
634 int err;
635
15e47304 636 ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd);
ccb1352e
JG
637 if (!ovs_header)
638 return -EMSGSIZE;
639
640 ovs_header->dp_ifindex = get_dpifindex(dp);
641
03f0d916 642 /* Fill flow key. */
ccb1352e
JG
643 nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
644 if (!nla)
645 goto nla_put_failure;
03f0d916 646
e6445719 647 err = ovs_nla_put_flow(&flow->unmasked_key, &flow->unmasked_key, skb);
03f0d916
AZ
648 if (err)
649 goto error;
650 nla_nest_end(skb, nla);
651
652 nla = nla_nest_start(skb, OVS_FLOW_ATTR_MASK);
653 if (!nla)
654 goto nla_put_failure;
655
e6445719 656 err = ovs_nla_put_flow(&flow->key, &flow->mask->key, skb);
ccb1352e
JG
657 if (err)
658 goto error;
03f0d916 659
ccb1352e
JG
660 nla_nest_end(skb, nla);
661
662 spin_lock_bh(&flow->lock);
663 used = flow->used;
664 stats.n_packets = flow->packet_count;
665 stats.n_bytes = flow->byte_count;
666 tcp_flags = flow->tcp_flags;
667 spin_unlock_bh(&flow->lock);
668
028d6a67
DM
669 if (used &&
670 nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
671 goto nla_put_failure;
ccb1352e 672
028d6a67
DM
673 if (stats.n_packets &&
674 nla_put(skb, OVS_FLOW_ATTR_STATS,
675 sizeof(struct ovs_flow_stats), &stats))
676 goto nla_put_failure;
ccb1352e 677
028d6a67
DM
678 if (tcp_flags &&
679 nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, tcp_flags))
680 goto nla_put_failure;
ccb1352e
JG
681
682 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
683 * this is the first flow to be dumped into 'skb'. This is unusual for
684 * Netlink but individual action lists can be longer than
685 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
686 * The userspace caller can always fetch the actions separately if it
687 * really wants them. (Most userspace callers in fact don't care.)
688 *
689 * This can only fail for dump operations because the skb is always
690 * properly sized for single flows.
691 */
74f84a57
PS
692 start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
693 if (start) {
d57170b1
PS
694 const struct sw_flow_actions *sf_acts;
695
696 sf_acts = rcu_dereference_check(flow->sf_acts,
697 lockdep_ovsl_is_held());
698
e6445719
PS
699 err = ovs_nla_put_actions(sf_acts->actions,
700 sf_acts->actions_len, skb);
74f84a57
PS
701 if (!err)
702 nla_nest_end(skb, start);
703 else {
704 if (skb_orig_len)
705 goto error;
706
707 nla_nest_cancel(skb, start);
708 }
709 } else if (skb_orig_len)
710 goto nla_put_failure;
ccb1352e
JG
711
712 return genlmsg_end(skb, ovs_header);
713
714nla_put_failure:
715 err = -EMSGSIZE;
716error:
717 genlmsg_cancel(skb, ovs_header);
718 return err;
719}
720
721static struct sk_buff *ovs_flow_cmd_alloc_info(struct sw_flow *flow)
722{
723 const struct sw_flow_actions *sf_acts;
ccb1352e 724
8e4e1713 725 sf_acts = ovsl_dereference(flow->sf_acts);
ccb1352e 726
c3ff8cfe 727 return genlmsg_new(ovs_flow_cmd_msg_size(sf_acts), GFP_KERNEL);
ccb1352e
JG
728}
729
730static struct sk_buff *ovs_flow_cmd_build_info(struct sw_flow *flow,
731 struct datapath *dp,
15e47304 732 u32 portid, u32 seq, u8 cmd)
ccb1352e
JG
733{
734 struct sk_buff *skb;
735 int retval;
736
737 skb = ovs_flow_cmd_alloc_info(flow);
738 if (!skb)
739 return ERR_PTR(-ENOMEM);
740
15e47304 741 retval = ovs_flow_cmd_fill_info(flow, dp, skb, portid, seq, 0, cmd);
ccb1352e
JG
742 BUG_ON(retval < 0);
743 return skb;
744}
745
746static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info)
747{
748 struct nlattr **a = info->attrs;
749 struct ovs_header *ovs_header = info->userhdr;
03f0d916
AZ
750 struct sw_flow_key key, masked_key;
751 struct sw_flow *flow = NULL;
752 struct sw_flow_mask mask;
ccb1352e
JG
753 struct sk_buff *reply;
754 struct datapath *dp;
74f84a57 755 struct sw_flow_actions *acts = NULL;
03f0d916 756 struct sw_flow_match match;
ccb1352e 757 int error;
ccb1352e
JG
758
759 /* Extract key. */
760 error = -EINVAL;
761 if (!a[OVS_FLOW_ATTR_KEY])
762 goto error;
03f0d916
AZ
763
764 ovs_match_init(&match, &key, &mask);
e6445719
PS
765 error = ovs_nla_get_match(&match,
766 a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
ccb1352e
JG
767 if (error)
768 goto error;
769
770 /* Validate actions. */
771 if (a[OVS_FLOW_ATTR_ACTIONS]) {
e6445719 772 acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_FLOW_ATTR_ACTIONS]));
74f84a57
PS
773 error = PTR_ERR(acts);
774 if (IS_ERR(acts))
ccb1352e 775 goto error;
74f84a57 776
e6445719
PS
777 ovs_flow_mask_key(&masked_key, &key, &mask);
778 error = ovs_nla_copy_actions(a[OVS_FLOW_ATTR_ACTIONS],
779 &masked_key, 0, &acts);
03f0d916
AZ
780 if (error) {
781 OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
74f84a57 782 goto err_kfree;
03f0d916 783 }
ccb1352e
JG
784 } else if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW) {
785 error = -EINVAL;
786 goto error;
787 }
788
8e4e1713 789 ovs_lock();
46df7b81 790 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
ccb1352e
JG
791 error = -ENODEV;
792 if (!dp)
8e4e1713 793 goto err_unlock_ovs;
ccb1352e 794
03f0d916 795 /* Check if this is a duplicate flow */
b637e498 796 flow = ovs_flow_tbl_lookup(&dp->table, &key);
ccb1352e 797 if (!flow) {
ccb1352e
JG
798 /* Bail out if we're not allowed to create a new flow. */
799 error = -ENOENT;
800 if (info->genlhdr->cmd == OVS_FLOW_CMD_SET)
8e4e1713 801 goto err_unlock_ovs;
ccb1352e 802
ccb1352e
JG
803 /* Allocate flow. */
804 flow = ovs_flow_alloc();
805 if (IS_ERR(flow)) {
806 error = PTR_ERR(flow);
8e4e1713 807 goto err_unlock_ovs;
ccb1352e 808 }
ccb1352e
JG
809 clear_stats(flow);
810
03f0d916
AZ
811 flow->key = masked_key;
812 flow->unmasked_key = key;
ccb1352e
JG
813 rcu_assign_pointer(flow->sf_acts, acts);
814
815 /* Put flow in bucket. */
618ed0c8
PS
816 error = ovs_flow_tbl_insert(&dp->table, flow, &mask);
817 if (error) {
818 acts = NULL;
819 goto err_flow_free;
820 }
ccb1352e 821
15e47304 822 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
03f0d916 823 info->snd_seq, OVS_FLOW_CMD_NEW);
ccb1352e
JG
824 } else {
825 /* We found a matching flow. */
826 struct sw_flow_actions *old_acts;
ccb1352e
JG
827
828 /* Bail out if we're not allowed to modify an existing flow.
829 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
830 * because Generic Netlink treats the latter as a dump
831 * request. We also accept NLM_F_EXCL in case that bug ever
832 * gets fixed.
833 */
834 error = -EEXIST;
835 if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW &&
836 info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL))
8e4e1713 837 goto err_unlock_ovs;
ccb1352e 838
03f0d916
AZ
839 /* The unmasked key has to be the same for flow updates. */
840 error = -EINVAL;
e6445719 841 if (!ovs_flow_cmp_unmasked_key(flow, &match)) {
03f0d916
AZ
842 OVS_NLERR("Flow modification message rejected, unmasked key does not match.\n");
843 goto err_unlock_ovs;
844 }
845
ccb1352e 846 /* Update actions. */
8e4e1713 847 old_acts = ovsl_dereference(flow->sf_acts);
74f84a57 848 rcu_assign_pointer(flow->sf_acts, acts);
e6445719 849 ovs_nla_free_flow_actions(old_acts);
ccb1352e 850
15e47304 851 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
ccb1352e
JG
852 info->snd_seq, OVS_FLOW_CMD_NEW);
853
854 /* Clear stats. */
855 if (a[OVS_FLOW_ATTR_CLEAR]) {
856 spin_lock_bh(&flow->lock);
857 clear_stats(flow);
858 spin_unlock_bh(&flow->lock);
859 }
860 }
8e4e1713 861 ovs_unlock();
ccb1352e
JG
862
863 if (!IS_ERR(reply))
ed661185 864 ovs_notify(reply, info, &ovs_dp_flow_multicast_group);
ccb1352e 865 else
46df7b81 866 netlink_set_err(sock_net(skb->sk)->genl_sock, 0,
ccb1352e
JG
867 ovs_dp_flow_multicast_group.id, PTR_ERR(reply));
868 return 0;
869
03f0d916
AZ
870err_flow_free:
871 ovs_flow_free(flow, false);
8e4e1713
PS
872err_unlock_ovs:
873 ovs_unlock();
74f84a57
PS
874err_kfree:
875 kfree(acts);
ccb1352e
JG
876error:
877 return error;
878}
879
880static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
881{
882 struct nlattr **a = info->attrs;
883 struct ovs_header *ovs_header = info->userhdr;
884 struct sw_flow_key key;
885 struct sk_buff *reply;
886 struct sw_flow *flow;
887 struct datapath *dp;
03f0d916 888 struct sw_flow_match match;
ccb1352e 889 int err;
ccb1352e 890
03f0d916
AZ
891 if (!a[OVS_FLOW_ATTR_KEY]) {
892 OVS_NLERR("Flow get message rejected, Key attribute missing.\n");
ccb1352e 893 return -EINVAL;
03f0d916
AZ
894 }
895
896 ovs_match_init(&match, &key, NULL);
e6445719 897 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
ccb1352e
JG
898 if (err)
899 return err;
900
8e4e1713 901 ovs_lock();
46df7b81 902 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
8e4e1713
PS
903 if (!dp) {
904 err = -ENODEV;
905 goto unlock;
906 }
ccb1352e 907
b637e498 908 flow = ovs_flow_tbl_lookup(&dp->table, &key);
e6445719 909 if (!flow || !ovs_flow_cmp_unmasked_key(flow, &match)) {
8e4e1713
PS
910 err = -ENOENT;
911 goto unlock;
912 }
ccb1352e 913
15e47304 914 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
ccb1352e 915 info->snd_seq, OVS_FLOW_CMD_NEW);
8e4e1713
PS
916 if (IS_ERR(reply)) {
917 err = PTR_ERR(reply);
918 goto unlock;
919 }
ccb1352e 920
8e4e1713 921 ovs_unlock();
ccb1352e 922 return genlmsg_reply(reply, info);
8e4e1713
PS
923unlock:
924 ovs_unlock();
925 return err;
ccb1352e
JG
926}
927
928static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
929{
930 struct nlattr **a = info->attrs;
931 struct ovs_header *ovs_header = info->userhdr;
932 struct sw_flow_key key;
933 struct sk_buff *reply;
934 struct sw_flow *flow;
935 struct datapath *dp;
03f0d916 936 struct sw_flow_match match;
ccb1352e 937 int err;
ccb1352e 938
8e4e1713 939 ovs_lock();
46df7b81 940 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
8e4e1713
PS
941 if (!dp) {
942 err = -ENODEV;
943 goto unlock;
944 }
46df7b81 945
8e4e1713 946 if (!a[OVS_FLOW_ATTR_KEY]) {
b637e498 947 err = ovs_flow_tbl_flush(&dp->table);
8e4e1713
PS
948 goto unlock;
949 }
03f0d916
AZ
950
951 ovs_match_init(&match, &key, NULL);
e6445719 952 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
ccb1352e 953 if (err)
8e4e1713 954 goto unlock;
ccb1352e 955
b637e498 956 flow = ovs_flow_tbl_lookup(&dp->table, &key);
e6445719 957 if (!flow || !ovs_flow_cmp_unmasked_key(flow, &match)) {
8e4e1713
PS
958 err = -ENOENT;
959 goto unlock;
960 }
ccb1352e
JG
961
962 reply = ovs_flow_cmd_alloc_info(flow);
8e4e1713
PS
963 if (!reply) {
964 err = -ENOMEM;
965 goto unlock;
966 }
ccb1352e 967
b637e498 968 ovs_flow_tbl_remove(&dp->table, flow);
ccb1352e 969
15e47304 970 err = ovs_flow_cmd_fill_info(flow, dp, reply, info->snd_portid,
ccb1352e
JG
971 info->snd_seq, 0, OVS_FLOW_CMD_DEL);
972 BUG_ON(err < 0);
973
03f0d916 974 ovs_flow_free(flow, true);
8e4e1713 975 ovs_unlock();
ccb1352e 976
ed661185 977 ovs_notify(reply, info, &ovs_dp_flow_multicast_group);
ccb1352e 978 return 0;
8e4e1713
PS
979unlock:
980 ovs_unlock();
981 return err;
ccb1352e
JG
982}
983
984static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
985{
986 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
b637e498 987 struct table_instance *ti;
ccb1352e 988 struct datapath *dp;
ccb1352e 989
d57170b1 990 rcu_read_lock();
46df7b81 991 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
8e4e1713 992 if (!dp) {
d57170b1 993 rcu_read_unlock();
ccb1352e 994 return -ENODEV;
8e4e1713 995 }
ccb1352e 996
b637e498 997 ti = rcu_dereference(dp->table.ti);
ccb1352e
JG
998 for (;;) {
999 struct sw_flow *flow;
1000 u32 bucket, obj;
1001
1002 bucket = cb->args[0];
1003 obj = cb->args[1];
b637e498 1004 flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj);
ccb1352e
JG
1005 if (!flow)
1006 break;
1007
1008 if (ovs_flow_cmd_fill_info(flow, dp, skb,
15e47304 1009 NETLINK_CB(cb->skb).portid,
ccb1352e
JG
1010 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1011 OVS_FLOW_CMD_NEW) < 0)
1012 break;
1013
1014 cb->args[0] = bucket;
1015 cb->args[1] = obj;
1016 }
d57170b1 1017 rcu_read_unlock();
ccb1352e
JG
1018 return skb->len;
1019}
1020
1021static struct genl_ops dp_flow_genl_ops[] = {
1022 { .cmd = OVS_FLOW_CMD_NEW,
1023 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1024 .policy = flow_policy,
1025 .doit = ovs_flow_cmd_new_or_set
1026 },
1027 { .cmd = OVS_FLOW_CMD_DEL,
1028 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1029 .policy = flow_policy,
1030 .doit = ovs_flow_cmd_del
1031 },
1032 { .cmd = OVS_FLOW_CMD_GET,
1033 .flags = 0, /* OK for unprivileged users. */
1034 .policy = flow_policy,
1035 .doit = ovs_flow_cmd_get,
1036 .dumpit = ovs_flow_cmd_dump
1037 },
1038 { .cmd = OVS_FLOW_CMD_SET,
1039 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1040 .policy = flow_policy,
1041 .doit = ovs_flow_cmd_new_or_set,
1042 },
1043};
1044
1045static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1046 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1047 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1048};
1049
1050static struct genl_family dp_datapath_genl_family = {
1051 .id = GENL_ID_GENERATE,
1052 .hdrsize = sizeof(struct ovs_header),
1053 .name = OVS_DATAPATH_FAMILY,
1054 .version = OVS_DATAPATH_VERSION,
46df7b81 1055 .maxattr = OVS_DP_ATTR_MAX,
3a4e0d6a
PS
1056 .netnsok = true,
1057 .parallel_ops = true,
ccb1352e
JG
1058};
1059
1060static struct genl_multicast_group ovs_dp_datapath_multicast_group = {
1061 .name = OVS_DATAPATH_MCGROUP
1062};
1063
c3ff8cfe
TG
1064static size_t ovs_dp_cmd_msg_size(void)
1065{
1066 size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
1067
1068 msgsize += nla_total_size(IFNAMSIZ);
1069 msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
1070
1071 return msgsize;
1072}
1073
ccb1352e 1074static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
15e47304 1075 u32 portid, u32 seq, u32 flags, u8 cmd)
ccb1352e
JG
1076{
1077 struct ovs_header *ovs_header;
1078 struct ovs_dp_stats dp_stats;
1079 int err;
1080
15e47304 1081 ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
ccb1352e
JG
1082 flags, cmd);
1083 if (!ovs_header)
1084 goto error;
1085
1086 ovs_header->dp_ifindex = get_dpifindex(dp);
1087
1088 rcu_read_lock();
1089 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
1090 rcu_read_unlock();
1091 if (err)
1092 goto nla_put_failure;
1093
1094 get_dp_stats(dp, &dp_stats);
028d6a67
DM
1095 if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats), &dp_stats))
1096 goto nla_put_failure;
ccb1352e
JG
1097
1098 return genlmsg_end(skb, ovs_header);
1099
1100nla_put_failure:
1101 genlmsg_cancel(skb, ovs_header);
1102error:
1103 return -EMSGSIZE;
1104}
1105
15e47304 1106static struct sk_buff *ovs_dp_cmd_build_info(struct datapath *dp, u32 portid,
ccb1352e
JG
1107 u32 seq, u8 cmd)
1108{
1109 struct sk_buff *skb;
1110 int retval;
1111
c3ff8cfe 1112 skb = genlmsg_new(ovs_dp_cmd_msg_size(), GFP_KERNEL);
ccb1352e
JG
1113 if (!skb)
1114 return ERR_PTR(-ENOMEM);
1115
15e47304 1116 retval = ovs_dp_cmd_fill_info(dp, skb, portid, seq, 0, cmd);
ccb1352e
JG
1117 if (retval < 0) {
1118 kfree_skb(skb);
1119 return ERR_PTR(retval);
1120 }
1121 return skb;
1122}
1123
8e4e1713 1124/* Called with ovs_mutex. */
46df7b81
PS
1125static struct datapath *lookup_datapath(struct net *net,
1126 struct ovs_header *ovs_header,
ccb1352e
JG
1127 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1128{
1129 struct datapath *dp;
1130
1131 if (!a[OVS_DP_ATTR_NAME])
46df7b81 1132 dp = get_dp(net, ovs_header->dp_ifindex);
ccb1352e
JG
1133 else {
1134 struct vport *vport;
1135
1136 rcu_read_lock();
46df7b81 1137 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
ccb1352e
JG
1138 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
1139 rcu_read_unlock();
1140 }
1141 return dp ? dp : ERR_PTR(-ENODEV);
1142}
1143
1144static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1145{
1146 struct nlattr **a = info->attrs;
1147 struct vport_parms parms;
1148 struct sk_buff *reply;
1149 struct datapath *dp;
1150 struct vport *vport;
46df7b81 1151 struct ovs_net *ovs_net;
15eac2a7 1152 int err, i;
ccb1352e
JG
1153
1154 err = -EINVAL;
1155 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1156 goto err;
1157
8e4e1713 1158 ovs_lock();
ccb1352e
JG
1159
1160 err = -ENOMEM;
1161 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1162 if (dp == NULL)
8e4e1713 1163 goto err_unlock_ovs;
46df7b81 1164
46df7b81 1165 ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
ccb1352e
JG
1166
1167 /* Allocate table. */
b637e498
PS
1168 err = ovs_flow_tbl_init(&dp->table);
1169 if (err)
ccb1352e
JG
1170 goto err_free_dp;
1171
1172 dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
1173 if (!dp->stats_percpu) {
1174 err = -ENOMEM;
1175 goto err_destroy_table;
1176 }
1177
15eac2a7 1178 dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
e6445719 1179 GFP_KERNEL);
15eac2a7
PS
1180 if (!dp->ports) {
1181 err = -ENOMEM;
1182 goto err_destroy_percpu;
1183 }
1184
1185 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1186 INIT_HLIST_HEAD(&dp->ports[i]);
1187
ccb1352e
JG
1188 /* Set up our datapath device. */
1189 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1190 parms.type = OVS_VPORT_TYPE_INTERNAL;
1191 parms.options = NULL;
1192 parms.dp = dp;
1193 parms.port_no = OVSP_LOCAL;
15e47304 1194 parms.upcall_portid = nla_get_u32(a[OVS_DP_ATTR_UPCALL_PID]);
ccb1352e
JG
1195
1196 vport = new_vport(&parms);
1197 if (IS_ERR(vport)) {
1198 err = PTR_ERR(vport);
1199 if (err == -EBUSY)
1200 err = -EEXIST;
1201
15eac2a7 1202 goto err_destroy_ports_array;
ccb1352e
JG
1203 }
1204
15e47304 1205 reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
ccb1352e
JG
1206 info->snd_seq, OVS_DP_CMD_NEW);
1207 err = PTR_ERR(reply);
1208 if (IS_ERR(reply))
1209 goto err_destroy_local_port;
1210
46df7b81 1211 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
59a35d60 1212 list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
8e4e1713
PS
1213
1214 ovs_unlock();
ccb1352e 1215
ed661185 1216 ovs_notify(reply, info, &ovs_dp_datapath_multicast_group);
ccb1352e
JG
1217 return 0;
1218
1219err_destroy_local_port:
8e4e1713 1220 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
15eac2a7
PS
1221err_destroy_ports_array:
1222 kfree(dp->ports);
ccb1352e
JG
1223err_destroy_percpu:
1224 free_percpu(dp->stats_percpu);
1225err_destroy_table:
618ed0c8 1226 ovs_flow_tbl_destroy(&dp->table);
ccb1352e 1227err_free_dp:
46df7b81 1228 release_net(ovs_dp_get_net(dp));
ccb1352e 1229 kfree(dp);
8e4e1713
PS
1230err_unlock_ovs:
1231 ovs_unlock();
ccb1352e
JG
1232err:
1233 return err;
1234}
1235
8e4e1713 1236/* Called with ovs_mutex. */
46df7b81 1237static void __dp_destroy(struct datapath *dp)
ccb1352e 1238{
15eac2a7 1239 int i;
ccb1352e 1240
15eac2a7
PS
1241 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1242 struct vport *vport;
b67bfe0d 1243 struct hlist_node *n;
15eac2a7 1244
b67bfe0d 1245 hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
15eac2a7
PS
1246 if (vport->port_no != OVSP_LOCAL)
1247 ovs_dp_detach_port(vport);
1248 }
ccb1352e 1249
59a35d60 1250 list_del_rcu(&dp->list_node);
ccb1352e 1251
8e4e1713
PS
1252 /* OVSP_LOCAL is datapath internal port. We need to make sure that
1253 * all port in datapath are destroyed first before freeing datapath.
ccb1352e 1254 */
8e4e1713 1255 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
ccb1352e
JG
1256
1257 call_rcu(&dp->rcu, destroy_dp_rcu);
46df7b81
PS
1258}
1259
1260static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1261{
1262 struct sk_buff *reply;
1263 struct datapath *dp;
1264 int err;
1265
8e4e1713 1266 ovs_lock();
46df7b81
PS
1267 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1268 err = PTR_ERR(dp);
1269 if (IS_ERR(dp))
8e4e1713 1270 goto unlock;
46df7b81 1271
15e47304 1272 reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
46df7b81
PS
1273 info->snd_seq, OVS_DP_CMD_DEL);
1274 err = PTR_ERR(reply);
1275 if (IS_ERR(reply))
8e4e1713 1276 goto unlock;
46df7b81
PS
1277
1278 __dp_destroy(dp);
8e4e1713 1279 ovs_unlock();
ccb1352e 1280
ed661185 1281 ovs_notify(reply, info, &ovs_dp_datapath_multicast_group);
ccb1352e
JG
1282
1283 return 0;
8e4e1713
PS
1284unlock:
1285 ovs_unlock();
1286 return err;
ccb1352e
JG
1287}
1288
1289static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1290{
1291 struct sk_buff *reply;
1292 struct datapath *dp;
1293 int err;
1294
8e4e1713 1295 ovs_lock();
46df7b81 1296 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
8e4e1713 1297 err = PTR_ERR(dp);
ccb1352e 1298 if (IS_ERR(dp))
8e4e1713 1299 goto unlock;
ccb1352e 1300
15e47304 1301 reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
ccb1352e
JG
1302 info->snd_seq, OVS_DP_CMD_NEW);
1303 if (IS_ERR(reply)) {
1304 err = PTR_ERR(reply);
46df7b81 1305 netlink_set_err(sock_net(skb->sk)->genl_sock, 0,
ccb1352e 1306 ovs_dp_datapath_multicast_group.id, err);
8e4e1713
PS
1307 err = 0;
1308 goto unlock;
ccb1352e
JG
1309 }
1310
8e4e1713 1311 ovs_unlock();
ed661185 1312 ovs_notify(reply, info, &ovs_dp_datapath_multicast_group);
ccb1352e
JG
1313
1314 return 0;
8e4e1713
PS
1315unlock:
1316 ovs_unlock();
1317 return err;
ccb1352e
JG
1318}
1319
1320static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1321{
1322 struct sk_buff *reply;
1323 struct datapath *dp;
8e4e1713 1324 int err;
ccb1352e 1325
8e4e1713 1326 ovs_lock();
46df7b81 1327 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
8e4e1713
PS
1328 if (IS_ERR(dp)) {
1329 err = PTR_ERR(dp);
1330 goto unlock;
1331 }
ccb1352e 1332
15e47304 1333 reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
ccb1352e 1334 info->snd_seq, OVS_DP_CMD_NEW);
8e4e1713
PS
1335 if (IS_ERR(reply)) {
1336 err = PTR_ERR(reply);
1337 goto unlock;
1338 }
ccb1352e 1339
8e4e1713 1340 ovs_unlock();
ccb1352e 1341 return genlmsg_reply(reply, info);
8e4e1713
PS
1342
1343unlock:
1344 ovs_unlock();
1345 return err;
ccb1352e
JG
1346}
1347
1348static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1349{
46df7b81 1350 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
ccb1352e
JG
1351 struct datapath *dp;
1352 int skip = cb->args[0];
1353 int i = 0;
1354
59a35d60
PS
1355 rcu_read_lock();
1356 list_for_each_entry_rcu(dp, &ovs_net->dps, list_node) {
77676fdb 1357 if (i >= skip &&
15e47304 1358 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
ccb1352e
JG
1359 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1360 OVS_DP_CMD_NEW) < 0)
1361 break;
1362 i++;
1363 }
59a35d60 1364 rcu_read_unlock();
ccb1352e
JG
1365
1366 cb->args[0] = i;
1367
1368 return skb->len;
1369}
1370
1371static struct genl_ops dp_datapath_genl_ops[] = {
1372 { .cmd = OVS_DP_CMD_NEW,
1373 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1374 .policy = datapath_policy,
1375 .doit = ovs_dp_cmd_new
1376 },
1377 { .cmd = OVS_DP_CMD_DEL,
1378 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1379 .policy = datapath_policy,
1380 .doit = ovs_dp_cmd_del
1381 },
1382 { .cmd = OVS_DP_CMD_GET,
1383 .flags = 0, /* OK for unprivileged users. */
1384 .policy = datapath_policy,
1385 .doit = ovs_dp_cmd_get,
1386 .dumpit = ovs_dp_cmd_dump
1387 },
1388 { .cmd = OVS_DP_CMD_SET,
1389 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1390 .policy = datapath_policy,
1391 .doit = ovs_dp_cmd_set,
1392 },
1393};
1394
1395static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
1396 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1397 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
1398 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
1399 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
1400 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1401 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
1402};
1403
1404static struct genl_family dp_vport_genl_family = {
1405 .id = GENL_ID_GENERATE,
1406 .hdrsize = sizeof(struct ovs_header),
1407 .name = OVS_VPORT_FAMILY,
1408 .version = OVS_VPORT_VERSION,
46df7b81 1409 .maxattr = OVS_VPORT_ATTR_MAX,
3a4e0d6a
PS
1410 .netnsok = true,
1411 .parallel_ops = true,
ccb1352e
JG
1412};
1413
1414struct genl_multicast_group ovs_dp_vport_multicast_group = {
1415 .name = OVS_VPORT_MCGROUP
1416};
1417
8e4e1713 1418/* Called with ovs_mutex or RCU read lock. */
ccb1352e 1419static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
15e47304 1420 u32 portid, u32 seq, u32 flags, u8 cmd)
ccb1352e
JG
1421{
1422 struct ovs_header *ovs_header;
1423 struct ovs_vport_stats vport_stats;
1424 int err;
1425
15e47304 1426 ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
ccb1352e
JG
1427 flags, cmd);
1428 if (!ovs_header)
1429 return -EMSGSIZE;
1430
1431 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1432
028d6a67
DM
1433 if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1434 nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
1435 nla_put_string(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport)) ||
15e47304 1436 nla_put_u32(skb, OVS_VPORT_ATTR_UPCALL_PID, vport->upcall_portid))
028d6a67 1437 goto nla_put_failure;
ccb1352e
JG
1438
1439 ovs_vport_get_stats(vport, &vport_stats);
028d6a67
DM
1440 if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
1441 &vport_stats))
1442 goto nla_put_failure;
ccb1352e
JG
1443
1444 err = ovs_vport_get_options(vport, skb);
1445 if (err == -EMSGSIZE)
1446 goto error;
1447
1448 return genlmsg_end(skb, ovs_header);
1449
1450nla_put_failure:
1451 err = -EMSGSIZE;
1452error:
1453 genlmsg_cancel(skb, ovs_header);
1454 return err;
1455}
1456
8e4e1713 1457/* Called with ovs_mutex or RCU read lock. */
15e47304 1458struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
ccb1352e
JG
1459 u32 seq, u8 cmd)
1460{
1461 struct sk_buff *skb;
1462 int retval;
1463
1464 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1465 if (!skb)
1466 return ERR_PTR(-ENOMEM);
1467
15e47304 1468 retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
a9341512
JG
1469 BUG_ON(retval < 0);
1470
ccb1352e
JG
1471 return skb;
1472}
1473
8e4e1713 1474/* Called with ovs_mutex or RCU read lock. */
46df7b81
PS
1475static struct vport *lookup_vport(struct net *net,
1476 struct ovs_header *ovs_header,
ccb1352e
JG
1477 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1478{
1479 struct datapath *dp;
1480 struct vport *vport;
1481
1482 if (a[OVS_VPORT_ATTR_NAME]) {
46df7b81 1483 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
ccb1352e
JG
1484 if (!vport)
1485 return ERR_PTR(-ENODEV);
651a68ea
BP
1486 if (ovs_header->dp_ifindex &&
1487 ovs_header->dp_ifindex != get_dpifindex(vport->dp))
1488 return ERR_PTR(-ENODEV);
ccb1352e
JG
1489 return vport;
1490 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1491 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1492
1493 if (port_no >= DP_MAX_PORTS)
1494 return ERR_PTR(-EFBIG);
1495
46df7b81 1496 dp = get_dp(net, ovs_header->dp_ifindex);
ccb1352e
JG
1497 if (!dp)
1498 return ERR_PTR(-ENODEV);
1499
8e4e1713 1500 vport = ovs_vport_ovsl_rcu(dp, port_no);
ccb1352e 1501 if (!vport)
14408dba 1502 return ERR_PTR(-ENODEV);
ccb1352e
JG
1503 return vport;
1504 } else
1505 return ERR_PTR(-EINVAL);
1506}
1507
1508static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1509{
1510 struct nlattr **a = info->attrs;
1511 struct ovs_header *ovs_header = info->userhdr;
1512 struct vport_parms parms;
1513 struct sk_buff *reply;
1514 struct vport *vport;
1515 struct datapath *dp;
1516 u32 port_no;
1517 int err;
1518
1519 err = -EINVAL;
1520 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
1521 !a[OVS_VPORT_ATTR_UPCALL_PID])
1522 goto exit;
1523
8e4e1713 1524 ovs_lock();
46df7b81 1525 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
ccb1352e
JG
1526 err = -ENODEV;
1527 if (!dp)
1528 goto exit_unlock;
1529
1530 if (a[OVS_VPORT_ATTR_PORT_NO]) {
1531 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1532
1533 err = -EFBIG;
1534 if (port_no >= DP_MAX_PORTS)
1535 goto exit_unlock;
1536
8e4e1713 1537 vport = ovs_vport_ovsl(dp, port_no);
ccb1352e
JG
1538 err = -EBUSY;
1539 if (vport)
1540 goto exit_unlock;
1541 } else {
1542 for (port_no = 1; ; port_no++) {
1543 if (port_no >= DP_MAX_PORTS) {
1544 err = -EFBIG;
1545 goto exit_unlock;
1546 }
8e4e1713 1547 vport = ovs_vport_ovsl(dp, port_no);
ccb1352e
JG
1548 if (!vport)
1549 break;
1550 }
1551 }
1552
1553 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
1554 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1555 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
1556 parms.dp = dp;
1557 parms.port_no = port_no;
15e47304 1558 parms.upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
ccb1352e
JG
1559
1560 vport = new_vport(&parms);
1561 err = PTR_ERR(vport);
1562 if (IS_ERR(vport))
1563 goto exit_unlock;
1564
cb7c5bdf 1565 err = 0;
15e47304 1566 reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq,
ccb1352e
JG
1567 OVS_VPORT_CMD_NEW);
1568 if (IS_ERR(reply)) {
1569 err = PTR_ERR(reply);
1570 ovs_dp_detach_port(vport);
1571 goto exit_unlock;
1572 }
ed661185
TG
1573
1574 ovs_notify(reply, info, &ovs_dp_vport_multicast_group);
ccb1352e
JG
1575
1576exit_unlock:
8e4e1713 1577 ovs_unlock();
ccb1352e
JG
1578exit:
1579 return err;
1580}
1581
1582static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
1583{
1584 struct nlattr **a = info->attrs;
1585 struct sk_buff *reply;
1586 struct vport *vport;
1587 int err;
1588
8e4e1713 1589 ovs_lock();
46df7b81 1590 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
ccb1352e
JG
1591 err = PTR_ERR(vport);
1592 if (IS_ERR(vport))
1593 goto exit_unlock;
1594
ccb1352e 1595 if (a[OVS_VPORT_ATTR_TYPE] &&
f44f3408 1596 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
ccb1352e 1597 err = -EINVAL;
f44f3408
JG
1598 goto exit_unlock;
1599 }
ccb1352e 1600
a9341512
JG
1601 reply = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1602 if (!reply) {
1603 err = -ENOMEM;
1604 goto exit_unlock;
1605 }
1606
f44f3408 1607 if (a[OVS_VPORT_ATTR_OPTIONS]) {
ccb1352e 1608 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
f44f3408
JG
1609 if (err)
1610 goto exit_free;
1611 }
a9341512 1612
03fbf8b3 1613 if (a[OVS_VPORT_ATTR_UPCALL_PID])
15e47304 1614 vport->upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
ccb1352e 1615
a9341512
JG
1616 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1617 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1618 BUG_ON(err < 0);
ccb1352e 1619
8e4e1713 1620 ovs_unlock();
ed661185 1621 ovs_notify(reply, info, &ovs_dp_vport_multicast_group);
8e4e1713 1622 return 0;
ccb1352e 1623
a9341512
JG
1624exit_free:
1625 kfree_skb(reply);
ccb1352e 1626exit_unlock:
8e4e1713 1627 ovs_unlock();
ccb1352e
JG
1628 return err;
1629}
1630
1631static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
1632{
1633 struct nlattr **a = info->attrs;
1634 struct sk_buff *reply;
1635 struct vport *vport;
1636 int err;
1637
8e4e1713 1638 ovs_lock();
46df7b81 1639 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
ccb1352e
JG
1640 err = PTR_ERR(vport);
1641 if (IS_ERR(vport))
1642 goto exit_unlock;
1643
1644 if (vport->port_no == OVSP_LOCAL) {
1645 err = -EINVAL;
1646 goto exit_unlock;
1647 }
1648
74f84a57
PS
1649 reply = ovs_vport_cmd_build_info(vport, info->snd_portid,
1650 info->snd_seq, OVS_VPORT_CMD_DEL);
ccb1352e
JG
1651 err = PTR_ERR(reply);
1652 if (IS_ERR(reply))
1653 goto exit_unlock;
1654
734907e8 1655 err = 0;
ccb1352e
JG
1656 ovs_dp_detach_port(vport);
1657
ed661185 1658 ovs_notify(reply, info, &ovs_dp_vport_multicast_group);
ccb1352e
JG
1659
1660exit_unlock:
8e4e1713 1661 ovs_unlock();
ccb1352e
JG
1662 return err;
1663}
1664
1665static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
1666{
1667 struct nlattr **a = info->attrs;
1668 struct ovs_header *ovs_header = info->userhdr;
1669 struct sk_buff *reply;
1670 struct vport *vport;
1671 int err;
1672
1673 rcu_read_lock();
46df7b81 1674 vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
ccb1352e
JG
1675 err = PTR_ERR(vport);
1676 if (IS_ERR(vport))
1677 goto exit_unlock;
1678
74f84a57
PS
1679 reply = ovs_vport_cmd_build_info(vport, info->snd_portid,
1680 info->snd_seq, OVS_VPORT_CMD_NEW);
ccb1352e
JG
1681 err = PTR_ERR(reply);
1682 if (IS_ERR(reply))
1683 goto exit_unlock;
1684
1685 rcu_read_unlock();
1686
1687 return genlmsg_reply(reply, info);
1688
1689exit_unlock:
1690 rcu_read_unlock();
1691 return err;
1692}
1693
1694static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1695{
1696 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1697 struct datapath *dp;
15eac2a7
PS
1698 int bucket = cb->args[0], skip = cb->args[1];
1699 int i, j = 0;
ccb1352e 1700
46df7b81 1701 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
ccb1352e
JG
1702 if (!dp)
1703 return -ENODEV;
1704
1705 rcu_read_lock();
15eac2a7 1706 for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
ccb1352e 1707 struct vport *vport;
15eac2a7
PS
1708
1709 j = 0;
b67bfe0d 1710 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
15eac2a7
PS
1711 if (j >= skip &&
1712 ovs_vport_cmd_fill_info(vport, skb,
15e47304 1713 NETLINK_CB(cb->skb).portid,
15eac2a7
PS
1714 cb->nlh->nlmsg_seq,
1715 NLM_F_MULTI,
1716 OVS_VPORT_CMD_NEW) < 0)
1717 goto out;
1718
1719 j++;
1720 }
1721 skip = 0;
ccb1352e 1722 }
15eac2a7 1723out:
ccb1352e
JG
1724 rcu_read_unlock();
1725
15eac2a7
PS
1726 cb->args[0] = i;
1727 cb->args[1] = j;
ccb1352e 1728
15eac2a7 1729 return skb->len;
ccb1352e
JG
1730}
1731
ccb1352e
JG
1732static struct genl_ops dp_vport_genl_ops[] = {
1733 { .cmd = OVS_VPORT_CMD_NEW,
1734 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1735 .policy = vport_policy,
1736 .doit = ovs_vport_cmd_new
1737 },
1738 { .cmd = OVS_VPORT_CMD_DEL,
1739 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1740 .policy = vport_policy,
1741 .doit = ovs_vport_cmd_del
1742 },
1743 { .cmd = OVS_VPORT_CMD_GET,
1744 .flags = 0, /* OK for unprivileged users. */
1745 .policy = vport_policy,
1746 .doit = ovs_vport_cmd_get,
1747 .dumpit = ovs_vport_cmd_dump
1748 },
1749 { .cmd = OVS_VPORT_CMD_SET,
1750 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1751 .policy = vport_policy,
1752 .doit = ovs_vport_cmd_set,
1753 },
1754};
1755
1756struct genl_family_and_ops {
1757 struct genl_family *family;
1758 struct genl_ops *ops;
1759 int n_ops;
1760 struct genl_multicast_group *group;
1761};
1762
1763static const struct genl_family_and_ops dp_genl_families[] = {
1764 { &dp_datapath_genl_family,
1765 dp_datapath_genl_ops, ARRAY_SIZE(dp_datapath_genl_ops),
1766 &ovs_dp_datapath_multicast_group },
1767 { &dp_vport_genl_family,
1768 dp_vport_genl_ops, ARRAY_SIZE(dp_vport_genl_ops),
1769 &ovs_dp_vport_multicast_group },
1770 { &dp_flow_genl_family,
1771 dp_flow_genl_ops, ARRAY_SIZE(dp_flow_genl_ops),
1772 &ovs_dp_flow_multicast_group },
1773 { &dp_packet_genl_family,
1774 dp_packet_genl_ops, ARRAY_SIZE(dp_packet_genl_ops),
1775 NULL },
1776};
1777
1778static void dp_unregister_genl(int n_families)
1779{
1780 int i;
1781
1782 for (i = 0; i < n_families; i++)
1783 genl_unregister_family(dp_genl_families[i].family);
1784}
1785
1786static int dp_register_genl(void)
1787{
1788 int n_registered;
1789 int err;
1790 int i;
1791
1792 n_registered = 0;
1793 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
1794 const struct genl_family_and_ops *f = &dp_genl_families[i];
1795
1796 err = genl_register_family_with_ops(f->family, f->ops,
1797 f->n_ops);
1798 if (err)
1799 goto error;
1800 n_registered++;
1801
1802 if (f->group) {
1803 err = genl_register_mc_group(f->family, f->group);
1804 if (err)
1805 goto error;
1806 }
1807 }
1808
1809 return 0;
1810
1811error:
1812 dp_unregister_genl(n_registered);
1813 return err;
1814}
1815
46df7b81
PS
1816static int __net_init ovs_init_net(struct net *net)
1817{
1818 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
1819
1820 INIT_LIST_HEAD(&ovs_net->dps);
8e4e1713 1821 INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
46df7b81
PS
1822 return 0;
1823}
1824
1825static void __net_exit ovs_exit_net(struct net *net)
1826{
46df7b81 1827 struct datapath *dp, *dp_next;
8e4e1713 1828 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
46df7b81 1829
8e4e1713 1830 ovs_lock();
46df7b81
PS
1831 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
1832 __dp_destroy(dp);
8e4e1713
PS
1833 ovs_unlock();
1834
1835 cancel_work_sync(&ovs_net->dp_notify_work);
46df7b81
PS
1836}
1837
1838static struct pernet_operations ovs_net_ops = {
1839 .init = ovs_init_net,
1840 .exit = ovs_exit_net,
1841 .id = &ovs_net_id,
1842 .size = sizeof(struct ovs_net),
1843};
1844
ccb1352e
JG
1845static int __init dp_init(void)
1846{
ccb1352e
JG
1847 int err;
1848
3523b29b 1849 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
ccb1352e
JG
1850
1851 pr_info("Open vSwitch switching datapath\n");
1852
1853 err = ovs_flow_init();
1854 if (err)
1855 goto error;
1856
1857 err = ovs_vport_init();
1858 if (err)
1859 goto error_flow_exit;
1860
46df7b81 1861 err = register_pernet_device(&ovs_net_ops);
ccb1352e
JG
1862 if (err)
1863 goto error_vport_exit;
1864
46df7b81
PS
1865 err = register_netdevice_notifier(&ovs_dp_device_notifier);
1866 if (err)
1867 goto error_netns_exit;
1868
ccb1352e
JG
1869 err = dp_register_genl();
1870 if (err < 0)
1871 goto error_unreg_notifier;
1872
ccb1352e
JG
1873 return 0;
1874
1875error_unreg_notifier:
1876 unregister_netdevice_notifier(&ovs_dp_device_notifier);
46df7b81
PS
1877error_netns_exit:
1878 unregister_pernet_device(&ovs_net_ops);
ccb1352e
JG
1879error_vport_exit:
1880 ovs_vport_exit();
1881error_flow_exit:
1882 ovs_flow_exit();
1883error:
1884 return err;
1885}
1886
1887static void dp_cleanup(void)
1888{
ccb1352e
JG
1889 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
1890 unregister_netdevice_notifier(&ovs_dp_device_notifier);
46df7b81
PS
1891 unregister_pernet_device(&ovs_net_ops);
1892 rcu_barrier();
ccb1352e
JG
1893 ovs_vport_exit();
1894 ovs_flow_exit();
1895}
1896
1897module_init(dp_init);
1898module_exit(dp_cleanup);
1899
1900MODULE_DESCRIPTION("Open vSwitch switching datapath");
1901MODULE_LICENSE("GPL");
This page took 0.360801 seconds and 5 git commands to generate.