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