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 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 datapath *dp, struct sk_buff *,
114 const struct dp_upcall_info *);
115 static int queue_userspace_packet(struct datapath *dp, struct sk_buff *,
116 const struct dp_upcall_info *);
118 /* Must be called with rcu_read_lock or ovs_mutex. */
119 static struct datapath *get_dp(struct net *net, int dp_ifindex)
121 struct datapath *dp = NULL;
122 struct net_device *dev;
125 dev = dev_get_by_index_rcu(net, dp_ifindex);
127 struct vport *vport = ovs_internal_dev_get_vport(dev);
136 /* Must be called with rcu_read_lock or ovs_mutex. */
137 const char *ovs_dp_name(const struct datapath *dp)
139 struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL);
140 return vport->ops->get_name(vport);
143 static int get_dpifindex(struct datapath *dp)
150 local = ovs_vport_rcu(dp, OVSP_LOCAL);
152 ifindex = netdev_vport_priv(local)->dev->ifindex;
161 static void destroy_dp_rcu(struct rcu_head *rcu)
163 struct datapath *dp = container_of(rcu, struct datapath, rcu);
165 ovs_flow_tbl_destroy(&dp->table);
166 free_percpu(dp->stats_percpu);
167 release_net(ovs_dp_get_net(dp));
172 static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
175 return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
178 struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
181 struct hlist_head *head;
183 head = vport_hash_bucket(dp, port_no);
184 hlist_for_each_entry_rcu(vport, head, dp_hash_node) {
185 if (vport->port_no == port_no)
191 /* Called with ovs_mutex. */
192 static struct vport *new_vport(const struct vport_parms *parms)
196 vport = ovs_vport_add(parms);
197 if (!IS_ERR(vport)) {
198 struct datapath *dp = parms->dp;
199 struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
201 hlist_add_head_rcu(&vport->dp_hash_node, head);
206 void ovs_dp_detach_port(struct vport *p)
210 /* First drop references to device. */
211 hlist_del_rcu(&p->dp_hash_node);
213 /* Then destroy it. */
217 /* Must be called with rcu_read_lock. */
218 void ovs_dp_process_received_packet(struct vport *p, struct sk_buff *skb)
220 struct datapath *dp = p->dp;
221 struct sw_flow *flow;
222 struct dp_stats_percpu *stats;
223 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_stats(&dp->table, &key, &n_mask_hit);
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 ovs_flow_stats_update(OVS_CB(skb)->flow, skb);
256 ovs_execute_actions(dp, skb);
257 stats_counter = &stats->n_hit;
260 /* Update datapath statistics. */
261 u64_stats_update_begin(&stats->sync);
263 stats->n_mask_hit += n_mask_hit;
264 u64_stats_update_end(&stats->sync);
267 static struct genl_family dp_packet_genl_family = {
268 .id = GENL_ID_GENERATE,
269 .hdrsize = sizeof(struct ovs_header),
270 .name = OVS_PACKET_FAMILY,
271 .version = OVS_PACKET_VERSION,
272 .maxattr = OVS_PACKET_ATTR_MAX,
277 int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
278 const struct dp_upcall_info *upcall_info)
280 struct dp_stats_percpu *stats;
283 if (upcall_info->portid == 0) {
288 if (!skb_is_gso(skb))
289 err = queue_userspace_packet(dp, skb, upcall_info);
291 err = queue_gso_packets(dp, skb, upcall_info);
298 stats = this_cpu_ptr(dp->stats_percpu);
300 u64_stats_update_begin(&stats->sync);
302 u64_stats_update_end(&stats->sync);
307 static int queue_gso_packets(struct datapath *dp, struct sk_buff *skb,
308 const struct dp_upcall_info *upcall_info)
310 unsigned short gso_type = skb_shinfo(skb)->gso_type;
311 struct dp_upcall_info later_info;
312 struct sw_flow_key later_key;
313 struct sk_buff *segs, *nskb;
316 segs = __skb_gso_segment(skb, NETIF_F_SG, false);
318 return PTR_ERR(segs);
320 /* Queue all of the segments. */
323 err = queue_userspace_packet(dp, skb, upcall_info);
327 if (skb == segs && gso_type & SKB_GSO_UDP) {
328 /* The initial flow key extracted by ovs_flow_extract()
329 * in this case is for a first fragment, so we need to
330 * properly mark later fragments.
332 later_key = *upcall_info->key;
333 later_key.ip.frag = OVS_FRAG_TYPE_LATER;
335 later_info = *upcall_info;
336 later_info.key = &later_key;
337 upcall_info = &later_info;
339 } while ((skb = skb->next));
341 /* Free all of the segments. */
349 } while ((skb = nskb));
353 static size_t key_attr_size(void)
355 return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */
356 + nla_total_size(0) /* OVS_KEY_ATTR_TUNNEL */
357 + nla_total_size(8) /* OVS_TUNNEL_KEY_ATTR_ID */
358 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_SRC */
359 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_DST */
360 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TOS */
361 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TTL */
362 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT */
363 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_CSUM */
364 + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */
365 + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
366 + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
367 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
368 + nla_total_size(4) /* OVS_KEY_ATTR_8021Q */
369 + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
370 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
371 + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
372 + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
373 + nla_total_size(28); /* OVS_KEY_ATTR_ND */
376 static size_t upcall_msg_size(const struct nlattr *userdata,
379 size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
380 + nla_total_size(hdrlen) /* OVS_PACKET_ATTR_PACKET */
381 + nla_total_size(key_attr_size()); /* OVS_PACKET_ATTR_KEY */
383 /* OVS_PACKET_ATTR_USERDATA */
385 size += NLA_ALIGN(userdata->nla_len);
390 static int queue_userspace_packet(struct datapath *dp, struct sk_buff *skb,
391 const struct dp_upcall_info *upcall_info)
393 struct ovs_header *upcall;
394 struct sk_buff *nskb = NULL;
395 struct sk_buff *user_skb; /* to be queued to userspace */
397 struct genl_info info = {
398 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,14,0)
399 .dst_sk = ovs_dp_get_net(dp)->genl_sock,
401 .snd_portid = upcall_info->portid,
407 dp_ifindex = get_dpifindex(dp);
411 if (vlan_tx_tag_present(skb)) {
412 nskb = skb_clone(skb, GFP_ATOMIC);
416 nskb = __vlan_put_tag(nskb, nskb->vlan_proto, vlan_tx_tag_get(nskb));
420 vlan_set_tci(nskb, 0);
425 if (nla_attr_size(skb->len) > USHRT_MAX) {
430 /* Complete checksum if needed */
431 if (skb->ip_summed == CHECKSUM_PARTIAL &&
432 (err = skb_checksum_help(skb)))
435 /* Older versions of OVS user space enforce alignment of the last
436 * Netlink attribute to NLA_ALIGNTO which would require extensive
437 * padding logic. Only perform zerocopy if padding is not required.
439 if (dp->user_features & OVS_DP_F_UNALIGNED)
440 hlen = skb_zerocopy_headlen(skb);
444 len = upcall_msg_size(upcall_info->userdata, hlen);
445 user_skb = genlmsg_new_unicast(len, &info, GFP_ATOMIC);
451 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
452 0, upcall_info->cmd);
453 upcall->dp_ifindex = dp_ifindex;
455 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
456 ovs_nla_put_flow(upcall_info->key, upcall_info->key, user_skb);
457 nla_nest_end(user_skb, nla);
459 if (upcall_info->userdata)
460 __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
461 nla_len(upcall_info->userdata),
462 nla_data(upcall_info->userdata));
464 /* Only reserve room for attribute header, packet data is added
465 * in skb_zerocopy() */
466 if (!(nla = nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, 0))) {
470 nla->nla_len = nla_attr_size(skb->len);
472 skb_zerocopy(user_skb, skb, skb->len, hlen);
474 ((struct nlmsghdr *) user_skb->data)->nlmsg_len = user_skb->len;
476 err = genlmsg_unicast(ovs_dp_get_net(dp), user_skb, upcall_info->portid);
482 static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
484 struct ovs_header *ovs_header = info->userhdr;
485 struct nlattr **a = info->attrs;
486 struct sw_flow_actions *acts;
487 struct sk_buff *packet;
488 struct sw_flow *flow;
495 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
496 !a[OVS_PACKET_ATTR_ACTIONS])
499 len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
500 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
504 skb_reserve(packet, NET_IP_ALIGN);
506 nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
508 skb_reset_mac_header(packet);
509 eth = eth_hdr(packet);
511 /* Normally, setting the skb 'protocol' field would be handled by a
512 * call to eth_type_trans(), but it assumes there's a sending
513 * device, which we may not have. */
514 if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN)
515 packet->protocol = eth->h_proto;
517 packet->protocol = htons(ETH_P_802_2);
519 /* Build an sw_flow for sending this packet. */
520 flow = ovs_flow_alloc(false);
525 err = ovs_flow_extract(packet, -1, &flow->key);
529 err = ovs_nla_get_flow_metadata(flow, a[OVS_PACKET_ATTR_KEY]);
532 acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_PACKET_ATTR_ACTIONS]));
537 err = ovs_nla_copy_actions(a[OVS_PACKET_ATTR_ACTIONS],
538 &flow->key, 0, &acts);
539 rcu_assign_pointer(flow->sf_acts, acts);
543 OVS_CB(packet)->flow = flow;
544 OVS_CB(packet)->pkt_key = &flow->key;
545 packet->priority = flow->key.phy.priority;
546 packet->mark = flow->key.phy.skb_mark;
549 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
555 err = ovs_execute_actions(dp, packet);
559 ovs_flow_free(flow, false);
565 ovs_flow_free(flow, false);
572 static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
573 [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
574 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
575 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
578 static struct genl_ops dp_packet_genl_ops[] = {
579 { .cmd = OVS_PACKET_CMD_EXECUTE,
580 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
581 .policy = packet_policy,
582 .doit = ovs_packet_cmd_execute
586 static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats,
587 struct ovs_dp_megaflow_stats *mega_stats)
591 memset(mega_stats, 0, sizeof(*mega_stats));
593 stats->n_flows = ovs_flow_tbl_count(&dp->table);
594 mega_stats->n_masks = ovs_flow_tbl_num_masks(&dp->table);
596 stats->n_hit = stats->n_missed = stats->n_lost = 0;
598 for_each_possible_cpu(i) {
599 const struct dp_stats_percpu *percpu_stats;
600 struct dp_stats_percpu local_stats;
603 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
606 start = u64_stats_fetch_begin_bh(&percpu_stats->sync);
607 local_stats = *percpu_stats;
608 } while (u64_stats_fetch_retry_bh(&percpu_stats->sync, start));
610 stats->n_hit += local_stats.n_hit;
611 stats->n_missed += local_stats.n_missed;
612 stats->n_lost += local_stats.n_lost;
613 mega_stats->n_mask_hit += local_stats.n_mask_hit;
617 static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
618 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
619 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
620 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
623 static struct genl_family dp_flow_genl_family = {
624 .id = GENL_ID_GENERATE,
625 .hdrsize = sizeof(struct ovs_header),
626 .name = OVS_FLOW_FAMILY,
627 .version = OVS_FLOW_VERSION,
628 .maxattr = OVS_FLOW_ATTR_MAX,
633 static struct genl_multicast_group ovs_dp_flow_multicast_group = {
634 .name = OVS_FLOW_MCGROUP
637 static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts)
639 return NLMSG_ALIGN(sizeof(struct ovs_header))
640 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_KEY */
641 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_MASK */
642 + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
643 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
644 + nla_total_size(8) /* OVS_FLOW_ATTR_USED */
645 + nla_total_size(acts->actions_len); /* OVS_FLOW_ATTR_ACTIONS */
648 /* Called with ovs_mutex. */
649 static int ovs_flow_cmd_fill_info(struct sw_flow *flow, struct datapath *dp,
650 struct sk_buff *skb, u32 portid,
651 u32 seq, u32 flags, u8 cmd)
653 const int skb_orig_len = skb->len;
654 struct nlattr *start;
655 struct ovs_flow_stats stats;
658 struct ovs_header *ovs_header;
662 ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd);
666 ovs_header->dp_ifindex = get_dpifindex(dp);
669 nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
671 goto nla_put_failure;
673 err = ovs_nla_put_flow(&flow->unmasked_key, &flow->unmasked_key, skb);
676 nla_nest_end(skb, nla);
678 nla = nla_nest_start(skb, OVS_FLOW_ATTR_MASK);
680 goto nla_put_failure;
682 err = ovs_nla_put_flow(&flow->key, &flow->mask->key, skb);
686 nla_nest_end(skb, nla);
688 ovs_flow_stats_get(flow, &stats, &used, &tcp_flags);
690 nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
691 goto nla_put_failure;
693 if (stats.n_packets &&
694 nla_put(skb, OVS_FLOW_ATTR_STATS, sizeof(struct ovs_flow_stats), &stats))
695 goto nla_put_failure;
697 if ((u8)ntohs(tcp_flags) &&
698 nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, (u8)ntohs(tcp_flags)))
699 goto nla_put_failure;
701 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
702 * this is the first flow to be dumped into 'skb'. This is unusual for
703 * Netlink but individual action lists can be longer than
704 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
705 * The userspace caller can always fetch the actions separately if it
706 * really wants them. (Most userspace callers in fact don't care.)
708 * This can only fail for dump operations because the skb is always
709 * properly sized for single flows.
711 start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
713 const struct sw_flow_actions *sf_acts;
715 sf_acts = rcu_dereference_ovsl(flow->sf_acts);
717 err = ovs_nla_put_actions(sf_acts->actions,
718 sf_acts->actions_len, skb);
720 nla_nest_end(skb, start);
725 nla_nest_cancel(skb, start);
727 } else if (skb_orig_len)
728 goto nla_put_failure;
730 return genlmsg_end(skb, ovs_header);
735 genlmsg_cancel(skb, ovs_header);
739 static struct sk_buff *ovs_flow_cmd_alloc_info(struct sw_flow *flow,
740 struct genl_info *info)
744 len = ovs_flow_cmd_msg_size(ovsl_dereference(flow->sf_acts));
746 return genlmsg_new_unicast(len, info, GFP_KERNEL);
749 static struct sk_buff *ovs_flow_cmd_build_info(struct sw_flow *flow,
751 struct genl_info *info,
757 skb = ovs_flow_cmd_alloc_info(flow, info);
759 return ERR_PTR(-ENOMEM);
761 retval = ovs_flow_cmd_fill_info(flow, dp, skb, info->snd_portid,
762 info->snd_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 sw_flow_actions *acts = NULL;
777 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, &exact_5tuple,
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 /* Check if this is a duplicate flow */
818 flow = ovs_flow_tbl_lookup(&dp->table, &key);
820 /* Bail out if we're not allowed to create a new flow. */
822 if (info->genlhdr->cmd == OVS_FLOW_CMD_SET)
826 flow = ovs_flow_alloc(!exact_5tuple);
828 error = PTR_ERR(flow);
832 flow->key = masked_key;
833 flow->unmasked_key = key;
834 rcu_assign_pointer(flow->sf_acts, acts);
836 /* Put flow in bucket. */
837 error = ovs_flow_tbl_insert(&dp->table, flow, &mask);
843 reply = ovs_flow_cmd_build_info(flow, dp, info, OVS_FLOW_CMD_NEW);
845 /* We found a matching flow. */
846 struct sw_flow_actions *old_acts;
848 /* Bail out if we're not allowed to modify an existing flow.
849 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
850 * because Generic Netlink treats the latter as a dump
851 * request. We also accept NLM_F_EXCL in case that bug ever
855 if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW &&
856 info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL))
859 /* The unmasked key has to be the same for flow updates. */
861 if (!ovs_flow_cmp_unmasked_key(flow, &match)) {
862 OVS_NLERR("Flow modification message rejected, unmasked key does not match.\n");
866 /* Update actions. */
867 old_acts = ovsl_dereference(flow->sf_acts);
868 rcu_assign_pointer(flow->sf_acts, acts);
869 ovs_nla_free_flow_actions(old_acts);
871 reply = ovs_flow_cmd_build_info(flow, dp, info, OVS_FLOW_CMD_NEW);
874 if (a[OVS_FLOW_ATTR_CLEAR])
875 ovs_flow_stats_clear(flow);
880 ovs_notify(reply, info, &ovs_dp_flow_multicast_group);
882 netlink_set_err(sock_net(skb->sk)->genl_sock, 0,
883 ovs_dp_flow_multicast_group.id, PTR_ERR(reply));
887 ovs_flow_free(flow, false);
896 static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
898 struct nlattr **a = info->attrs;
899 struct ovs_header *ovs_header = info->userhdr;
900 struct sw_flow_key key;
901 struct sk_buff *reply;
902 struct sw_flow *flow;
904 struct sw_flow_match match;
907 if (!a[OVS_FLOW_ATTR_KEY]) {
908 OVS_NLERR("Flow get message rejected, Key attribute missing.\n");
912 ovs_match_init(&match, &key, NULL);
913 err = ovs_nla_get_match(&match, NULL, a[OVS_FLOW_ATTR_KEY], NULL);
918 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
924 flow = ovs_flow_tbl_lookup(&dp->table, &key);
925 if (!flow || !ovs_flow_cmp_unmasked_key(flow, &match)) {
930 reply = ovs_flow_cmd_build_info(flow, dp, info, OVS_FLOW_CMD_NEW);
932 err = PTR_ERR(reply);
937 return genlmsg_reply(reply, info);
943 static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
945 struct nlattr **a = info->attrs;
946 struct ovs_header *ovs_header = info->userhdr;
947 struct sw_flow_key key;
948 struct sk_buff *reply;
949 struct sw_flow *flow;
951 struct sw_flow_match match;
955 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
961 if (!a[OVS_FLOW_ATTR_KEY]) {
962 err = ovs_flow_tbl_flush(&dp->table);
966 ovs_match_init(&match, &key, NULL);
967 err = ovs_nla_get_match(&match, NULL, a[OVS_FLOW_ATTR_KEY], NULL);
971 flow = ovs_flow_tbl_lookup(&dp->table, &key);
972 if (!flow || !ovs_flow_cmp_unmasked_key(flow, &match)) {
977 reply = ovs_flow_cmd_alloc_info(flow, info);
983 ovs_flow_tbl_remove(&dp->table, flow);
985 err = ovs_flow_cmd_fill_info(flow, dp, reply, info->snd_portid,
986 info->snd_seq, 0, OVS_FLOW_CMD_DEL);
989 ovs_flow_free(flow, true);
992 ovs_notify(reply, info, &ovs_dp_flow_multicast_group);
999 static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1001 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1002 struct table_instance *ti;
1003 struct datapath *dp;
1006 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1012 ti = rcu_dereference(dp->table.ti);
1014 struct sw_flow *flow;
1017 bucket = cb->args[0];
1019 flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj);
1023 if (ovs_flow_cmd_fill_info(flow, dp, skb,
1024 NETLINK_CB(cb->skb).portid,
1025 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1026 OVS_FLOW_CMD_NEW) < 0)
1029 cb->args[0] = bucket;
1036 static struct genl_ops dp_flow_genl_ops[] = {
1037 { .cmd = OVS_FLOW_CMD_NEW,
1038 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1039 .policy = flow_policy,
1040 .doit = ovs_flow_cmd_new_or_set
1042 { .cmd = OVS_FLOW_CMD_DEL,
1043 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1044 .policy = flow_policy,
1045 .doit = ovs_flow_cmd_del
1047 { .cmd = OVS_FLOW_CMD_GET,
1048 .flags = 0, /* OK for unprivileged users. */
1049 .policy = flow_policy,
1050 .doit = ovs_flow_cmd_get,
1051 .dumpit = ovs_flow_cmd_dump
1053 { .cmd = OVS_FLOW_CMD_SET,
1054 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1055 .policy = flow_policy,
1056 .doit = ovs_flow_cmd_new_or_set,
1060 static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1061 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1062 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1063 [OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 },
1066 static struct genl_family dp_datapath_genl_family = {
1067 .id = GENL_ID_GENERATE,
1068 .hdrsize = sizeof(struct ovs_header),
1069 .name = OVS_DATAPATH_FAMILY,
1070 .version = OVS_DATAPATH_VERSION,
1071 .maxattr = OVS_DP_ATTR_MAX,
1076 static struct genl_multicast_group ovs_dp_datapath_multicast_group = {
1077 .name = OVS_DATAPATH_MCGROUP
1080 static size_t ovs_dp_cmd_msg_size(void)
1082 size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
1084 msgsize += nla_total_size(IFNAMSIZ);
1085 msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
1086 msgsize += nla_total_size(sizeof(struct ovs_dp_megaflow_stats));
1091 static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
1092 u32 portid, u32 seq, u32 flags, u8 cmd)
1094 struct ovs_header *ovs_header;
1095 struct ovs_dp_stats dp_stats;
1096 struct ovs_dp_megaflow_stats dp_megaflow_stats;
1099 ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
1104 ovs_header->dp_ifindex = get_dpifindex(dp);
1107 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
1110 goto nla_put_failure;
1112 get_dp_stats(dp, &dp_stats, &dp_megaflow_stats);
1113 if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats),
1115 goto nla_put_failure;
1117 if (nla_put(skb, OVS_DP_ATTR_MEGAFLOW_STATS,
1118 sizeof(struct ovs_dp_megaflow_stats),
1119 &dp_megaflow_stats))
1120 goto nla_put_failure;
1122 if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, dp->user_features))
1123 goto nla_put_failure;
1125 return genlmsg_end(skb, ovs_header);
1128 genlmsg_cancel(skb, ovs_header);
1133 static struct sk_buff *ovs_dp_cmd_build_info(struct datapath *dp,
1134 struct genl_info *info, u8 cmd)
1136 struct sk_buff *skb;
1139 skb = genlmsg_new_unicast(ovs_dp_cmd_msg_size(), info, GFP_KERNEL);
1141 return ERR_PTR(-ENOMEM);
1143 retval = ovs_dp_cmd_fill_info(dp, skb, info->snd_portid, info->snd_seq, 0, cmd);
1146 return ERR_PTR(retval);
1151 /* Called with ovs_mutex. */
1152 static struct datapath *lookup_datapath(struct net *net,
1153 struct ovs_header *ovs_header,
1154 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1156 struct datapath *dp;
1158 if (!a[OVS_DP_ATTR_NAME])
1159 dp = get_dp(net, ovs_header->dp_ifindex);
1161 struct vport *vport;
1164 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
1165 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
1168 return dp ? dp : ERR_PTR(-ENODEV);
1171 static void ovs_dp_reset_user_features(struct sk_buff *skb, struct genl_info *info)
1173 struct datapath *dp;
1175 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1179 WARN(dp->user_features, "Dropping previously announced user features\n");
1180 dp->user_features = 0;
1183 static void ovs_dp_change(struct datapath *dp, struct nlattr **a)
1185 if (a[OVS_DP_ATTR_USER_FEATURES])
1186 dp->user_features = nla_get_u32(a[OVS_DP_ATTR_USER_FEATURES]);
1189 static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1191 struct nlattr **a = info->attrs;
1192 struct vport_parms parms;
1193 struct sk_buff *reply;
1194 struct datapath *dp;
1195 struct vport *vport;
1196 struct ovs_net *ovs_net;
1200 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1206 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1208 goto err_unlock_ovs;
1210 ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
1212 /* Allocate table. */
1213 err = ovs_flow_tbl_init(&dp->table);
1217 dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
1218 if (!dp->stats_percpu) {
1220 goto err_destroy_table;
1223 dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
1227 goto err_destroy_percpu;
1230 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1231 INIT_HLIST_HEAD(&dp->ports[i]);
1233 /* Set up our datapath device. */
1234 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1235 parms.type = OVS_VPORT_TYPE_INTERNAL;
1236 parms.options = NULL;
1238 parms.port_no = OVSP_LOCAL;
1239 parms.upcall_portid = nla_get_u32(a[OVS_DP_ATTR_UPCALL_PID]);
1241 ovs_dp_change(dp, a);
1243 vport = new_vport(&parms);
1244 if (IS_ERR(vport)) {
1245 err = PTR_ERR(vport);
1249 if (err == -EEXIST) {
1250 /* An outdated user space instance that does not understand
1251 * the concept of user_features has attempted to create a new
1252 * datapath and is likely to reuse it. Drop all user features.
1254 if (info->genlhdr->version < OVS_DP_VER_FEATURES)
1255 ovs_dp_reset_user_features(skb, info);
1258 goto err_destroy_ports_array;
1261 reply = ovs_dp_cmd_build_info(dp, info, OVS_DP_CMD_NEW);
1262 err = PTR_ERR(reply);
1264 goto err_destroy_local_port;
1266 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
1267 list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
1271 ovs_notify(reply, info, &ovs_dp_datapath_multicast_group);
1274 err_destroy_local_port:
1275 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
1276 err_destroy_ports_array:
1279 free_percpu(dp->stats_percpu);
1281 ovs_flow_tbl_destroy(&dp->table);
1283 release_net(ovs_dp_get_net(dp));
1291 /* Called with ovs_mutex. */
1292 static void __dp_destroy(struct datapath *dp)
1296 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1297 struct vport *vport;
1298 struct hlist_node *n;
1300 hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
1301 if (vport->port_no != OVSP_LOCAL)
1302 ovs_dp_detach_port(vport);
1305 list_del_rcu(&dp->list_node);
1307 /* OVSP_LOCAL is datapath internal port. We need to make sure that
1308 * all port in datapath are destroyed first before freeing datapath.
1310 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
1312 call_rcu(&dp->rcu, destroy_dp_rcu);
1315 static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1317 struct sk_buff *reply;
1318 struct datapath *dp;
1322 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1327 reply = ovs_dp_cmd_build_info(dp, info, OVS_DP_CMD_DEL);
1328 err = PTR_ERR(reply);
1335 ovs_notify(reply, info, &ovs_dp_datapath_multicast_group);
1343 static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1345 struct sk_buff *reply;
1346 struct datapath *dp;
1350 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1355 ovs_dp_change(dp, info->attrs);
1357 reply = ovs_dp_cmd_build_info(dp, info, OVS_DP_CMD_NEW);
1358 if (IS_ERR(reply)) {
1359 err = PTR_ERR(reply);
1360 netlink_set_err(sock_net(skb->sk)->genl_sock, 0,
1361 ovs_dp_datapath_multicast_group.id, err);
1367 ovs_notify(reply, info, &ovs_dp_datapath_multicast_group);
1375 static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1377 struct sk_buff *reply;
1378 struct datapath *dp;
1382 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1388 reply = ovs_dp_cmd_build_info(dp, info, OVS_DP_CMD_NEW);
1389 if (IS_ERR(reply)) {
1390 err = PTR_ERR(reply);
1395 return genlmsg_reply(reply, info);
1402 static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1404 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
1405 struct datapath *dp;
1406 int skip = cb->args[0];
1410 list_for_each_entry_rcu(dp, &ovs_net->dps, list_node) {
1412 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
1413 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1414 OVS_DP_CMD_NEW) < 0)
1425 static struct genl_ops dp_datapath_genl_ops[] = {
1426 { .cmd = OVS_DP_CMD_NEW,
1427 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1428 .policy = datapath_policy,
1429 .doit = ovs_dp_cmd_new
1431 { .cmd = OVS_DP_CMD_DEL,
1432 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1433 .policy = datapath_policy,
1434 .doit = ovs_dp_cmd_del
1436 { .cmd = OVS_DP_CMD_GET,
1437 .flags = 0, /* OK for unprivileged users. */
1438 .policy = datapath_policy,
1439 .doit = ovs_dp_cmd_get,
1440 .dumpit = ovs_dp_cmd_dump
1442 { .cmd = OVS_DP_CMD_SET,
1443 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1444 .policy = datapath_policy,
1445 .doit = ovs_dp_cmd_set,
1449 static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
1450 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1451 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
1452 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
1453 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
1454 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1455 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
1458 static struct genl_family dp_vport_genl_family = {
1459 .id = GENL_ID_GENERATE,
1460 .hdrsize = sizeof(struct ovs_header),
1461 .name = OVS_VPORT_FAMILY,
1462 .version = OVS_VPORT_VERSION,
1463 .maxattr = OVS_VPORT_ATTR_MAX,
1468 struct genl_multicast_group ovs_dp_vport_multicast_group = {
1469 .name = OVS_VPORT_MCGROUP
1472 /* Called with ovs_mutex or RCU read lock. */
1473 static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
1474 u32 portid, u32 seq, u32 flags, u8 cmd)
1476 struct ovs_header *ovs_header;
1477 struct ovs_vport_stats vport_stats;
1480 ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
1485 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1487 if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1488 nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
1489 nla_put_string(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport)) ||
1490 nla_put_u32(skb, OVS_VPORT_ATTR_UPCALL_PID, vport->upcall_portid))
1491 goto nla_put_failure;
1493 ovs_vport_get_stats(vport, &vport_stats);
1494 if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
1496 goto nla_put_failure;
1498 err = ovs_vport_get_options(vport, skb);
1499 if (err == -EMSGSIZE)
1502 return genlmsg_end(skb, ovs_header);
1507 genlmsg_cancel(skb, ovs_header);
1511 /* Called with ovs_mutex or RCU read lock. */
1512 struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
1515 struct sk_buff *skb;
1518 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1520 return ERR_PTR(-ENOMEM);
1522 retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
1528 /* Called with ovs_mutex or RCU read lock. */
1529 static struct vport *lookup_vport(struct net *net,
1530 struct ovs_header *ovs_header,
1531 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1533 struct datapath *dp;
1534 struct vport *vport;
1536 if (a[OVS_VPORT_ATTR_NAME]) {
1537 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
1539 return ERR_PTR(-ENODEV);
1540 if (ovs_header->dp_ifindex &&
1541 ovs_header->dp_ifindex != get_dpifindex(vport->dp))
1542 return ERR_PTR(-ENODEV);
1544 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1545 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1547 if (port_no >= DP_MAX_PORTS)
1548 return ERR_PTR(-EFBIG);
1550 dp = get_dp(net, ovs_header->dp_ifindex);
1552 return ERR_PTR(-ENODEV);
1554 vport = ovs_vport_ovsl_rcu(dp, port_no);
1556 return ERR_PTR(-ENODEV);
1559 return ERR_PTR(-EINVAL);
1562 static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1564 struct nlattr **a = info->attrs;
1565 struct ovs_header *ovs_header = info->userhdr;
1566 struct vport_parms parms;
1567 struct sk_buff *reply;
1568 struct vport *vport;
1569 struct datapath *dp;
1574 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
1575 !a[OVS_VPORT_ATTR_UPCALL_PID])
1579 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1584 if (a[OVS_VPORT_ATTR_PORT_NO]) {
1585 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1588 if (port_no >= DP_MAX_PORTS)
1591 vport = ovs_vport_ovsl(dp, port_no);
1596 for (port_no = 1; ; port_no++) {
1597 if (port_no >= DP_MAX_PORTS) {
1601 vport = ovs_vport_ovsl(dp, port_no);
1607 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
1608 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1609 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
1611 parms.port_no = port_no;
1612 parms.upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
1614 vport = new_vport(&parms);
1615 err = PTR_ERR(vport);
1620 if (a[OVS_VPORT_ATTR_STATS])
1621 ovs_vport_set_stats(vport, nla_data(a[OVS_VPORT_ATTR_STATS]));
1623 reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq,
1625 if (IS_ERR(reply)) {
1626 err = PTR_ERR(reply);
1627 ovs_dp_detach_port(vport);
1631 ovs_notify(reply, info, &ovs_dp_vport_multicast_group);
1639 static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
1641 struct nlattr **a = info->attrs;
1642 struct sk_buff *reply;
1643 struct vport *vport;
1647 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
1648 err = PTR_ERR(vport);
1652 if (a[OVS_VPORT_ATTR_TYPE] &&
1653 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
1658 reply = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1664 if (a[OVS_VPORT_ATTR_OPTIONS]) {
1665 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
1670 if (a[OVS_VPORT_ATTR_STATS])
1671 ovs_vport_set_stats(vport, nla_data(a[OVS_VPORT_ATTR_STATS]));
1673 if (a[OVS_VPORT_ATTR_UPCALL_PID])
1674 vport->upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
1676 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1677 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1681 ovs_notify(reply, info, &ovs_dp_vport_multicast_group);
1691 static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
1693 struct nlattr **a = info->attrs;
1694 struct sk_buff *reply;
1695 struct vport *vport;
1699 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
1700 err = PTR_ERR(vport);
1704 if (vport->port_no == OVSP_LOCAL) {
1709 reply = ovs_vport_cmd_build_info(vport, info->snd_portid,
1710 info->snd_seq, OVS_VPORT_CMD_DEL);
1711 err = PTR_ERR(reply);
1716 ovs_dp_detach_port(vport);
1718 ovs_notify(reply, info, &ovs_dp_vport_multicast_group);
1725 static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
1727 struct nlattr **a = info->attrs;
1728 struct ovs_header *ovs_header = info->userhdr;
1729 struct sk_buff *reply;
1730 struct vport *vport;
1734 vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
1735 err = PTR_ERR(vport);
1739 reply = ovs_vport_cmd_build_info(vport, info->snd_portid,
1740 info->snd_seq, OVS_VPORT_CMD_NEW);
1741 err = PTR_ERR(reply);
1747 return genlmsg_reply(reply, info);
1754 static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1756 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1757 struct datapath *dp;
1758 int bucket = cb->args[0], skip = cb->args[1];
1761 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1766 for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
1767 struct vport *vport;
1770 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
1772 ovs_vport_cmd_fill_info(vport, skb,
1773 NETLINK_CB(cb->skb).portid,
1776 OVS_VPORT_CMD_NEW) < 0)
1792 static struct genl_ops dp_vport_genl_ops[] = {
1793 { .cmd = OVS_VPORT_CMD_NEW,
1794 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1795 .policy = vport_policy,
1796 .doit = ovs_vport_cmd_new
1798 { .cmd = OVS_VPORT_CMD_DEL,
1799 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1800 .policy = vport_policy,
1801 .doit = ovs_vport_cmd_del
1803 { .cmd = OVS_VPORT_CMD_GET,
1804 .flags = 0, /* OK for unprivileged users. */
1805 .policy = vport_policy,
1806 .doit = ovs_vport_cmd_get,
1807 .dumpit = ovs_vport_cmd_dump
1809 { .cmd = OVS_VPORT_CMD_SET,
1810 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1811 .policy = vport_policy,
1812 .doit = ovs_vport_cmd_set,
1816 struct genl_family_and_ops {
1817 struct genl_family *family;
1818 struct genl_ops *ops;
1820 struct genl_multicast_group *group;
1823 static const struct genl_family_and_ops dp_genl_families[] = {
1824 { &dp_datapath_genl_family,
1825 dp_datapath_genl_ops, ARRAY_SIZE(dp_datapath_genl_ops),
1826 &ovs_dp_datapath_multicast_group },
1827 { &dp_vport_genl_family,
1828 dp_vport_genl_ops, ARRAY_SIZE(dp_vport_genl_ops),
1829 &ovs_dp_vport_multicast_group },
1830 { &dp_flow_genl_family,
1831 dp_flow_genl_ops, ARRAY_SIZE(dp_flow_genl_ops),
1832 &ovs_dp_flow_multicast_group },
1833 { &dp_packet_genl_family,
1834 dp_packet_genl_ops, ARRAY_SIZE(dp_packet_genl_ops),
1838 static void dp_unregister_genl(int n_families)
1842 for (i = 0; i < n_families; i++)
1843 genl_unregister_family(dp_genl_families[i].family);
1846 static int dp_register_genl(void)
1853 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
1854 const struct genl_family_and_ops *f = &dp_genl_families[i];
1856 err = genl_register_family_with_ops(f->family, f->ops,
1863 err = genl_register_mc_group(f->family, f->group);
1872 dp_unregister_genl(n_registered);
1876 static int __net_init ovs_init_net(struct net *net)
1878 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
1880 INIT_LIST_HEAD(&ovs_net->dps);
1881 INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
1885 static void __net_exit ovs_exit_net(struct net *net)
1887 struct datapath *dp, *dp_next;
1888 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
1891 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
1895 cancel_work_sync(&ovs_net->dp_notify_work);
1898 static struct pernet_operations ovs_net_ops = {
1899 .init = ovs_init_net,
1900 .exit = ovs_exit_net,
1902 .size = sizeof(struct ovs_net),
1905 DEFINE_COMPAT_PNET_REG_FUNC(device);
1907 static int __init dp_init(void)
1911 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
1913 pr_info("Open vSwitch switching datapath %s, built "__DATE__" "__TIME__"\n",
1916 err = ovs_flow_init();
1920 err = ovs_vport_init();
1922 goto error_flow_exit;
1924 err = register_pernet_device(&ovs_net_ops);
1926 goto error_vport_exit;
1928 err = register_netdevice_notifier(&ovs_dp_device_notifier);
1930 goto error_netns_exit;
1932 err = dp_register_genl();
1934 goto error_unreg_notifier;
1938 error_unreg_notifier:
1939 unregister_netdevice_notifier(&ovs_dp_device_notifier);
1941 unregister_pernet_device(&ovs_net_ops);
1950 static void dp_cleanup(void)
1952 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
1953 unregister_netdevice_notifier(&ovs_dp_device_notifier);
1954 unregister_pernet_device(&ovs_net_ops);
1960 module_init(dp_init);
1961 module_exit(dp_cleanup);
1963 MODULE_DESCRIPTION("Open vSwitch switching datapath");
1964 MODULE_LICENSE("GPL");
1965 MODULE_VERSION(VERSION);