2 * Copyright (c) 2007-2013 Nicira, Inc.
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.
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.
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
19 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21 #include <linux/init.h>
22 #include <linux/module.h>
23 #include <linux/if_arp.h>
24 #include <linux/if_vlan.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/lockdep.h>
48 #include <linux/openvswitch.h>
49 #include <linux/rculist.h>
50 #include <linux/dmi.h>
51 #include <linux/workqueue.h>
52 #include <net/genetlink.h>
53 #include <net/net_namespace.h>
54 #include <net/netns/generic.h>
58 #include "flow_netlink.h"
59 #include "vport-internal_dev.h"
60 #include "vport-netdev.h"
62 #define REHASH_FLOW_INTERVAL (10 * 60 * HZ)
64 int ovs_net_id __read_mostly;
66 static void ovs_notify(struct sk_buff *skb, struct genl_info *info,
67 struct genl_multicast_group *grp)
69 genl_notify(skb, genl_info_net(info), info->snd_portid,
70 grp->id, info->nlhdr, GFP_KERNEL);
76 * All writes e.g. Writes to device state (add/remove datapath, port, set
77 * operations on vports, etc.), Writes to other state (flow table
78 * modifications, set miscellaneous datapath parameters, etc.) are protected
81 * Reads are protected by RCU.
83 * There are a few special cases (mostly stats) that have their own
84 * synchronization but they nest under all of above and don't interact with
87 * The RTNL lock nests inside ovs_mutex.
90 static DEFINE_MUTEX(ovs_mutex);
94 mutex_lock(&ovs_mutex);
99 mutex_unlock(&ovs_mutex);
102 #ifdef CONFIG_LOCKDEP
103 int lockdep_ovsl_is_held(void)
106 return lockdep_is_held(&ovs_mutex);
112 static struct vport *new_vport(const struct vport_parms *);
113 static int queue_gso_packets(struct net *, int dp_ifindex, struct sk_buff *,
114 const struct dp_upcall_info *);
115 static int queue_userspace_packet(struct net *, int dp_ifindex,
117 const struct dp_upcall_info *);
119 /* Must be called with rcu_read_lock or ovs_mutex. */
120 static struct datapath *get_dp(struct net *net, int dp_ifindex)
122 struct datapath *dp = NULL;
123 struct net_device *dev;
126 dev = dev_get_by_index_rcu(net, dp_ifindex);
128 struct vport *vport = ovs_internal_dev_get_vport(dev);
137 /* Must be called with rcu_read_lock or ovs_mutex. */
138 const char *ovs_dp_name(const struct datapath *dp)
140 struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL);
141 return vport->ops->get_name(vport);
144 static int get_dpifindex(struct datapath *dp)
151 local = ovs_vport_rcu(dp, OVSP_LOCAL);
153 ifindex = netdev_vport_priv(local)->dev->ifindex;
162 static void destroy_dp_rcu(struct rcu_head *rcu)
164 struct datapath *dp = container_of(rcu, struct datapath, rcu);
166 ovs_flow_tbl_destroy((__force struct flow_table *)dp->table, false);
167 free_percpu(dp->stats_percpu);
168 release_net(ovs_dp_get_net(dp));
173 static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
176 return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
179 struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
182 struct hlist_head *head;
184 head = vport_hash_bucket(dp, port_no);
185 hlist_for_each_entry_rcu(vport, head, dp_hash_node) {
186 if (vport->port_no == port_no)
192 /* Called with ovs_mutex. */
193 static struct vport *new_vport(const struct vport_parms *parms)
197 vport = ovs_vport_add(parms);
198 if (!IS_ERR(vport)) {
199 struct datapath *dp = parms->dp;
200 struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
202 hlist_add_head_rcu(&vport->dp_hash_node, head);
207 void ovs_dp_detach_port(struct vport *p)
211 /* First drop references to device. */
212 hlist_del_rcu(&p->dp_hash_node);
214 /* Then destroy it. */
218 /* Must be called with rcu_read_lock. */
219 void ovs_dp_process_received_packet(struct vport *p, struct sk_buff *skb)
221 struct datapath *dp = p->dp;
222 struct sw_flow *flow;
223 struct dp_stats_percpu *stats;
224 struct sw_flow_key key;
228 stats = this_cpu_ptr(dp->stats_percpu);
230 /* Extract flow from 'skb' into 'key'. */
231 error = ovs_flow_extract(skb, p->port_no, &key);
232 if (unlikely(error)) {
238 flow = ovs_flow_tbl_lookup(rcu_dereference(dp->table), &key);
239 if (unlikely(!flow)) {
240 struct dp_upcall_info upcall;
242 upcall.cmd = OVS_PACKET_CMD_MISS;
244 upcall.userdata = NULL;
245 upcall.portid = p->upcall_portid;
246 ovs_dp_upcall(dp, skb, &upcall);
248 stats_counter = &stats->n_missed;
252 OVS_CB(skb)->flow = flow;
253 OVS_CB(skb)->pkt_key = &key;
255 stats_counter = &stats->n_hit;
256 ovs_flow_used(OVS_CB(skb)->flow, skb);
257 ovs_execute_actions(dp, skb);
260 /* Update datapath statistics. */
261 u64_stats_update_begin(&stats->sync);
263 u64_stats_update_end(&stats->sync);
266 static struct genl_family dp_packet_genl_family = {
267 .id = GENL_ID_GENERATE,
268 .hdrsize = sizeof(struct ovs_header),
269 .name = OVS_PACKET_FAMILY,
270 .version = OVS_PACKET_VERSION,
271 .maxattr = OVS_PACKET_ATTR_MAX,
273 .parallel_ops = true,
276 int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
277 const struct dp_upcall_info *upcall_info)
279 struct dp_stats_percpu *stats;
283 if (upcall_info->portid == 0) {
288 dp_ifindex = get_dpifindex(dp);
294 if (!skb_is_gso(skb))
295 err = queue_userspace_packet(ovs_dp_get_net(dp), dp_ifindex, skb, upcall_info);
297 err = queue_gso_packets(ovs_dp_get_net(dp), dp_ifindex, skb, upcall_info);
304 stats = this_cpu_ptr(dp->stats_percpu);
306 u64_stats_update_begin(&stats->sync);
308 u64_stats_update_end(&stats->sync);
313 static int queue_gso_packets(struct net *net, int dp_ifindex,
315 const struct dp_upcall_info *upcall_info)
317 unsigned short gso_type = skb_shinfo(skb)->gso_type;
318 struct dp_upcall_info later_info;
319 struct sw_flow_key later_key;
320 struct sk_buff *segs, *nskb;
323 segs = __skb_gso_segment(skb, NETIF_F_SG | NETIF_F_HW_CSUM, false);
325 return PTR_ERR(segs);
327 /* Queue all of the segments. */
330 err = queue_userspace_packet(net, dp_ifindex, skb, upcall_info);
334 if (skb == segs && gso_type & SKB_GSO_UDP) {
335 /* The initial flow key extracted by ovs_flow_extract()
336 * in this case is for a first fragment, so we need to
337 * properly mark later fragments.
339 later_key = *upcall_info->key;
340 later_key.ip.frag = OVS_FRAG_TYPE_LATER;
342 later_info = *upcall_info;
343 later_info.key = &later_key;
344 upcall_info = &later_info;
346 } while ((skb = skb->next));
348 /* Free all of the segments. */
356 } while ((skb = nskb));
360 static size_t key_attr_size(void)
362 return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */
363 + nla_total_size(0) /* OVS_KEY_ATTR_TUNNEL */
364 + nla_total_size(8) /* OVS_TUNNEL_KEY_ATTR_ID */
365 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_SRC */
366 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_DST */
367 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TOS */
368 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TTL */
369 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT */
370 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_CSUM */
371 + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */
372 + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
373 + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
374 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
375 + nla_total_size(4) /* OVS_KEY_ATTR_8021Q */
376 + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
377 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
378 + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
379 + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
380 + nla_total_size(28); /* OVS_KEY_ATTR_ND */
383 static size_t upcall_msg_size(const struct sk_buff *skb,
384 const struct nlattr *userdata)
386 size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
387 + nla_total_size(skb->len) /* OVS_PACKET_ATTR_PACKET */
388 + nla_total_size(key_attr_size()); /* OVS_PACKET_ATTR_KEY */
390 /* OVS_PACKET_ATTR_USERDATA */
392 size += NLA_ALIGN(userdata->nla_len);
397 static int queue_userspace_packet(struct net *net, int dp_ifindex,
399 const struct dp_upcall_info *upcall_info)
401 struct ovs_header *upcall;
402 struct sk_buff *nskb = NULL;
403 struct sk_buff *user_skb; /* to be queued to userspace */
407 if (vlan_tx_tag_present(skb)) {
408 nskb = skb_clone(skb, GFP_ATOMIC);
412 nskb = __vlan_put_tag(nskb, nskb->vlan_proto, vlan_tx_tag_get(nskb));
420 if (nla_attr_size(skb->len) > USHRT_MAX) {
425 user_skb = genlmsg_new(upcall_msg_size(skb, upcall_info->userdata), GFP_ATOMIC);
431 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
432 0, upcall_info->cmd);
433 upcall->dp_ifindex = dp_ifindex;
435 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
436 ovs_nla_put_flow(upcall_info->key, upcall_info->key, user_skb);
437 nla_nest_end(user_skb, nla);
439 if (upcall_info->userdata)
440 __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
441 nla_len(upcall_info->userdata),
442 nla_data(upcall_info->userdata));
444 nla = __nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, skb->len);
446 skb_copy_and_csum_dev(skb, nla_data(nla));
448 genlmsg_end(user_skb, upcall);
449 err = genlmsg_unicast(net, user_skb, upcall_info->portid);
456 /* Called with ovs_mutex. */
457 static int flush_flows(struct datapath *dp)
459 struct flow_table *old_table;
460 struct flow_table *new_table;
462 old_table = ovsl_dereference(dp->table);
463 new_table = ovs_flow_tbl_alloc(TBL_MIN_BUCKETS);
467 rcu_assign_pointer(dp->table, new_table);
469 ovs_flow_tbl_destroy(old_table, true);
473 static void clear_stats(struct sw_flow *flow)
477 flow->packet_count = 0;
478 flow->byte_count = 0;
481 static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
483 struct ovs_header *ovs_header = info->userhdr;
484 struct nlattr **a = info->attrs;
485 struct sw_flow_actions *acts;
486 struct sk_buff *packet;
487 struct sw_flow *flow;
494 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
495 !a[OVS_PACKET_ATTR_ACTIONS])
498 len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
499 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
503 skb_reserve(packet, NET_IP_ALIGN);
505 nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
507 skb_reset_mac_header(packet);
508 eth = eth_hdr(packet);
510 /* Normally, setting the skb 'protocol' field would be handled by a
511 * call to eth_type_trans(), but it assumes there's a sending
512 * device, which we may not have. */
513 if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN)
514 packet->protocol = eth->h_proto;
516 packet->protocol = htons(ETH_P_802_2);
518 /* Build an sw_flow for sending this packet. */
519 flow = ovs_flow_alloc();
524 err = ovs_flow_extract(packet, -1, &flow->key);
528 err = ovs_nla_get_flow_metadata(flow, a[OVS_PACKET_ATTR_KEY]);
531 acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_PACKET_ATTR_ACTIONS]));
536 err = ovs_nla_copy_actions(a[OVS_PACKET_ATTR_ACTIONS],
537 &flow->key, 0, &acts);
538 rcu_assign_pointer(flow->sf_acts, acts);
542 OVS_CB(packet)->flow = flow;
543 OVS_CB(packet)->pkt_key = &flow->key;
544 packet->priority = flow->key.phy.priority;
545 packet->mark = flow->key.phy.skb_mark;
548 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
554 err = ovs_execute_actions(dp, packet);
558 ovs_flow_free(flow, false);
564 ovs_flow_free(flow, false);
571 static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
572 [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
573 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
574 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
577 static struct genl_ops dp_packet_genl_ops[] = {
578 { .cmd = OVS_PACKET_CMD_EXECUTE,
579 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
580 .policy = packet_policy,
581 .doit = ovs_packet_cmd_execute
585 static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats)
587 struct flow_table *table;
590 table = rcu_dereference_check(dp->table, lockdep_ovsl_is_held());
591 stats->n_flows = ovs_flow_tbl_count(table);
593 stats->n_hit = stats->n_missed = stats->n_lost = 0;
594 for_each_possible_cpu(i) {
595 const struct dp_stats_percpu *percpu_stats;
596 struct dp_stats_percpu local_stats;
599 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
602 start = u64_stats_fetch_begin_bh(&percpu_stats->sync);
603 local_stats = *percpu_stats;
604 } while (u64_stats_fetch_retry_bh(&percpu_stats->sync, start));
606 stats->n_hit += local_stats.n_hit;
607 stats->n_missed += local_stats.n_missed;
608 stats->n_lost += local_stats.n_lost;
612 static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
613 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
614 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
615 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
618 static struct genl_family dp_flow_genl_family = {
619 .id = GENL_ID_GENERATE,
620 .hdrsize = sizeof(struct ovs_header),
621 .name = OVS_FLOW_FAMILY,
622 .version = OVS_FLOW_VERSION,
623 .maxattr = OVS_FLOW_ATTR_MAX,
625 .parallel_ops = true,
628 static struct genl_multicast_group ovs_dp_flow_multicast_group = {
629 .name = OVS_FLOW_MCGROUP
632 static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts)
634 return NLMSG_ALIGN(sizeof(struct ovs_header))
635 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_KEY */
636 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_MASK */
637 + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
638 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
639 + nla_total_size(8) /* OVS_FLOW_ATTR_USED */
640 + nla_total_size(acts->actions_len); /* OVS_FLOW_ATTR_ACTIONS */
643 /* Called with ovs_mutex. */
644 static int ovs_flow_cmd_fill_info(struct sw_flow *flow, struct datapath *dp,
645 struct sk_buff *skb, u32 portid,
646 u32 seq, u32 flags, u8 cmd)
648 const int skb_orig_len = skb->len;
649 struct nlattr *start;
650 struct ovs_flow_stats stats;
651 struct ovs_header *ovs_header;
657 ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd);
661 ovs_header->dp_ifindex = get_dpifindex(dp);
664 nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
666 goto nla_put_failure;
668 err = ovs_nla_put_flow(&flow->unmasked_key, &flow->unmasked_key, skb);
671 nla_nest_end(skb, nla);
673 nla = nla_nest_start(skb, OVS_FLOW_ATTR_MASK);
675 goto nla_put_failure;
677 err = ovs_nla_put_flow(&flow->key, &flow->mask->key, skb);
681 nla_nest_end(skb, nla);
683 spin_lock_bh(&flow->lock);
685 stats.n_packets = flow->packet_count;
686 stats.n_bytes = flow->byte_count;
687 tcp_flags = flow->tcp_flags;
688 spin_unlock_bh(&flow->lock);
691 nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
692 goto nla_put_failure;
694 if (stats.n_packets &&
695 nla_put(skb, OVS_FLOW_ATTR_STATS,
696 sizeof(struct ovs_flow_stats), &stats))
697 goto nla_put_failure;
700 nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, tcp_flags))
701 goto nla_put_failure;
703 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
704 * this is the first flow to be dumped into 'skb'. This is unusual for
705 * Netlink but individual action lists can be longer than
706 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
707 * The userspace caller can always fetch the actions separately if it
708 * really wants them. (Most userspace callers in fact don't care.)
710 * This can only fail for dump operations because the skb is always
711 * properly sized for single flows.
713 start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
715 const struct sw_flow_actions *sf_acts;
717 sf_acts = rcu_dereference_check(flow->sf_acts,
718 lockdep_ovsl_is_held());
720 err = ovs_nla_put_actions(sf_acts->actions,
721 sf_acts->actions_len, skb);
723 nla_nest_end(skb, start);
728 nla_nest_cancel(skb, start);
730 } else if (skb_orig_len)
731 goto nla_put_failure;
733 return genlmsg_end(skb, ovs_header);
738 genlmsg_cancel(skb, ovs_header);
742 static struct sk_buff *ovs_flow_cmd_alloc_info(struct sw_flow *flow)
744 const struct sw_flow_actions *sf_acts;
746 sf_acts = ovsl_dereference(flow->sf_acts);
748 return genlmsg_new(ovs_flow_cmd_msg_size(sf_acts), GFP_KERNEL);
751 static struct sk_buff *ovs_flow_cmd_build_info(struct sw_flow *flow,
753 u32 portid, u32 seq, u8 cmd)
758 skb = ovs_flow_cmd_alloc_info(flow);
760 return ERR_PTR(-ENOMEM);
762 retval = ovs_flow_cmd_fill_info(flow, dp, skb, portid, seq, 0, cmd);
767 static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info)
769 struct nlattr **a = info->attrs;
770 struct ovs_header *ovs_header = info->userhdr;
771 struct sw_flow_key key, masked_key;
772 struct sw_flow *flow = NULL;
773 struct sw_flow_mask mask;
774 struct sk_buff *reply;
776 struct flow_table *table;
777 struct sw_flow_actions *acts = NULL;
778 struct sw_flow_match match;
783 if (!a[OVS_FLOW_ATTR_KEY])
786 ovs_match_init(&match, &key, &mask);
787 error = ovs_nla_get_match(&match,
788 a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
792 /* Validate actions. */
793 if (a[OVS_FLOW_ATTR_ACTIONS]) {
794 acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_FLOW_ATTR_ACTIONS]));
795 error = PTR_ERR(acts);
799 ovs_flow_mask_key(&masked_key, &key, &mask);
800 error = ovs_nla_copy_actions(a[OVS_FLOW_ATTR_ACTIONS],
801 &masked_key, 0, &acts);
803 OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
806 } else if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW) {
812 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
817 table = ovsl_dereference(dp->table);
819 /* Check if this is a duplicate flow */
820 flow = ovs_flow_tbl_lookup(table, &key);
822 struct flow_table *new_table = NULL;
823 struct sw_flow_mask *mask_p;
825 /* Bail out if we're not allowed to create a new flow. */
827 if (info->genlhdr->cmd == OVS_FLOW_CMD_SET)
830 /* Expand table, if necessary, to make room. */
831 if (ovs_flow_tbl_need_to_expand(table))
832 new_table = ovs_flow_tbl_expand(table);
833 else if (time_after(jiffies, dp->last_rehash + REHASH_FLOW_INTERVAL))
834 new_table = ovs_flow_tbl_rehash(table);
836 if (new_table && !IS_ERR(new_table)) {
837 rcu_assign_pointer(dp->table, new_table);
838 ovs_flow_tbl_destroy(table, true);
839 table = ovsl_dereference(dp->table);
840 dp->last_rehash = jiffies;
844 flow = ovs_flow_alloc();
846 error = PTR_ERR(flow);
851 flow->key = masked_key;
852 flow->unmasked_key = key;
854 /* Make sure mask is unique in the system */
855 mask_p = ovs_sw_flow_mask_find(table, &mask);
857 /* Allocate a new mask if none exsits. */
858 mask_p = ovs_sw_flow_mask_alloc();
861 mask_p->key = mask.key;
862 mask_p->range = mask.range;
863 ovs_sw_flow_mask_insert(table, mask_p);
866 ovs_sw_flow_mask_add_ref(mask_p);
868 rcu_assign_pointer(flow->sf_acts, acts);
870 /* Put flow in bucket. */
871 ovs_flow_tbl_insert(table, flow);
873 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
874 info->snd_seq, OVS_FLOW_CMD_NEW);
876 /* We found a matching flow. */
877 struct sw_flow_actions *old_acts;
879 /* Bail out if we're not allowed to modify an existing flow.
880 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
881 * because Generic Netlink treats the latter as a dump
882 * request. We also accept NLM_F_EXCL in case that bug ever
886 if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW &&
887 info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL))
890 /* The unmasked key has to be the same for flow updates. */
892 if (!ovs_flow_cmp_unmasked_key(flow, &match)) {
893 OVS_NLERR("Flow modification message rejected, unmasked key does not match.\n");
897 /* Update actions. */
898 old_acts = ovsl_dereference(flow->sf_acts);
899 rcu_assign_pointer(flow->sf_acts, acts);
900 ovs_nla_free_flow_actions(old_acts);
902 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
903 info->snd_seq, OVS_FLOW_CMD_NEW);
906 if (a[OVS_FLOW_ATTR_CLEAR]) {
907 spin_lock_bh(&flow->lock);
909 spin_unlock_bh(&flow->lock);
915 ovs_notify(reply, info, &ovs_dp_flow_multicast_group);
917 netlink_set_err(sock_net(skb->sk)->genl_sock, 0,
918 ovs_dp_flow_multicast_group.id, PTR_ERR(reply));
922 ovs_flow_free(flow, false);
931 static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
933 struct nlattr **a = info->attrs;
934 struct ovs_header *ovs_header = info->userhdr;
935 struct sw_flow_key key;
936 struct sk_buff *reply;
937 struct sw_flow *flow;
939 struct flow_table *table;
940 struct sw_flow_match match;
943 if (!a[OVS_FLOW_ATTR_KEY]) {
944 OVS_NLERR("Flow get message rejected, Key attribute missing.\n");
948 ovs_match_init(&match, &key, NULL);
949 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
954 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
960 table = ovsl_dereference(dp->table);
961 flow = ovs_flow_tbl_lookup(table, &key);
962 if (!flow || !ovs_flow_cmp_unmasked_key(flow, &match)) {
967 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
968 info->snd_seq, OVS_FLOW_CMD_NEW);
970 err = PTR_ERR(reply);
975 return genlmsg_reply(reply, info);
981 static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
983 struct nlattr **a = info->attrs;
984 struct ovs_header *ovs_header = info->userhdr;
985 struct sw_flow_key key;
986 struct sk_buff *reply;
987 struct sw_flow *flow;
989 struct flow_table *table;
990 struct sw_flow_match match;
994 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1000 if (!a[OVS_FLOW_ATTR_KEY]) {
1001 err = flush_flows(dp);
1005 ovs_match_init(&match, &key, NULL);
1006 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
1010 table = ovsl_dereference(dp->table);
1011 flow = ovs_flow_tbl_lookup(table, &key);
1012 if (!flow || !ovs_flow_cmp_unmasked_key(flow, &match)) {
1017 reply = ovs_flow_cmd_alloc_info(flow);
1023 ovs_flow_tbl_remove(table, flow);
1025 err = ovs_flow_cmd_fill_info(flow, dp, reply, info->snd_portid,
1026 info->snd_seq, 0, OVS_FLOW_CMD_DEL);
1029 ovs_flow_free(flow, true);
1032 ovs_notify(reply, info, &ovs_dp_flow_multicast_group);
1039 static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1041 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1042 struct datapath *dp;
1043 struct flow_table *table;
1046 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1052 table = rcu_dereference(dp->table);
1054 struct sw_flow *flow;
1057 bucket = cb->args[0];
1059 flow = ovs_flow_tbl_dump_next(table, &bucket, &obj);
1063 if (ovs_flow_cmd_fill_info(flow, dp, skb,
1064 NETLINK_CB(cb->skb).portid,
1065 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1066 OVS_FLOW_CMD_NEW) < 0)
1069 cb->args[0] = bucket;
1076 static struct genl_ops dp_flow_genl_ops[] = {
1077 { .cmd = OVS_FLOW_CMD_NEW,
1078 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1079 .policy = flow_policy,
1080 .doit = ovs_flow_cmd_new_or_set
1082 { .cmd = OVS_FLOW_CMD_DEL,
1083 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1084 .policy = flow_policy,
1085 .doit = ovs_flow_cmd_del
1087 { .cmd = OVS_FLOW_CMD_GET,
1088 .flags = 0, /* OK for unprivileged users. */
1089 .policy = flow_policy,
1090 .doit = ovs_flow_cmd_get,
1091 .dumpit = ovs_flow_cmd_dump
1093 { .cmd = OVS_FLOW_CMD_SET,
1094 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1095 .policy = flow_policy,
1096 .doit = ovs_flow_cmd_new_or_set,
1100 static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1101 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1102 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1105 static struct genl_family dp_datapath_genl_family = {
1106 .id = GENL_ID_GENERATE,
1107 .hdrsize = sizeof(struct ovs_header),
1108 .name = OVS_DATAPATH_FAMILY,
1109 .version = OVS_DATAPATH_VERSION,
1110 .maxattr = OVS_DP_ATTR_MAX,
1112 .parallel_ops = true,
1115 static struct genl_multicast_group ovs_dp_datapath_multicast_group = {
1116 .name = OVS_DATAPATH_MCGROUP
1119 static size_t ovs_dp_cmd_msg_size(void)
1121 size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
1123 msgsize += nla_total_size(IFNAMSIZ);
1124 msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
1129 static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
1130 u32 portid, u32 seq, u32 flags, u8 cmd)
1132 struct ovs_header *ovs_header;
1133 struct ovs_dp_stats dp_stats;
1136 ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
1141 ovs_header->dp_ifindex = get_dpifindex(dp);
1144 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
1147 goto nla_put_failure;
1149 get_dp_stats(dp, &dp_stats);
1150 if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats), &dp_stats))
1151 goto nla_put_failure;
1153 return genlmsg_end(skb, ovs_header);
1156 genlmsg_cancel(skb, ovs_header);
1161 static struct sk_buff *ovs_dp_cmd_build_info(struct datapath *dp, u32 portid,
1164 struct sk_buff *skb;
1167 skb = genlmsg_new(ovs_dp_cmd_msg_size(), GFP_KERNEL);
1169 return ERR_PTR(-ENOMEM);
1171 retval = ovs_dp_cmd_fill_info(dp, skb, portid, seq, 0, cmd);
1174 return ERR_PTR(retval);
1179 /* Called with ovs_mutex. */
1180 static struct datapath *lookup_datapath(struct net *net,
1181 struct ovs_header *ovs_header,
1182 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1184 struct datapath *dp;
1186 if (!a[OVS_DP_ATTR_NAME])
1187 dp = get_dp(net, ovs_header->dp_ifindex);
1189 struct vport *vport;
1192 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
1193 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
1196 return dp ? dp : ERR_PTR(-ENODEV);
1199 static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1201 struct nlattr **a = info->attrs;
1202 struct vport_parms parms;
1203 struct sk_buff *reply;
1204 struct datapath *dp;
1205 struct vport *vport;
1206 struct ovs_net *ovs_net;
1210 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1216 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1218 goto err_unlock_ovs;
1220 ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
1222 /* Allocate table. */
1224 rcu_assign_pointer(dp->table, ovs_flow_tbl_alloc(TBL_MIN_BUCKETS));
1228 dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
1229 if (!dp->stats_percpu) {
1231 goto err_destroy_table;
1234 dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
1238 goto err_destroy_percpu;
1241 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1242 INIT_HLIST_HEAD(&dp->ports[i]);
1244 /* Set up our datapath device. */
1245 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1246 parms.type = OVS_VPORT_TYPE_INTERNAL;
1247 parms.options = NULL;
1249 parms.port_no = OVSP_LOCAL;
1250 parms.upcall_portid = nla_get_u32(a[OVS_DP_ATTR_UPCALL_PID]);
1252 vport = new_vport(&parms);
1253 if (IS_ERR(vport)) {
1254 err = PTR_ERR(vport);
1258 goto err_destroy_ports_array;
1261 reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
1262 info->snd_seq, OVS_DP_CMD_NEW);
1263 err = PTR_ERR(reply);
1265 goto err_destroy_local_port;
1267 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
1268 list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
1272 ovs_notify(reply, info, &ovs_dp_datapath_multicast_group);
1275 err_destroy_local_port:
1276 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
1277 err_destroy_ports_array:
1280 free_percpu(dp->stats_percpu);
1282 ovs_flow_tbl_destroy(ovsl_dereference(dp->table), false);
1284 release_net(ovs_dp_get_net(dp));
1292 /* Called with ovs_mutex. */
1293 static void __dp_destroy(struct datapath *dp)
1297 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1298 struct vport *vport;
1299 struct hlist_node *n;
1301 hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
1302 if (vport->port_no != OVSP_LOCAL)
1303 ovs_dp_detach_port(vport);
1306 list_del_rcu(&dp->list_node);
1308 /* OVSP_LOCAL is datapath internal port. We need to make sure that
1309 * all port in datapath are destroyed first before freeing datapath.
1311 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
1313 call_rcu(&dp->rcu, destroy_dp_rcu);
1316 static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1318 struct sk_buff *reply;
1319 struct datapath *dp;
1323 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1328 reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
1329 info->snd_seq, OVS_DP_CMD_DEL);
1330 err = PTR_ERR(reply);
1337 ovs_notify(reply, info, &ovs_dp_datapath_multicast_group);
1345 static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1347 struct sk_buff *reply;
1348 struct datapath *dp;
1352 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1357 reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
1358 info->snd_seq, OVS_DP_CMD_NEW);
1359 if (IS_ERR(reply)) {
1360 err = PTR_ERR(reply);
1361 netlink_set_err(sock_net(skb->sk)->genl_sock, 0,
1362 ovs_dp_datapath_multicast_group.id, err);
1368 ovs_notify(reply, info, &ovs_dp_datapath_multicast_group);
1376 static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1378 struct sk_buff *reply;
1379 struct datapath *dp;
1383 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1389 reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
1390 info->snd_seq, OVS_DP_CMD_NEW);
1391 if (IS_ERR(reply)) {
1392 err = PTR_ERR(reply);
1397 return genlmsg_reply(reply, info);
1404 static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1406 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
1407 struct datapath *dp;
1408 int skip = cb->args[0];
1412 list_for_each_entry_rcu(dp, &ovs_net->dps, list_node) {
1414 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
1415 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1416 OVS_DP_CMD_NEW) < 0)
1427 static struct genl_ops dp_datapath_genl_ops[] = {
1428 { .cmd = OVS_DP_CMD_NEW,
1429 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1430 .policy = datapath_policy,
1431 .doit = ovs_dp_cmd_new
1433 { .cmd = OVS_DP_CMD_DEL,
1434 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1435 .policy = datapath_policy,
1436 .doit = ovs_dp_cmd_del
1438 { .cmd = OVS_DP_CMD_GET,
1439 .flags = 0, /* OK for unprivileged users. */
1440 .policy = datapath_policy,
1441 .doit = ovs_dp_cmd_get,
1442 .dumpit = ovs_dp_cmd_dump
1444 { .cmd = OVS_DP_CMD_SET,
1445 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1446 .policy = datapath_policy,
1447 .doit = ovs_dp_cmd_set,
1451 static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
1452 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1453 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
1454 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
1455 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
1456 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1457 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
1460 static struct genl_family dp_vport_genl_family = {
1461 .id = GENL_ID_GENERATE,
1462 .hdrsize = sizeof(struct ovs_header),
1463 .name = OVS_VPORT_FAMILY,
1464 .version = OVS_VPORT_VERSION,
1465 .maxattr = OVS_VPORT_ATTR_MAX,
1467 .parallel_ops = true,
1470 struct genl_multicast_group ovs_dp_vport_multicast_group = {
1471 .name = OVS_VPORT_MCGROUP
1474 /* Called with ovs_mutex or RCU read lock. */
1475 static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
1476 u32 portid, u32 seq, u32 flags, u8 cmd)
1478 struct ovs_header *ovs_header;
1479 struct ovs_vport_stats vport_stats;
1482 ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
1487 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1489 if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1490 nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
1491 nla_put_string(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport)) ||
1492 nla_put_u32(skb, OVS_VPORT_ATTR_UPCALL_PID, vport->upcall_portid))
1493 goto nla_put_failure;
1495 ovs_vport_get_stats(vport, &vport_stats);
1496 if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
1498 goto nla_put_failure;
1500 err = ovs_vport_get_options(vport, skb);
1501 if (err == -EMSGSIZE)
1504 return genlmsg_end(skb, ovs_header);
1509 genlmsg_cancel(skb, ovs_header);
1513 /* Called with ovs_mutex or RCU read lock. */
1514 struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
1517 struct sk_buff *skb;
1520 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1522 return ERR_PTR(-ENOMEM);
1524 retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
1530 /* Called with ovs_mutex or RCU read lock. */
1531 static struct vport *lookup_vport(struct net *net,
1532 struct ovs_header *ovs_header,
1533 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1535 struct datapath *dp;
1536 struct vport *vport;
1538 if (a[OVS_VPORT_ATTR_NAME]) {
1539 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
1541 return ERR_PTR(-ENODEV);
1542 if (ovs_header->dp_ifindex &&
1543 ovs_header->dp_ifindex != get_dpifindex(vport->dp))
1544 return ERR_PTR(-ENODEV);
1546 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1547 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1549 if (port_no >= DP_MAX_PORTS)
1550 return ERR_PTR(-EFBIG);
1552 dp = get_dp(net, ovs_header->dp_ifindex);
1554 return ERR_PTR(-ENODEV);
1556 vport = ovs_vport_ovsl_rcu(dp, port_no);
1558 return ERR_PTR(-ENODEV);
1561 return ERR_PTR(-EINVAL);
1564 static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1566 struct nlattr **a = info->attrs;
1567 struct ovs_header *ovs_header = info->userhdr;
1568 struct vport_parms parms;
1569 struct sk_buff *reply;
1570 struct vport *vport;
1571 struct datapath *dp;
1576 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
1577 !a[OVS_VPORT_ATTR_UPCALL_PID])
1581 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1586 if (a[OVS_VPORT_ATTR_PORT_NO]) {
1587 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1590 if (port_no >= DP_MAX_PORTS)
1593 vport = ovs_vport_ovsl(dp, port_no);
1598 for (port_no = 1; ; port_no++) {
1599 if (port_no >= DP_MAX_PORTS) {
1603 vport = ovs_vport_ovsl(dp, port_no);
1609 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
1610 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1611 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
1613 parms.port_no = port_no;
1614 parms.upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
1616 vport = new_vport(&parms);
1617 err = PTR_ERR(vport);
1622 reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq,
1624 if (IS_ERR(reply)) {
1625 err = PTR_ERR(reply);
1626 ovs_dp_detach_port(vport);
1630 ovs_notify(reply, info, &ovs_dp_vport_multicast_group);
1638 static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
1640 struct nlattr **a = info->attrs;
1641 struct sk_buff *reply;
1642 struct vport *vport;
1646 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
1647 err = PTR_ERR(vport);
1651 if (a[OVS_VPORT_ATTR_TYPE] &&
1652 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
1657 reply = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1663 if (a[OVS_VPORT_ATTR_OPTIONS]) {
1664 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
1669 if (a[OVS_VPORT_ATTR_UPCALL_PID])
1670 vport->upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
1672 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1673 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1677 ovs_notify(reply, info, &ovs_dp_vport_multicast_group);
1687 static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
1689 struct nlattr **a = info->attrs;
1690 struct sk_buff *reply;
1691 struct vport *vport;
1695 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
1696 err = PTR_ERR(vport);
1700 if (vport->port_no == OVSP_LOCAL) {
1705 reply = ovs_vport_cmd_build_info(vport, info->snd_portid,
1706 info->snd_seq, OVS_VPORT_CMD_DEL);
1707 err = PTR_ERR(reply);
1712 ovs_dp_detach_port(vport);
1714 ovs_notify(reply, info, &ovs_dp_vport_multicast_group);
1721 static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
1723 struct nlattr **a = info->attrs;
1724 struct ovs_header *ovs_header = info->userhdr;
1725 struct sk_buff *reply;
1726 struct vport *vport;
1730 vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
1731 err = PTR_ERR(vport);
1735 reply = ovs_vport_cmd_build_info(vport, info->snd_portid,
1736 info->snd_seq, OVS_VPORT_CMD_NEW);
1737 err = PTR_ERR(reply);
1743 return genlmsg_reply(reply, info);
1750 static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1752 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1753 struct datapath *dp;
1754 int bucket = cb->args[0], skip = cb->args[1];
1757 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1762 for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
1763 struct vport *vport;
1766 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
1768 ovs_vport_cmd_fill_info(vport, skb,
1769 NETLINK_CB(cb->skb).portid,
1772 OVS_VPORT_CMD_NEW) < 0)
1788 static struct genl_ops dp_vport_genl_ops[] = {
1789 { .cmd = OVS_VPORT_CMD_NEW,
1790 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1791 .policy = vport_policy,
1792 .doit = ovs_vport_cmd_new
1794 { .cmd = OVS_VPORT_CMD_DEL,
1795 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1796 .policy = vport_policy,
1797 .doit = ovs_vport_cmd_del
1799 { .cmd = OVS_VPORT_CMD_GET,
1800 .flags = 0, /* OK for unprivileged users. */
1801 .policy = vport_policy,
1802 .doit = ovs_vport_cmd_get,
1803 .dumpit = ovs_vport_cmd_dump
1805 { .cmd = OVS_VPORT_CMD_SET,
1806 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1807 .policy = vport_policy,
1808 .doit = ovs_vport_cmd_set,
1812 struct genl_family_and_ops {
1813 struct genl_family *family;
1814 struct genl_ops *ops;
1816 struct genl_multicast_group *group;
1819 static const struct genl_family_and_ops dp_genl_families[] = {
1820 { &dp_datapath_genl_family,
1821 dp_datapath_genl_ops, ARRAY_SIZE(dp_datapath_genl_ops),
1822 &ovs_dp_datapath_multicast_group },
1823 { &dp_vport_genl_family,
1824 dp_vport_genl_ops, ARRAY_SIZE(dp_vport_genl_ops),
1825 &ovs_dp_vport_multicast_group },
1826 { &dp_flow_genl_family,
1827 dp_flow_genl_ops, ARRAY_SIZE(dp_flow_genl_ops),
1828 &ovs_dp_flow_multicast_group },
1829 { &dp_packet_genl_family,
1830 dp_packet_genl_ops, ARRAY_SIZE(dp_packet_genl_ops),
1834 static void dp_unregister_genl(int n_families)
1838 for (i = 0; i < n_families; i++)
1839 genl_unregister_family(dp_genl_families[i].family);
1842 static int dp_register_genl(void)
1849 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
1850 const struct genl_family_and_ops *f = &dp_genl_families[i];
1852 err = genl_register_family_with_ops(f->family, f->ops,
1859 err = genl_register_mc_group(f->family, f->group);
1868 dp_unregister_genl(n_registered);
1872 static int __net_init ovs_init_net(struct net *net)
1874 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
1876 INIT_LIST_HEAD(&ovs_net->dps);
1877 INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
1881 static void __net_exit ovs_exit_net(struct net *net)
1883 struct datapath *dp, *dp_next;
1884 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
1887 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
1891 cancel_work_sync(&ovs_net->dp_notify_work);
1894 static struct pernet_operations ovs_net_ops = {
1895 .init = ovs_init_net,
1896 .exit = ovs_exit_net,
1898 .size = sizeof(struct ovs_net),
1901 static int __init dp_init(void)
1905 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
1907 pr_info("Open vSwitch switching datapath\n");
1909 err = ovs_flow_init();
1913 err = ovs_vport_init();
1915 goto error_flow_exit;
1917 err = register_pernet_device(&ovs_net_ops);
1919 goto error_vport_exit;
1921 err = register_netdevice_notifier(&ovs_dp_device_notifier);
1923 goto error_netns_exit;
1925 err = dp_register_genl();
1927 goto error_unreg_notifier;
1931 error_unreg_notifier:
1932 unregister_netdevice_notifier(&ovs_dp_device_notifier);
1934 unregister_pernet_device(&ovs_net_ops);
1943 static void dp_cleanup(void)
1945 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
1946 unregister_netdevice_notifier(&ovs_dp_device_notifier);
1947 unregister_pernet_device(&ovs_net_ops);
1953 module_init(dp_init);
1954 module_exit(dp_cleanup);
1956 MODULE_DESCRIPTION("Open vSwitch switching datapath");
1957 MODULE_LICENSE("GPL");