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