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