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/version.h>
40 #include <linux/ethtool.h>
41 #include <linux/wait.h>
42 #include <asm/div64.h>
43 #include <linux/highmem.h>
44 #include <linux/netfilter_bridge.h>
45 #include <linux/netfilter_ipv4.h>
46 #include <linux/inetdevice.h>
47 #include <linux/list.h>
48 #include <linux/openvswitch.h>
49 #include <linux/rculist.h>
50 #include <linux/dmi.h>
51 #include <linux/genetlink.h>
52 #include <net/genetlink.h>
53 #include <net/genetlink.h>
54 #include <net/net_namespace.h>
55 #include <net/netns/generic.h>
59 #include "flow_netlink.h"
61 #include "vport-internal_dev.h"
62 #include "vport-netdev.h"
64 #define REHASH_FLOW_INTERVAL (10 * 60 * HZ)
66 int ovs_net_id __read_mostly;
68 static void ovs_notify(struct sk_buff *skb, struct genl_info *info,
69 struct genl_multicast_group *grp)
71 genl_notify(skb, genl_info_net(info), info->snd_portid,
72 grp->id, info->nlhdr, GFP_KERNEL);
78 * All writes e.g. Writes to device state (add/remove datapath, port, set
79 * operations on vports, etc.), Writes to other state (flow table
80 * modifications, set miscellaneous datapath parameters, etc.) are protected
83 * Reads are protected by RCU.
85 * There are a few special cases (mostly stats) that have their own
86 * synchronization but they nest under all of above and don't interact with
89 * The RTNL lock nests inside ovs_mutex.
92 static DEFINE_MUTEX(ovs_mutex);
96 mutex_lock(&ovs_mutex);
101 mutex_unlock(&ovs_mutex);
104 #ifdef CONFIG_LOCKDEP
105 int lockdep_ovsl_is_held(void)
108 return lockdep_is_held(&ovs_mutex);
114 static struct vport *new_vport(const struct vport_parms *);
115 static int queue_gso_packets(struct net *, int dp_ifindex, struct sk_buff *,
116 const struct dp_upcall_info *);
117 static int queue_userspace_packet(struct net *, int dp_ifindex,
119 const struct dp_upcall_info *);
121 /* Must be called with rcu_read_lock or ovs_mutex. */
122 static struct datapath *get_dp(struct net *net, int dp_ifindex)
124 struct datapath *dp = NULL;
125 struct net_device *dev;
128 dev = dev_get_by_index_rcu(net, dp_ifindex);
130 struct vport *vport = ovs_internal_dev_get_vport(dev);
139 /* Must be called with rcu_read_lock or ovs_mutex. */
140 const char *ovs_dp_name(const struct datapath *dp)
142 struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL);
143 return vport->ops->get_name(vport);
146 static int get_dpifindex(struct datapath *dp)
153 local = ovs_vport_rcu(dp, OVSP_LOCAL);
155 ifindex = netdev_vport_priv(local)->dev->ifindex;
164 static void destroy_dp_rcu(struct rcu_head *rcu)
166 struct datapath *dp = container_of(rcu, struct datapath, rcu);
168 ovs_flow_tbl_destroy((__force struct flow_table *)dp->table, false);
169 free_percpu(dp->stats_percpu);
170 release_net(ovs_dp_get_net(dp));
175 static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
178 return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
181 struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
184 struct hlist_head *head;
186 head = vport_hash_bucket(dp, port_no);
187 hlist_for_each_entry_rcu(vport, head, dp_hash_node) {
188 if (vport->port_no == port_no)
194 /* Called with ovs_mutex. */
195 static struct vport *new_vport(const struct vport_parms *parms)
199 vport = ovs_vport_add(parms);
200 if (!IS_ERR(vport)) {
201 struct datapath *dp = parms->dp;
202 struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
204 hlist_add_head_rcu(&vport->dp_hash_node, head);
209 void ovs_dp_detach_port(struct vport *p)
213 /* First drop references to device. */
214 hlist_del_rcu(&p->dp_hash_node);
216 /* Then destroy it. */
220 /* Must be called with rcu_read_lock. */
221 void ovs_dp_process_received_packet(struct vport *p, struct sk_buff *skb)
223 struct datapath *dp = p->dp;
224 struct sw_flow *flow;
225 struct dp_stats_percpu *stats;
226 struct sw_flow_key key;
230 stats = this_cpu_ptr(dp->stats_percpu);
232 /* Extract flow from 'skb' into 'key'. */
233 error = ovs_flow_extract(skb, p->port_no, &key);
234 if (unlikely(error)) {
240 flow = ovs_flow_tbl_lookup(rcu_dereference(dp->table), &key);
241 if (unlikely(!flow)) {
242 struct dp_upcall_info upcall;
244 upcall.cmd = OVS_PACKET_CMD_MISS;
246 upcall.userdata = NULL;
247 upcall.portid = p->upcall_portid;
248 ovs_dp_upcall(dp, skb, &upcall);
250 stats_counter = &stats->n_missed;
254 OVS_CB(skb)->flow = flow;
255 OVS_CB(skb)->pkt_key = &key;
257 stats_counter = &stats->n_hit;
258 ovs_flow_used(OVS_CB(skb)->flow, skb);
259 ovs_execute_actions(dp, skb);
262 /* Update datapath statistics. */
263 u64_stats_update_begin(&stats->sync);
265 u64_stats_update_end(&stats->sync);
268 static struct genl_family dp_packet_genl_family = {
269 .id = GENL_ID_GENERATE,
270 .hdrsize = sizeof(struct ovs_header),
271 .name = OVS_PACKET_FAMILY,
272 .version = OVS_PACKET_VERSION,
273 .maxattr = OVS_PACKET_ATTR_MAX,
278 int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
279 const struct dp_upcall_info *upcall_info)
281 struct dp_stats_percpu *stats;
285 if (upcall_info->portid == 0) {
290 dp_ifindex = get_dpifindex(dp);
296 if (!skb_is_gso(skb))
297 err = queue_userspace_packet(ovs_dp_get_net(dp), dp_ifindex, skb, upcall_info);
299 err = queue_gso_packets(ovs_dp_get_net(dp), dp_ifindex, skb, upcall_info);
306 stats = this_cpu_ptr(dp->stats_percpu);
308 u64_stats_update_begin(&stats->sync);
310 u64_stats_update_end(&stats->sync);
315 static int queue_gso_packets(struct net *net, int dp_ifindex,
317 const struct dp_upcall_info *upcall_info)
319 unsigned short gso_type = skb_shinfo(skb)->gso_type;
320 struct dp_upcall_info later_info;
321 struct sw_flow_key later_key;
322 struct sk_buff *segs, *nskb;
325 segs = __skb_gso_segment(skb, NETIF_F_SG | NETIF_F_HW_CSUM, false);
327 return PTR_ERR(segs);
329 /* Queue all of the segments. */
332 err = queue_userspace_packet(net, dp_ifindex, skb, upcall_info);
336 if (skb == segs && gso_type & SKB_GSO_UDP) {
337 /* The initial flow key extracted by ovs_flow_extract()
338 * in this case is for a first fragment, so we need to
339 * properly mark later fragments.
341 later_key = *upcall_info->key;
342 later_key.ip.frag = OVS_FRAG_TYPE_LATER;
344 later_info = *upcall_info;
345 later_info.key = &later_key;
346 upcall_info = &later_info;
348 } while ((skb = skb->next));
350 /* Free all of the segments. */
358 } while ((skb = nskb));
362 static size_t key_attr_size(void)
364 return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */
365 + nla_total_size(0) /* OVS_KEY_ATTR_TUNNEL */
366 + nla_total_size(8) /* OVS_TUNNEL_KEY_ATTR_ID */
367 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_SRC */
368 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_DST */
369 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TOS */
370 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TTL */
371 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT */
372 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_CSUM */
373 + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */
374 + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
375 + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
376 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
377 + nla_total_size(4) /* OVS_KEY_ATTR_8021Q */
378 + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
379 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
380 + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
381 + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
382 + nla_total_size(28); /* OVS_KEY_ATTR_ND */
385 static size_t upcall_msg_size(const struct sk_buff *skb,
386 const struct nlattr *userdata)
388 size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
389 + nla_total_size(skb->len) /* OVS_PACKET_ATTR_PACKET */
390 + nla_total_size(key_attr_size()); /* OVS_PACKET_ATTR_KEY */
392 /* OVS_PACKET_ATTR_USERDATA */
394 size += NLA_ALIGN(userdata->nla_len);
399 static int queue_userspace_packet(struct net *net, int dp_ifindex,
401 const struct dp_upcall_info *upcall_info)
403 struct ovs_header *upcall;
404 struct sk_buff *nskb = NULL;
405 struct sk_buff *user_skb; /* to be queued to userspace */
409 if (vlan_tx_tag_present(skb)) {
410 nskb = skb_clone(skb, GFP_ATOMIC);
414 nskb = __vlan_put_tag(nskb, nskb->vlan_proto, vlan_tx_tag_get(nskb));
418 vlan_set_tci(nskb, 0);
423 if (nla_attr_size(skb->len) > USHRT_MAX) {
428 user_skb = genlmsg_new(upcall_msg_size(skb, upcall_info->userdata), GFP_ATOMIC);
434 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
435 0, upcall_info->cmd);
436 upcall->dp_ifindex = dp_ifindex;
438 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
439 ovs_nla_put_flow(upcall_info->key, upcall_info->key, user_skb);
440 nla_nest_end(user_skb, nla);
442 if (upcall_info->userdata)
443 __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
444 nla_len(upcall_info->userdata),
445 nla_data(upcall_info->userdata));
447 nla = __nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, skb->len);
449 skb_copy_and_csum_dev(skb, nla_data(nla));
451 genlmsg_end(user_skb, upcall);
452 err = genlmsg_unicast(net, user_skb, upcall_info->portid);
459 /* Called with ovs_mutex. */
460 static int flush_flows(struct datapath *dp)
462 struct flow_table *old_table;
463 struct flow_table *new_table;
465 old_table = ovsl_dereference(dp->table);
466 new_table = ovs_flow_tbl_alloc(TBL_MIN_BUCKETS);
470 rcu_assign_pointer(dp->table, new_table);
472 ovs_flow_tbl_destroy(old_table, true);
476 static void clear_stats(struct sw_flow *flow)
480 flow->packet_count = 0;
481 flow->byte_count = 0;
484 static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
486 struct ovs_header *ovs_header = info->userhdr;
487 struct nlattr **a = info->attrs;
488 struct sw_flow_actions *acts;
489 struct sk_buff *packet;
490 struct sw_flow *flow;
497 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
498 !a[OVS_PACKET_ATTR_ACTIONS])
501 len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
502 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
506 skb_reserve(packet, NET_IP_ALIGN);
508 nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
510 skb_reset_mac_header(packet);
511 eth = eth_hdr(packet);
513 /* Normally, setting the skb 'protocol' field would be handled by a
514 * call to eth_type_trans(), but it assumes there's a sending
515 * device, which we may not have. */
516 if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN)
517 packet->protocol = eth->h_proto;
519 packet->protocol = htons(ETH_P_802_2);
521 /* Build an sw_flow for sending this packet. */
522 flow = ovs_flow_alloc();
527 err = ovs_flow_extract(packet, -1, &flow->key);
531 err = ovs_nla_get_flow_metadata(flow, a[OVS_PACKET_ATTR_KEY]);
534 acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_PACKET_ATTR_ACTIONS]));
539 err = ovs_nla_copy_actions(a[OVS_PACKET_ATTR_ACTIONS],
540 &flow->key, 0, &acts);
541 rcu_assign_pointer(flow->sf_acts, acts);
545 OVS_CB(packet)->flow = flow;
546 OVS_CB(packet)->pkt_key = &flow->key;
547 packet->priority = flow->key.phy.priority;
548 packet->mark = flow->key.phy.skb_mark;
551 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
557 err = ovs_execute_actions(dp, packet);
561 ovs_flow_free(flow, false);
567 ovs_flow_free(flow, false);
574 static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
575 [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
576 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
577 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
580 static struct genl_ops dp_packet_genl_ops[] = {
581 { .cmd = OVS_PACKET_CMD_EXECUTE,
582 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
583 .policy = packet_policy,
584 .doit = ovs_packet_cmd_execute
588 static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats)
590 struct flow_table *table;
593 table = rcu_dereference_check(dp->table, lockdep_ovsl_is_held());
594 stats->n_flows = ovs_flow_tbl_count(table);
596 stats->n_hit = stats->n_missed = stats->n_lost = 0;
597 for_each_possible_cpu(i) {
598 const struct dp_stats_percpu *percpu_stats;
599 struct dp_stats_percpu local_stats;
602 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
605 start = u64_stats_fetch_begin_bh(&percpu_stats->sync);
606 local_stats = *percpu_stats;
607 } while (u64_stats_fetch_retry_bh(&percpu_stats->sync, start));
609 stats->n_hit += local_stats.n_hit;
610 stats->n_missed += local_stats.n_missed;
611 stats->n_lost += local_stats.n_lost;
615 static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
616 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
617 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
618 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
621 static struct genl_family dp_flow_genl_family = {
622 .id = GENL_ID_GENERATE,
623 .hdrsize = sizeof(struct ovs_header),
624 .name = OVS_FLOW_FAMILY,
625 .version = OVS_FLOW_VERSION,
626 .maxattr = OVS_FLOW_ATTR_MAX,
631 static struct genl_multicast_group ovs_dp_flow_multicast_group = {
632 .name = OVS_FLOW_MCGROUP
635 static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts)
637 return NLMSG_ALIGN(sizeof(struct ovs_header))
638 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_KEY */
639 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_MASK */
640 + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
641 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
642 + nla_total_size(8) /* OVS_FLOW_ATTR_USED */
643 + nla_total_size(acts->actions_len); /* OVS_FLOW_ATTR_ACTIONS */
646 /* Called with ovs_mutex. */
647 static int ovs_flow_cmd_fill_info(struct sw_flow *flow, struct datapath *dp,
648 struct sk_buff *skb, u32 portid,
649 u32 seq, u32 flags, u8 cmd)
651 const int skb_orig_len = skb->len;
652 struct nlattr *start;
653 struct ovs_flow_stats stats;
654 struct ovs_header *ovs_header;
660 ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd);
664 ovs_header->dp_ifindex = get_dpifindex(dp);
667 nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
669 goto nla_put_failure;
671 err = ovs_nla_put_flow(&flow->unmasked_key, &flow->unmasked_key, skb);
674 nla_nest_end(skb, nla);
676 nla = nla_nest_start(skb, OVS_FLOW_ATTR_MASK);
678 goto nla_put_failure;
680 err = ovs_nla_put_flow(&flow->key, &flow->mask->key, skb);
684 nla_nest_end(skb, nla);
686 spin_lock_bh(&flow->lock);
688 stats.n_packets = flow->packet_count;
689 stats.n_bytes = flow->byte_count;
690 tcp_flags = flow->tcp_flags;
691 spin_unlock_bh(&flow->lock);
694 nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
695 goto nla_put_failure;
697 if (stats.n_packets &&
698 nla_put(skb, OVS_FLOW_ATTR_STATS,
699 sizeof(struct ovs_flow_stats), &stats))
700 goto nla_put_failure;
703 nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, tcp_flags))
704 goto nla_put_failure;
706 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
707 * this is the first flow to be dumped into 'skb'. This is unusual for
708 * Netlink but individual action lists can be longer than
709 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
710 * The userspace caller can always fetch the actions separately if it
711 * really wants them. (Most userspace callers in fact don't care.)
713 * This can only fail for dump operations because the skb is always
714 * properly sized for single flows.
716 start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
718 const struct sw_flow_actions *sf_acts;
720 sf_acts = rcu_dereference_check(flow->sf_acts,
721 lockdep_ovsl_is_held());
723 err = ovs_nla_put_actions(sf_acts->actions,
724 sf_acts->actions_len, skb);
726 nla_nest_end(skb, start);
731 nla_nest_cancel(skb, start);
733 } else if (skb_orig_len)
734 goto nla_put_failure;
736 return genlmsg_end(skb, ovs_header);
741 genlmsg_cancel(skb, ovs_header);
745 static struct sk_buff *ovs_flow_cmd_alloc_info(struct sw_flow *flow)
747 const struct sw_flow_actions *sf_acts;
749 sf_acts = ovsl_dereference(flow->sf_acts);
751 return genlmsg_new(ovs_flow_cmd_msg_size(sf_acts), GFP_KERNEL);
754 static struct sk_buff *ovs_flow_cmd_build_info(struct sw_flow *flow,
756 u32 portid, u32 seq, u8 cmd)
761 skb = ovs_flow_cmd_alloc_info(flow);
763 return ERR_PTR(-ENOMEM);
765 retval = ovs_flow_cmd_fill_info(flow, dp, skb, portid, seq, 0, cmd);
770 static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info)
772 struct nlattr **a = info->attrs;
773 struct ovs_header *ovs_header = info->userhdr;
774 struct sw_flow_key key, masked_key;
775 struct sw_flow *flow = NULL;
776 struct sw_flow_mask mask;
777 struct sk_buff *reply;
779 struct flow_table *table;
780 struct sw_flow_actions *acts = NULL;
781 struct sw_flow_match match;
786 if (!a[OVS_FLOW_ATTR_KEY])
789 ovs_match_init(&match, &key, &mask);
790 error = ovs_nla_get_match(&match,
791 a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
795 /* Validate actions. */
796 if (a[OVS_FLOW_ATTR_ACTIONS]) {
797 acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_FLOW_ATTR_ACTIONS]));
798 error = PTR_ERR(acts);
802 ovs_flow_mask_key(&masked_key, &key, &mask);
803 error = ovs_nla_copy_actions(a[OVS_FLOW_ATTR_ACTIONS],
804 &masked_key, 0, &acts);
806 OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
809 } else if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW) {
815 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
820 table = ovsl_dereference(dp->table);
822 /* Check if this is a duplicate flow */
823 flow = ovs_flow_tbl_lookup(table, &key);
825 struct flow_table *new_table = NULL;
826 struct sw_flow_mask *mask_p;
828 /* Bail out if we're not allowed to create a new flow. */
830 if (info->genlhdr->cmd == OVS_FLOW_CMD_SET)
833 /* Expand table, if necessary, to make room. */
834 if (ovs_flow_tbl_need_to_expand(table))
835 new_table = ovs_flow_tbl_expand(table);
836 else if (time_after(jiffies, dp->last_rehash + REHASH_FLOW_INTERVAL))
837 new_table = ovs_flow_tbl_rehash(table);
839 if (new_table && !IS_ERR(new_table)) {
840 rcu_assign_pointer(dp->table, new_table);
841 ovs_flow_tbl_destroy(table, true);
842 table = ovsl_dereference(dp->table);
843 dp->last_rehash = jiffies;
847 flow = ovs_flow_alloc();
849 error = PTR_ERR(flow);
854 flow->key = masked_key;
855 flow->unmasked_key = key;
857 /* Make sure mask is unique in the system */
858 mask_p = ovs_sw_flow_mask_find(table, &mask);
860 /* Allocate a new mask if none exsits. */
861 mask_p = ovs_sw_flow_mask_alloc();
864 mask_p->key = mask.key;
865 mask_p->range = mask.range;
866 ovs_sw_flow_mask_insert(table, mask_p);
869 ovs_sw_flow_mask_add_ref(mask_p);
871 rcu_assign_pointer(flow->sf_acts, acts);
873 /* Put flow in bucket. */
874 ovs_flow_tbl_insert(table, flow);
876 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
877 info->snd_seq, OVS_FLOW_CMD_NEW);
879 /* We found a matching flow. */
880 struct sw_flow_actions *old_acts;
882 /* Bail out if we're not allowed to modify an existing flow.
883 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
884 * because Generic Netlink treats the latter as a dump
885 * request. We also accept NLM_F_EXCL in case that bug ever
889 if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW &&
890 info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL))
893 /* The unmasked key has to be the same for flow updates. */
895 if (!ovs_flow_cmp_unmasked_key(flow, &match)) {
896 OVS_NLERR("Flow modification message rejected, unmasked key does not match.\n");
900 /* Update actions. */
901 old_acts = ovsl_dereference(flow->sf_acts);
902 rcu_assign_pointer(flow->sf_acts, acts);
903 ovs_nla_free_flow_actions(old_acts);
905 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
906 info->snd_seq, OVS_FLOW_CMD_NEW);
909 if (a[OVS_FLOW_ATTR_CLEAR]) {
910 spin_lock_bh(&flow->lock);
912 spin_unlock_bh(&flow->lock);
918 ovs_notify(reply, info, &ovs_dp_flow_multicast_group);
920 netlink_set_err(sock_net(skb->sk)->genl_sock, 0,
921 ovs_dp_flow_multicast_group.id, PTR_ERR(reply));
925 ovs_flow_free(flow, false);
934 static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
936 struct nlattr **a = info->attrs;
937 struct ovs_header *ovs_header = info->userhdr;
938 struct sw_flow_key key;
939 struct sk_buff *reply;
940 struct sw_flow *flow;
942 struct flow_table *table;
943 struct sw_flow_match match;
946 if (!a[OVS_FLOW_ATTR_KEY]) {
947 OVS_NLERR("Flow get message rejected, Key attribute missing.\n");
951 ovs_match_init(&match, &key, NULL);
952 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
957 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
963 table = ovsl_dereference(dp->table);
964 flow = ovs_flow_tbl_lookup(table, &key);
965 if (!flow || !ovs_flow_cmp_unmasked_key(flow, &match)) {
970 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
971 info->snd_seq, OVS_FLOW_CMD_NEW);
973 err = PTR_ERR(reply);
978 return genlmsg_reply(reply, info);
984 static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
986 struct nlattr **a = info->attrs;
987 struct ovs_header *ovs_header = info->userhdr;
988 struct sw_flow_key key;
989 struct sk_buff *reply;
990 struct sw_flow *flow;
992 struct flow_table *table;
993 struct sw_flow_match match;
997 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1003 if (!a[OVS_FLOW_ATTR_KEY]) {
1004 err = flush_flows(dp);
1008 ovs_match_init(&match, &key, NULL);
1009 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
1013 table = ovsl_dereference(dp->table);
1014 flow = ovs_flow_tbl_lookup(table, &key);
1015 if (!flow || !ovs_flow_cmp_unmasked_key(flow, &match)) {
1020 reply = ovs_flow_cmd_alloc_info(flow);
1026 ovs_flow_tbl_remove(table, flow);
1028 err = ovs_flow_cmd_fill_info(flow, dp, reply, info->snd_portid,
1029 info->snd_seq, 0, OVS_FLOW_CMD_DEL);
1032 ovs_flow_free(flow, true);
1035 ovs_notify(reply, info, &ovs_dp_flow_multicast_group);
1042 static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1044 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1045 struct datapath *dp;
1046 struct flow_table *table;
1049 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1055 table = rcu_dereference(dp->table);
1057 struct sw_flow *flow;
1060 bucket = cb->args[0];
1062 flow = ovs_flow_tbl_dump_next(table, &bucket, &obj);
1066 if (ovs_flow_cmd_fill_info(flow, dp, skb,
1067 NETLINK_CB(cb->skb).portid,
1068 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1069 OVS_FLOW_CMD_NEW) < 0)
1072 cb->args[0] = bucket;
1079 static struct genl_ops dp_flow_genl_ops[] = {
1080 { .cmd = OVS_FLOW_CMD_NEW,
1081 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1082 .policy = flow_policy,
1083 .doit = ovs_flow_cmd_new_or_set
1085 { .cmd = OVS_FLOW_CMD_DEL,
1086 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1087 .policy = flow_policy,
1088 .doit = ovs_flow_cmd_del
1090 { .cmd = OVS_FLOW_CMD_GET,
1091 .flags = 0, /* OK for unprivileged users. */
1092 .policy = flow_policy,
1093 .doit = ovs_flow_cmd_get,
1094 .dumpit = ovs_flow_cmd_dump
1096 { .cmd = OVS_FLOW_CMD_SET,
1097 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1098 .policy = flow_policy,
1099 .doit = ovs_flow_cmd_new_or_set,
1103 static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1104 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1105 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1108 static struct genl_family dp_datapath_genl_family = {
1109 .id = GENL_ID_GENERATE,
1110 .hdrsize = sizeof(struct ovs_header),
1111 .name = OVS_DATAPATH_FAMILY,
1112 .version = OVS_DATAPATH_VERSION,
1113 .maxattr = OVS_DP_ATTR_MAX,
1118 static struct genl_multicast_group ovs_dp_datapath_multicast_group = {
1119 .name = OVS_DATAPATH_MCGROUP
1122 static size_t ovs_dp_cmd_msg_size(void)
1124 size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
1126 msgsize += nla_total_size(IFNAMSIZ);
1127 msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
1132 static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
1133 u32 portid, u32 seq, u32 flags, u8 cmd)
1135 struct ovs_header *ovs_header;
1136 struct ovs_dp_stats dp_stats;
1139 ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
1144 ovs_header->dp_ifindex = get_dpifindex(dp);
1147 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
1150 goto nla_put_failure;
1152 get_dp_stats(dp, &dp_stats);
1153 if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats), &dp_stats))
1154 goto nla_put_failure;
1156 return genlmsg_end(skb, ovs_header);
1159 genlmsg_cancel(skb, ovs_header);
1164 static struct sk_buff *ovs_dp_cmd_build_info(struct datapath *dp, u32 portid,
1167 struct sk_buff *skb;
1170 skb = genlmsg_new(ovs_dp_cmd_msg_size(), GFP_KERNEL);
1172 return ERR_PTR(-ENOMEM);
1174 retval = ovs_dp_cmd_fill_info(dp, skb, portid, seq, 0, cmd);
1177 return ERR_PTR(retval);
1182 /* Called with ovs_mutex. */
1183 static struct datapath *lookup_datapath(struct net *net,
1184 struct ovs_header *ovs_header,
1185 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1187 struct datapath *dp;
1189 if (!a[OVS_DP_ATTR_NAME])
1190 dp = get_dp(net, ovs_header->dp_ifindex);
1192 struct vport *vport;
1195 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
1196 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
1199 return dp ? dp : ERR_PTR(-ENODEV);
1202 static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1204 struct nlattr **a = info->attrs;
1205 struct vport_parms parms;
1206 struct sk_buff *reply;
1207 struct datapath *dp;
1208 struct vport *vport;
1209 struct ovs_net *ovs_net;
1213 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1219 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1221 goto err_unlock_ovs;
1223 ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
1225 /* Allocate table. */
1227 rcu_assign_pointer(dp->table, ovs_flow_tbl_alloc(TBL_MIN_BUCKETS));
1231 dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
1232 if (!dp->stats_percpu) {
1234 goto err_destroy_table;
1237 dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
1241 goto err_destroy_percpu;
1244 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1245 INIT_HLIST_HEAD(&dp->ports[i]);
1247 /* Set up our datapath device. */
1248 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1249 parms.type = OVS_VPORT_TYPE_INTERNAL;
1250 parms.options = NULL;
1252 parms.port_no = OVSP_LOCAL;
1253 parms.upcall_portid = nla_get_u32(a[OVS_DP_ATTR_UPCALL_PID]);
1255 vport = new_vport(&parms);
1256 if (IS_ERR(vport)) {
1257 err = PTR_ERR(vport);
1261 goto err_destroy_ports_array;
1264 reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
1265 info->snd_seq, OVS_DP_CMD_NEW);
1266 err = PTR_ERR(reply);
1268 goto err_destroy_local_port;
1270 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
1271 list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
1275 ovs_notify(reply, info, &ovs_dp_datapath_multicast_group);
1278 err_destroy_local_port:
1279 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
1280 err_destroy_ports_array:
1283 free_percpu(dp->stats_percpu);
1285 ovs_flow_tbl_destroy(ovsl_dereference(dp->table), false);
1287 release_net(ovs_dp_get_net(dp));
1295 /* Called with ovs_mutex. */
1296 static void __dp_destroy(struct datapath *dp)
1300 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1301 struct vport *vport;
1302 struct hlist_node *n;
1304 hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
1305 if (vport->port_no != OVSP_LOCAL)
1306 ovs_dp_detach_port(vport);
1309 list_del_rcu(&dp->list_node);
1311 /* OVSP_LOCAL is datapath internal port. We need to make sure that
1312 * all port in datapath are destroyed first before freeing datapath.
1314 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
1316 call_rcu(&dp->rcu, destroy_dp_rcu);
1319 static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1321 struct sk_buff *reply;
1322 struct datapath *dp;
1326 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1331 reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
1332 info->snd_seq, OVS_DP_CMD_DEL);
1333 err = PTR_ERR(reply);
1340 ovs_notify(reply, info, &ovs_dp_datapath_multicast_group);
1348 static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1350 struct sk_buff *reply;
1351 struct datapath *dp;
1355 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1360 reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
1361 info->snd_seq, OVS_DP_CMD_NEW);
1362 if (IS_ERR(reply)) {
1363 err = PTR_ERR(reply);
1364 netlink_set_err(sock_net(skb->sk)->genl_sock, 0,
1365 ovs_dp_datapath_multicast_group.id, err);
1371 ovs_notify(reply, info, &ovs_dp_datapath_multicast_group);
1379 static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1381 struct sk_buff *reply;
1382 struct datapath *dp;
1386 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1392 reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
1393 info->snd_seq, OVS_DP_CMD_NEW);
1394 if (IS_ERR(reply)) {
1395 err = PTR_ERR(reply);
1400 return genlmsg_reply(reply, info);
1407 static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1409 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
1410 struct datapath *dp;
1411 int skip = cb->args[0];
1415 list_for_each_entry_rcu(dp, &ovs_net->dps, list_node) {
1417 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
1418 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1419 OVS_DP_CMD_NEW) < 0)
1430 static struct genl_ops dp_datapath_genl_ops[] = {
1431 { .cmd = OVS_DP_CMD_NEW,
1432 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1433 .policy = datapath_policy,
1434 .doit = ovs_dp_cmd_new
1436 { .cmd = OVS_DP_CMD_DEL,
1437 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1438 .policy = datapath_policy,
1439 .doit = ovs_dp_cmd_del
1441 { .cmd = OVS_DP_CMD_GET,
1442 .flags = 0, /* OK for unprivileged users. */
1443 .policy = datapath_policy,
1444 .doit = ovs_dp_cmd_get,
1445 .dumpit = ovs_dp_cmd_dump
1447 { .cmd = OVS_DP_CMD_SET,
1448 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1449 .policy = datapath_policy,
1450 .doit = ovs_dp_cmd_set,
1454 static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
1455 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1456 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
1457 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
1458 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
1459 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1460 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
1463 static struct genl_family dp_vport_genl_family = {
1464 .id = GENL_ID_GENERATE,
1465 .hdrsize = sizeof(struct ovs_header),
1466 .name = OVS_VPORT_FAMILY,
1467 .version = OVS_VPORT_VERSION,
1468 .maxattr = OVS_VPORT_ATTR_MAX,
1473 struct genl_multicast_group ovs_dp_vport_multicast_group = {
1474 .name = OVS_VPORT_MCGROUP
1477 /* Called with ovs_mutex or RCU read lock. */
1478 static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
1479 u32 portid, u32 seq, u32 flags, u8 cmd)
1481 struct ovs_header *ovs_header;
1482 struct ovs_vport_stats vport_stats;
1485 ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
1490 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1492 if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1493 nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
1494 nla_put_string(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport)) ||
1495 nla_put_u32(skb, OVS_VPORT_ATTR_UPCALL_PID, vport->upcall_portid))
1496 goto nla_put_failure;
1498 ovs_vport_get_stats(vport, &vport_stats);
1499 if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
1501 goto nla_put_failure;
1503 err = ovs_vport_get_options(vport, skb);
1504 if (err == -EMSGSIZE)
1507 return genlmsg_end(skb, ovs_header);
1512 genlmsg_cancel(skb, ovs_header);
1516 /* Called with ovs_mutex or RCU read lock. */
1517 struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
1520 struct sk_buff *skb;
1523 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1525 return ERR_PTR(-ENOMEM);
1527 retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
1533 /* Called with ovs_mutex or RCU read lock. */
1534 static struct vport *lookup_vport(struct net *net,
1535 struct ovs_header *ovs_header,
1536 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1538 struct datapath *dp;
1539 struct vport *vport;
1541 if (a[OVS_VPORT_ATTR_NAME]) {
1542 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
1544 return ERR_PTR(-ENODEV);
1545 if (ovs_header->dp_ifindex &&
1546 ovs_header->dp_ifindex != get_dpifindex(vport->dp))
1547 return ERR_PTR(-ENODEV);
1549 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1550 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1552 if (port_no >= DP_MAX_PORTS)
1553 return ERR_PTR(-EFBIG);
1555 dp = get_dp(net, ovs_header->dp_ifindex);
1557 return ERR_PTR(-ENODEV);
1559 vport = ovs_vport_ovsl_rcu(dp, port_no);
1561 return ERR_PTR(-ENODEV);
1564 return ERR_PTR(-EINVAL);
1567 static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1569 struct nlattr **a = info->attrs;
1570 struct ovs_header *ovs_header = info->userhdr;
1571 struct vport_parms parms;
1572 struct sk_buff *reply;
1573 struct vport *vport;
1574 struct datapath *dp;
1579 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
1580 !a[OVS_VPORT_ATTR_UPCALL_PID])
1584 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1589 if (a[OVS_VPORT_ATTR_PORT_NO]) {
1590 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1593 if (port_no >= DP_MAX_PORTS)
1596 vport = ovs_vport_ovsl(dp, port_no);
1601 for (port_no = 1; ; port_no++) {
1602 if (port_no >= DP_MAX_PORTS) {
1606 vport = ovs_vport_ovsl(dp, port_no);
1612 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
1613 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1614 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
1616 parms.port_no = port_no;
1617 parms.upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
1619 vport = new_vport(&parms);
1620 err = PTR_ERR(vport);
1625 if (a[OVS_VPORT_ATTR_STATS])
1626 ovs_vport_set_stats(vport, nla_data(a[OVS_VPORT_ATTR_STATS]));
1628 reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq,
1630 if (IS_ERR(reply)) {
1631 err = PTR_ERR(reply);
1632 ovs_dp_detach_port(vport);
1636 ovs_notify(reply, info, &ovs_dp_vport_multicast_group);
1644 static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
1646 struct nlattr **a = info->attrs;
1647 struct sk_buff *reply;
1648 struct vport *vport;
1652 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
1653 err = PTR_ERR(vport);
1657 if (a[OVS_VPORT_ATTR_TYPE] &&
1658 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
1663 reply = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1669 if (a[OVS_VPORT_ATTR_OPTIONS]) {
1670 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
1675 if (a[OVS_VPORT_ATTR_STATS])
1676 ovs_vport_set_stats(vport, nla_data(a[OVS_VPORT_ATTR_STATS]));
1678 if (a[OVS_VPORT_ATTR_UPCALL_PID])
1679 vport->upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
1681 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1682 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1686 ovs_notify(reply, info, &ovs_dp_vport_multicast_group);
1696 static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
1698 struct nlattr **a = info->attrs;
1699 struct sk_buff *reply;
1700 struct vport *vport;
1704 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
1705 err = PTR_ERR(vport);
1709 if (vport->port_no == OVSP_LOCAL) {
1714 reply = ovs_vport_cmd_build_info(vport, info->snd_portid,
1715 info->snd_seq, OVS_VPORT_CMD_DEL);
1716 err = PTR_ERR(reply);
1721 ovs_dp_detach_port(vport);
1723 ovs_notify(reply, info, &ovs_dp_vport_multicast_group);
1730 static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
1732 struct nlattr **a = info->attrs;
1733 struct ovs_header *ovs_header = info->userhdr;
1734 struct sk_buff *reply;
1735 struct vport *vport;
1739 vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
1740 err = PTR_ERR(vport);
1744 reply = ovs_vport_cmd_build_info(vport, info->snd_portid,
1745 info->snd_seq, OVS_VPORT_CMD_NEW);
1746 err = PTR_ERR(reply);
1752 return genlmsg_reply(reply, info);
1759 static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1761 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1762 struct datapath *dp;
1763 int bucket = cb->args[0], skip = cb->args[1];
1766 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1771 for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
1772 struct vport *vport;
1775 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
1777 ovs_vport_cmd_fill_info(vport, skb,
1778 NETLINK_CB(cb->skb).portid,
1781 OVS_VPORT_CMD_NEW) < 0)
1797 static struct genl_ops dp_vport_genl_ops[] = {
1798 { .cmd = OVS_VPORT_CMD_NEW,
1799 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1800 .policy = vport_policy,
1801 .doit = ovs_vport_cmd_new
1803 { .cmd = OVS_VPORT_CMD_DEL,
1804 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1805 .policy = vport_policy,
1806 .doit = ovs_vport_cmd_del
1808 { .cmd = OVS_VPORT_CMD_GET,
1809 .flags = 0, /* OK for unprivileged users. */
1810 .policy = vport_policy,
1811 .doit = ovs_vport_cmd_get,
1812 .dumpit = ovs_vport_cmd_dump
1814 { .cmd = OVS_VPORT_CMD_SET,
1815 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1816 .policy = vport_policy,
1817 .doit = ovs_vport_cmd_set,
1821 struct genl_family_and_ops {
1822 struct genl_family *family;
1823 struct genl_ops *ops;
1825 struct genl_multicast_group *group;
1828 static const struct genl_family_and_ops dp_genl_families[] = {
1829 { &dp_datapath_genl_family,
1830 dp_datapath_genl_ops, ARRAY_SIZE(dp_datapath_genl_ops),
1831 &ovs_dp_datapath_multicast_group },
1832 { &dp_vport_genl_family,
1833 dp_vport_genl_ops, ARRAY_SIZE(dp_vport_genl_ops),
1834 &ovs_dp_vport_multicast_group },
1835 { &dp_flow_genl_family,
1836 dp_flow_genl_ops, ARRAY_SIZE(dp_flow_genl_ops),
1837 &ovs_dp_flow_multicast_group },
1838 { &dp_packet_genl_family,
1839 dp_packet_genl_ops, ARRAY_SIZE(dp_packet_genl_ops),
1843 static void dp_unregister_genl(int n_families)
1847 for (i = 0; i < n_families; i++)
1848 genl_unregister_family(dp_genl_families[i].family);
1851 static int dp_register_genl(void)
1858 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
1859 const struct genl_family_and_ops *f = &dp_genl_families[i];
1861 err = genl_register_family_with_ops(f->family, f->ops,
1868 err = genl_register_mc_group(f->family, f->group);
1877 dp_unregister_genl(n_registered);
1881 static int __net_init ovs_init_net(struct net *net)
1883 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
1885 INIT_LIST_HEAD(&ovs_net->dps);
1886 INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
1890 static void __net_exit ovs_exit_net(struct net *net)
1892 struct datapath *dp, *dp_next;
1893 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
1896 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
1900 cancel_work_sync(&ovs_net->dp_notify_work);
1903 static struct pernet_operations ovs_net_ops = {
1904 .init = ovs_init_net,
1905 .exit = ovs_exit_net,
1907 .size = sizeof(struct ovs_net),
1910 DEFINE_COMPAT_PNET_REG_FUNC(device);
1912 static int __init dp_init(void)
1916 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
1918 pr_info("Open vSwitch switching datapath %s, built "__DATE__" "__TIME__"\n",
1921 err = ovs_flow_init();
1925 err = ovs_vport_init();
1927 goto error_flow_exit;
1929 err = register_pernet_device(&ovs_net_ops);
1931 goto error_vport_exit;
1933 err = register_netdevice_notifier(&ovs_dp_device_notifier);
1935 goto error_netns_exit;
1937 err = dp_register_genl();
1939 goto error_unreg_notifier;
1943 error_unreg_notifier:
1944 unregister_netdevice_notifier(&ovs_dp_device_notifier);
1946 unregister_pernet_device(&ovs_net_ops);
1955 static void dp_cleanup(void)
1957 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
1958 unregister_netdevice_notifier(&ovs_dp_device_notifier);
1959 unregister_pernet_device(&ovs_net_ops);
1965 module_init(dp_init);
1966 module_exit(dp_cleanup);
1968 MODULE_DESCRIPTION("Open vSwitch switching datapath");
1969 MODULE_LICENSE("GPL");
1970 MODULE_VERSION(VERSION);