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