2 * Copyright (c) 2007-2011 Nicira Networks.
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/system.h>
43 #include <asm/div64.h>
44 #include <linux/highmem.h>
45 #include <linux/netfilter_bridge.h>
46 #include <linux/netfilter_ipv4.h>
47 #include <linux/inetdevice.h>
48 #include <linux/list.h>
49 #include <linux/openvswitch.h>
50 #include <linux/rculist.h>
51 #include <linux/dmi.h>
52 #include <net/genetlink.h>
59 #include "vport-internal_dev.h"
61 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18) || \
62 LINUX_VERSION_CODE > KERNEL_VERSION(3,2,0)
63 #error Kernels before 2.6.18 or after 3.2 are not supported by this version of Open vSwitch.
66 int (*dp_ioctl_hook)(struct net_device *dev, struct ifreq *rq, int cmd);
67 EXPORT_SYMBOL(dp_ioctl_hook);
72 * Writes to device state (add/remove datapath, port, set operations on vports,
73 * etc.) are protected by RTNL.
75 * Writes to other state (flow table modifications, set miscellaneous datapath
76 * parameters, etc.) are protected by genl_mutex. The RTNL lock nests inside
79 * Reads are protected by RCU.
81 * There are a few special cases (mostly stats) that have their own
82 * synchronization but they nest under all of above and don't interact with
86 /* Global list of datapaths to enable dumping them all out.
87 * Protected by genl_mutex.
89 static LIST_HEAD(dps);
91 static struct vport *new_vport(const struct vport_parms *);
92 static int queue_gso_packets(int dp_ifindex, struct sk_buff *,
93 const struct dp_upcall_info *);
94 static int queue_userspace_packet(int dp_ifindex, struct sk_buff *,
95 const struct dp_upcall_info *);
97 /* Must be called with rcu_read_lock, genl_mutex, or RTNL lock. */
98 struct datapath *get_dp(int dp_ifindex)
100 struct datapath *dp = NULL;
101 struct net_device *dev;
104 dev = dev_get_by_index_rcu(&init_net, dp_ifindex);
106 struct vport *vport = internal_dev_get_vport(dev);
114 EXPORT_SYMBOL_GPL(get_dp);
116 /* Must be called with genl_mutex. */
117 static struct flow_table *get_table_protected(struct datapath *dp)
119 return rcu_dereference_protected(dp->table, lockdep_genl_is_held());
122 /* Must be called with rcu_read_lock or RTNL lock. */
123 static struct vport *get_vport_protected(struct datapath *dp, u16 port_no)
125 return rcu_dereference_rtnl(dp->ports[port_no]);
128 /* Must be called with rcu_read_lock or RTNL lock. */
129 const char *dp_name(const struct datapath *dp)
131 struct vport *vport = rcu_dereference_rtnl(dp->ports[OVSP_LOCAL]);
132 return vport->ops->get_name(vport);
135 static int get_dpifindex(struct datapath *dp)
142 local = get_vport_protected(dp, OVSP_LOCAL);
144 ifindex = local->ops->get_ifindex(local);
153 static size_t br_nlmsg_size(void)
155 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
156 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
157 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
158 + nla_total_size(4) /* IFLA_MASTER */
159 + nla_total_size(4) /* IFLA_MTU */
160 + nla_total_size(1); /* IFLA_OPERSTATE */
163 /* Caller must hold RTNL lock. */
164 static int dp_fill_ifinfo(struct sk_buff *skb,
165 const struct vport *port,
166 int event, unsigned int flags)
168 struct datapath *dp = port->dp;
169 struct ifinfomsg *hdr;
170 struct nlmsghdr *nlh;
172 if (!port->ops->get_ifindex)
175 nlh = nlmsg_put(skb, 0, 0, event, sizeof(*hdr), flags);
179 hdr = nlmsg_data(nlh);
180 hdr->ifi_family = AF_BRIDGE;
182 hdr->ifi_type = ARPHRD_ETHER;
183 hdr->ifi_index = port->ops->get_ifindex(port);
184 hdr->ifi_flags = port->ops->get_dev_flags(port);
187 NLA_PUT_STRING(skb, IFLA_IFNAME, port->ops->get_name(port));
188 NLA_PUT_U32(skb, IFLA_MASTER, get_dpifindex(dp));
189 NLA_PUT_U32(skb, IFLA_MTU, port->ops->get_mtu(port));
190 #ifdef IFLA_OPERSTATE
191 NLA_PUT_U8(skb, IFLA_OPERSTATE,
192 port->ops->is_running(port)
193 ? port->ops->get_operstate(port)
197 NLA_PUT(skb, IFLA_ADDRESS, ETH_ALEN, port->ops->get_addr(port));
199 return nlmsg_end(skb, nlh);
202 nlmsg_cancel(skb, nlh);
206 /* Caller must hold RTNL lock. */
207 static void dp_ifinfo_notify(int event, struct vport *port)
212 skb = nlmsg_new(br_nlmsg_size(), GFP_KERNEL);
218 err = dp_fill_ifinfo(skb, port, event, 0);
220 if (err == -ENODEV) {
223 /* -EMSGSIZE implies BUG in br_nlmsg_size() */
224 WARN_ON(err == -EMSGSIZE);
229 rtnl_notify(skb, &init_net, 0, RTNLGRP_LINK, NULL, GFP_KERNEL);
233 rtnl_set_sk_err(&init_net, RTNLGRP_LINK, err);
238 static void release_dp(struct kobject *kobj)
240 struct datapath *dp = container_of(kobj, struct datapath, ifobj);
244 static struct kobj_type dp_ktype = {
245 .release = release_dp
248 static void destroy_dp_rcu(struct rcu_head *rcu)
250 struct datapath *dp = container_of(rcu, struct datapath, rcu);
252 flow_tbl_destroy((__force struct flow_table *)dp->table);
253 free_percpu(dp->stats_percpu);
254 kobject_put(&dp->ifobj);
257 /* Called with RTNL lock and genl_lock. */
258 static struct vport *new_vport(const struct vport_parms *parms)
262 vport = vport_add(parms);
263 if (!IS_ERR(vport)) {
264 struct datapath *dp = parms->dp;
266 rcu_assign_pointer(dp->ports[parms->port_no], vport);
267 list_add(&vport->node, &dp->port_list);
269 dp_ifinfo_notify(RTM_NEWLINK, vport);
275 /* Called with RTNL lock. */
276 void dp_detach_port(struct vport *p)
280 if (p->port_no != OVSP_LOCAL)
282 dp_ifinfo_notify(RTM_DELLINK, p);
284 /* First drop references to device. */
286 rcu_assign_pointer(p->dp->ports[p->port_no], NULL);
288 /* Then destroy it. */
292 /* Must be called with rcu_read_lock. */
293 void dp_process_received_packet(struct vport *p, struct sk_buff *skb)
295 struct datapath *dp = p->dp;
296 struct sw_flow *flow;
297 struct dp_stats_percpu *stats;
301 stats = per_cpu_ptr(dp->stats_percpu, smp_processor_id());
303 if (!OVS_CB(skb)->flow) {
304 struct sw_flow_key key;
307 /* Extract flow from 'skb' into 'key'. */
308 error = flow_extract(skb, p->port_no, &key, &key_len);
309 if (unlikely(error)) {
315 flow = flow_tbl_lookup(rcu_dereference(dp->table),
317 if (unlikely(!flow)) {
318 struct dp_upcall_info upcall;
320 upcall.cmd = OVS_PACKET_CMD_MISS;
322 upcall.userdata = NULL;
323 upcall.pid = p->upcall_pid;
324 dp_upcall(dp, skb, &upcall);
326 stats_counter = &stats->n_missed;
330 OVS_CB(skb)->flow = flow;
333 stats_counter = &stats->n_hit;
334 flow_used(OVS_CB(skb)->flow, skb);
335 execute_actions(dp, skb);
338 /* Update datapath statistics. */
340 write_seqcount_begin(&stats->seqlock);
342 write_seqcount_end(&stats->seqlock);
345 static struct genl_family dp_packet_genl_family = {
346 .id = GENL_ID_GENERATE,
347 .hdrsize = sizeof(struct ovs_header),
348 .name = OVS_PACKET_FAMILY,
349 .version = OVS_PACKET_VERSION,
350 .maxattr = OVS_PACKET_ATTR_MAX
353 int dp_upcall(struct datapath *dp, struct sk_buff *skb,
354 const struct dp_upcall_info *upcall_info)
356 struct dp_stats_percpu *stats;
360 if (upcall_info->pid == 0) {
365 dp_ifindex = get_dpifindex(dp);
371 forward_ip_summed(skb, true);
373 if (!skb_is_gso(skb))
374 err = queue_userspace_packet(dp_ifindex, skb, upcall_info);
376 err = queue_gso_packets(dp_ifindex, skb, upcall_info);
383 stats = per_cpu_ptr(dp->stats_percpu, smp_processor_id());
385 write_seqcount_begin(&stats->seqlock);
387 write_seqcount_end(&stats->seqlock);
392 static int queue_gso_packets(int dp_ifindex, struct sk_buff *skb,
393 const struct dp_upcall_info *upcall_info)
395 struct dp_upcall_info later_info;
396 struct sw_flow_key later_key;
397 struct sk_buff *segs, *nskb;
400 segs = skb_gso_segment(skb, NETIF_F_SG | NETIF_F_HW_CSUM);
404 /* Queue all of the segments. */
407 err = queue_userspace_packet(dp_ifindex, skb, upcall_info);
411 if (skb == segs && skb_shinfo(skb)->gso_type & SKB_GSO_UDP) {
412 /* The initial flow key extracted by flow_extract() in
413 * this case is for a first fragment, so we need to
414 * properly mark later fragments.
416 later_key = *upcall_info->key;
417 later_key.ip.frag = OVS_FRAG_TYPE_LATER;
419 later_info = *upcall_info;
420 later_info.key = &later_key;
421 upcall_info = &later_info;
423 } while ((skb = skb->next));
425 /* Free all of the segments. */
433 } while ((skb = nskb));
437 static int queue_userspace_packet(int dp_ifindex, struct sk_buff *skb,
438 const struct dp_upcall_info *upcall_info)
440 struct ovs_header *upcall;
441 struct sk_buff *user_skb; /* to be queued to userspace */
446 err = vlan_deaccel_tag(skb);
450 if (nla_attr_size(skb->len) > USHRT_MAX)
453 len = sizeof(struct ovs_header);
454 len += nla_total_size(skb->len);
455 len += nla_total_size(FLOW_BUFSIZE);
456 if (upcall_info->cmd == OVS_PACKET_CMD_ACTION)
457 len += nla_total_size(8);
459 user_skb = genlmsg_new(len, GFP_ATOMIC);
463 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
464 0, upcall_info->cmd);
465 upcall->dp_ifindex = dp_ifindex;
467 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
468 flow_to_nlattrs(upcall_info->key, user_skb);
469 nla_nest_end(user_skb, nla);
471 if (upcall_info->userdata)
472 nla_put_u64(user_skb, OVS_PACKET_ATTR_USERDATA,
473 nla_get_u64(upcall_info->userdata));
475 nla = __nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, skb->len);
477 skb_copy_and_csum_dev(skb, nla_data(nla));
479 return genlmsg_unicast(&init_net, user_skb, upcall_info->pid);
482 /* Called with genl_mutex. */
483 static int flush_flows(int dp_ifindex)
485 struct flow_table *old_table;
486 struct flow_table *new_table;
489 dp = get_dp(dp_ifindex);
493 old_table = get_table_protected(dp);
494 new_table = flow_tbl_alloc(TBL_MIN_BUCKETS);
498 rcu_assign_pointer(dp->table, new_table);
500 flow_tbl_deferred_destroy(old_table);
504 static int validate_actions(const struct nlattr *attr,
505 const struct sw_flow_key *key, int depth);
507 static int validate_sample(const struct nlattr *attr,
508 const struct sw_flow_key *key, int depth)
510 const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
511 const struct nlattr *probability, *actions;
512 const struct nlattr *a;
515 memset(attrs, 0, sizeof(attrs));
516 nla_for_each_nested(a, attr, rem) {
517 int type = nla_type(a);
518 if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
525 probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
526 if (!probability || nla_len(probability) != sizeof(u32))
529 actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
530 if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
532 return validate_actions(actions, key, depth + 1);
535 static int validate_set(const struct nlattr *a,
536 const struct sw_flow_key *flow_key)
538 const struct nlattr *ovs_key = nla_data(a);
539 int key_type = nla_type(ovs_key);
541 /* There can be only one key in a action */
542 if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
545 if (key_type > OVS_KEY_ATTR_MAX ||
546 nla_len(ovs_key) != ovs_key_lens[key_type])
550 const struct ovs_key_ipv4 *ipv4_key;
552 case OVS_KEY_ATTR_PRIORITY:
553 case OVS_KEY_ATTR_TUN_ID:
554 case OVS_KEY_ATTR_ETHERNET:
557 case OVS_KEY_ATTR_IPV4:
558 if (flow_key->eth.type != htons(ETH_P_IP))
561 if (!flow_key->ipv4.addr.src || !flow_key->ipv4.addr.dst)
564 ipv4_key = nla_data(ovs_key);
565 if (ipv4_key->ipv4_proto != flow_key->ip.proto)
568 if (ipv4_key->ipv4_frag != flow_key->ip.frag)
573 case OVS_KEY_ATTR_TCP:
574 if (flow_key->ip.proto != IPPROTO_TCP)
577 if (!flow_key->ipv4.tp.src || !flow_key->ipv4.tp.dst)
582 case OVS_KEY_ATTR_UDP:
583 if (flow_key->ip.proto != IPPROTO_UDP)
586 if (!flow_key->ipv4.tp.src || !flow_key->ipv4.tp.dst)
597 static int validate_userspace(const struct nlattr *attr)
599 static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
600 [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
601 [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_U64 },
603 struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
606 error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
607 attr, userspace_policy);
611 if (!a[OVS_USERSPACE_ATTR_PID] ||
612 !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
618 static int validate_actions(const struct nlattr *attr,
619 const struct sw_flow_key *key, int depth)
621 const struct nlattr *a;
624 if (depth >= SAMPLE_ACTION_DEPTH)
627 nla_for_each_nested(a, attr, rem) {
628 /* Expected argument lengths, (u32)-1 for variable length. */
629 static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
630 [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
631 [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
632 [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
633 [OVS_ACTION_ATTR_POP_VLAN] = 0,
634 [OVS_ACTION_ATTR_SET] = (u32)-1,
635 [OVS_ACTION_ATTR_SAMPLE] = (u32)-1
637 const struct ovs_action_push_vlan *vlan;
638 int type = nla_type(a);
640 if (type > OVS_ACTION_ATTR_MAX ||
641 (action_lens[type] != nla_len(a) &&
642 action_lens[type] != (u32)-1))
646 case OVS_ACTION_ATTR_UNSPEC:
649 case OVS_ACTION_ATTR_USERSPACE:
650 err = validate_userspace(a);
655 case OVS_ACTION_ATTR_OUTPUT:
656 if (nla_get_u32(a) >= DP_MAX_PORTS)
661 case OVS_ACTION_ATTR_POP_VLAN:
664 case OVS_ACTION_ATTR_PUSH_VLAN:
666 if (vlan->vlan_tpid != htons(ETH_P_8021Q))
668 if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
672 case OVS_ACTION_ATTR_SET:
673 err = validate_set(a, key);
678 case OVS_ACTION_ATTR_SAMPLE:
679 err = validate_sample(a, key, depth);
695 static void clear_stats(struct sw_flow *flow)
699 flow->packet_count = 0;
700 flow->byte_count = 0;
703 static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
705 struct ovs_header *ovs_header = info->userhdr;
706 struct nlattr **a = info->attrs;
707 struct sw_flow_actions *acts;
708 struct sk_buff *packet;
709 struct sw_flow *flow;
717 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
718 !a[OVS_PACKET_ATTR_ACTIONS] ||
719 nla_len(a[OVS_PACKET_ATTR_PACKET]) < ETH_HLEN)
722 len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
723 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
727 skb_reserve(packet, NET_IP_ALIGN);
729 memcpy(__skb_put(packet, len), nla_data(a[OVS_PACKET_ATTR_PACKET]), len);
731 skb_reset_mac_header(packet);
732 eth = eth_hdr(packet);
734 /* Normally, setting the skb 'protocol' field would be handled by a
735 * call to eth_type_trans(), but it assumes there's a sending
736 * device, which we may not have. */
737 if (ntohs(eth->h_proto) >= 1536)
738 packet->protocol = eth->h_proto;
740 packet->protocol = htons(ETH_P_802_2);
742 /* Build an sw_flow for sending this packet. */
748 err = flow_extract(packet, -1, &flow->key, &key_len);
752 err = flow_metadata_from_nlattrs(&flow->key.phy.priority,
753 &flow->key.phy.in_port,
754 &flow->key.phy.tun_id,
755 a[OVS_PACKET_ATTR_KEY]);
759 err = validate_actions(a[OVS_PACKET_ATTR_ACTIONS], &flow->key, 0);
763 flow->hash = flow_hash(&flow->key, key_len);
765 acts = flow_actions_alloc(a[OVS_PACKET_ATTR_ACTIONS]);
769 rcu_assign_pointer(flow->sf_acts, acts);
771 OVS_CB(packet)->flow = flow;
772 packet->priority = flow->key.phy.priority;
775 dp = get_dp(ovs_header->dp_ifindex);
781 err = execute_actions(dp, packet);
798 static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
799 [OVS_PACKET_ATTR_PACKET] = { .type = NLA_UNSPEC },
800 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
801 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
804 static struct genl_ops dp_packet_genl_ops[] = {
805 { .cmd = OVS_PACKET_CMD_EXECUTE,
806 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
807 .policy = packet_policy,
808 .doit = ovs_packet_cmd_execute
812 static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats)
815 struct flow_table *table = get_table_protected(dp);
817 stats->n_flows = flow_tbl_count(table);
819 stats->n_hit = stats->n_missed = stats->n_lost = 0;
820 for_each_possible_cpu(i) {
821 const struct dp_stats_percpu *percpu_stats;
822 struct dp_stats_percpu local_stats;
825 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
828 seqcount = read_seqcount_begin(&percpu_stats->seqlock);
829 local_stats = *percpu_stats;
830 } while (read_seqcount_retry(&percpu_stats->seqlock, seqcount));
832 stats->n_hit += local_stats.n_hit;
833 stats->n_missed += local_stats.n_missed;
834 stats->n_lost += local_stats.n_lost;
838 static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
839 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
840 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
841 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
844 static struct genl_family dp_flow_genl_family = {
845 .id = GENL_ID_GENERATE,
846 .hdrsize = sizeof(struct ovs_header),
847 .name = OVS_FLOW_FAMILY,
848 .version = OVS_FLOW_VERSION,
849 .maxattr = OVS_FLOW_ATTR_MAX
852 static struct genl_multicast_group dp_flow_multicast_group = {
853 .name = OVS_FLOW_MCGROUP
856 /* Called with genl_lock. */
857 static int ovs_flow_cmd_fill_info(struct sw_flow *flow, struct datapath *dp,
858 struct sk_buff *skb, u32 pid,
859 u32 seq, u32 flags, u8 cmd)
861 const int skb_orig_len = skb->len;
862 const struct sw_flow_actions *sf_acts;
863 struct ovs_flow_stats stats;
864 struct ovs_header *ovs_header;
870 sf_acts = rcu_dereference_protected(flow->sf_acts,
871 lockdep_genl_is_held());
873 ovs_header = genlmsg_put(skb, pid, seq, &dp_flow_genl_family, flags, cmd);
877 ovs_header->dp_ifindex = get_dpifindex(dp);
879 nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
881 goto nla_put_failure;
882 err = flow_to_nlattrs(&flow->key, skb);
885 nla_nest_end(skb, nla);
887 spin_lock_bh(&flow->lock);
889 stats.n_packets = flow->packet_count;
890 stats.n_bytes = flow->byte_count;
891 tcp_flags = flow->tcp_flags;
892 spin_unlock_bh(&flow->lock);
895 NLA_PUT_U64(skb, OVS_FLOW_ATTR_USED, flow_used_time(used));
898 NLA_PUT(skb, OVS_FLOW_ATTR_STATS,
899 sizeof(struct ovs_flow_stats), &stats);
902 NLA_PUT_U8(skb, OVS_FLOW_ATTR_TCP_FLAGS, tcp_flags);
904 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
905 * this is the first flow to be dumped into 'skb'. This is unusual for
906 * Netlink but individual action lists can be longer than
907 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
908 * The userspace caller can always fetch the actions separately if it
909 * really wants them. (Most userspace callers in fact don't care.)
911 * This can only fail for dump operations because the skb is always
912 * properly sized for single flows.
914 err = nla_put(skb, OVS_FLOW_ATTR_ACTIONS, sf_acts->actions_len,
916 if (err < 0 && skb_orig_len)
919 return genlmsg_end(skb, ovs_header);
924 genlmsg_cancel(skb, ovs_header);
928 static struct sk_buff *ovs_flow_cmd_alloc_info(struct sw_flow *flow)
930 const struct sw_flow_actions *sf_acts;
933 sf_acts = rcu_dereference_protected(flow->sf_acts,
934 lockdep_genl_is_held());
936 /* OVS_FLOW_ATTR_KEY */
937 len = nla_total_size(FLOW_BUFSIZE);
938 /* OVS_FLOW_ATTR_ACTIONS */
939 len += nla_total_size(sf_acts->actions_len);
940 /* OVS_FLOW_ATTR_STATS */
941 len += nla_total_size(sizeof(struct ovs_flow_stats));
942 /* OVS_FLOW_ATTR_TCP_FLAGS */
943 len += nla_total_size(1);
944 /* OVS_FLOW_ATTR_USED */
945 len += nla_total_size(8);
947 len += NLMSG_ALIGN(sizeof(struct ovs_header));
949 return genlmsg_new(len, GFP_KERNEL);
952 static struct sk_buff *ovs_flow_cmd_build_info(struct sw_flow *flow,
954 u32 pid, u32 seq, u8 cmd)
959 skb = ovs_flow_cmd_alloc_info(flow);
961 return ERR_PTR(-ENOMEM);
963 retval = ovs_flow_cmd_fill_info(flow, dp, skb, pid, seq, 0, cmd);
968 static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info)
970 struct nlattr **a = info->attrs;
971 struct ovs_header *ovs_header = info->userhdr;
972 struct sw_flow_key key;
973 struct sw_flow *flow;
974 struct sk_buff *reply;
976 struct flow_table *table;
982 if (!a[OVS_FLOW_ATTR_KEY])
984 error = flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
988 /* Validate actions. */
989 if (a[OVS_FLOW_ATTR_ACTIONS]) {
990 error = validate_actions(a[OVS_FLOW_ATTR_ACTIONS], &key, 0);
993 } else if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW) {
998 dp = get_dp(ovs_header->dp_ifindex);
1003 table = get_table_protected(dp);
1004 flow = flow_tbl_lookup(table, &key, key_len);
1006 struct sw_flow_actions *acts;
1008 /* Bail out if we're not allowed to create a new flow. */
1010 if (info->genlhdr->cmd == OVS_FLOW_CMD_SET)
1013 /* Expand table, if necessary, to make room. */
1014 if (flow_tbl_need_to_expand(table)) {
1015 struct flow_table *new_table;
1017 new_table = flow_tbl_expand(table);
1018 if (!IS_ERR(new_table)) {
1019 rcu_assign_pointer(dp->table, new_table);
1020 flow_tbl_deferred_destroy(table);
1021 table = get_table_protected(dp);
1025 /* Allocate flow. */
1026 flow = flow_alloc();
1028 error = PTR_ERR(flow);
1034 /* Obtain actions. */
1035 acts = flow_actions_alloc(a[OVS_FLOW_ATTR_ACTIONS]);
1036 error = PTR_ERR(acts);
1038 goto error_free_flow;
1039 rcu_assign_pointer(flow->sf_acts, acts);
1041 /* Put flow in bucket. */
1042 flow->hash = flow_hash(&key, key_len);
1043 flow_tbl_insert(table, flow);
1045 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_pid,
1049 /* We found a matching flow. */
1050 struct sw_flow_actions *old_acts;
1051 struct nlattr *acts_attrs;
1053 /* Bail out if we're not allowed to modify an existing flow.
1054 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
1055 * because Generic Netlink treats the latter as a dump
1056 * request. We also accept NLM_F_EXCL in case that bug ever
1060 if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW &&
1061 info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL))
1064 /* Update actions. */
1065 old_acts = rcu_dereference_protected(flow->sf_acts,
1066 lockdep_genl_is_held());
1067 acts_attrs = a[OVS_FLOW_ATTR_ACTIONS];
1069 (old_acts->actions_len != nla_len(acts_attrs) ||
1070 memcmp(old_acts->actions, nla_data(acts_attrs),
1071 old_acts->actions_len))) {
1072 struct sw_flow_actions *new_acts;
1074 new_acts = flow_actions_alloc(acts_attrs);
1075 error = PTR_ERR(new_acts);
1076 if (IS_ERR(new_acts))
1079 rcu_assign_pointer(flow->sf_acts, new_acts);
1080 flow_deferred_free_acts(old_acts);
1083 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_pid,
1084 info->snd_seq, OVS_FLOW_CMD_NEW);
1087 if (a[OVS_FLOW_ATTR_CLEAR]) {
1088 spin_lock_bh(&flow->lock);
1090 spin_unlock_bh(&flow->lock);
1095 genl_notify(reply, genl_info_net(info), info->snd_pid,
1096 dp_flow_multicast_group.id, info->nlhdr, GFP_KERNEL);
1098 netlink_set_err(INIT_NET_GENL_SOCK, 0,
1099 dp_flow_multicast_group.id, PTR_ERR(reply));
1108 static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1110 struct nlattr **a = info->attrs;
1111 struct ovs_header *ovs_header = info->userhdr;
1112 struct sw_flow_key key;
1113 struct sk_buff *reply;
1114 struct sw_flow *flow;
1115 struct datapath *dp;
1116 struct flow_table *table;
1120 if (!a[OVS_FLOW_ATTR_KEY])
1122 err = flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
1126 dp = get_dp(ovs_header->dp_ifindex);
1130 table = get_table_protected(dp);
1131 flow = flow_tbl_lookup(table, &key, key_len);
1135 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_pid,
1136 info->snd_seq, OVS_FLOW_CMD_NEW);
1138 return PTR_ERR(reply);
1140 return genlmsg_reply(reply, info);
1143 static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1145 struct nlattr **a = info->attrs;
1146 struct ovs_header *ovs_header = info->userhdr;
1147 struct sw_flow_key key;
1148 struct sk_buff *reply;
1149 struct sw_flow *flow;
1150 struct datapath *dp;
1151 struct flow_table *table;
1155 if (!a[OVS_FLOW_ATTR_KEY])
1156 return flush_flows(ovs_header->dp_ifindex);
1157 err = flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
1161 dp = get_dp(ovs_header->dp_ifindex);
1165 table = get_table_protected(dp);
1166 flow = flow_tbl_lookup(table, &key, key_len);
1170 reply = ovs_flow_cmd_alloc_info(flow);
1174 flow_tbl_remove(table, flow);
1176 err = ovs_flow_cmd_fill_info(flow, dp, reply, info->snd_pid,
1177 info->snd_seq, 0, OVS_FLOW_CMD_DEL);
1180 flow_deferred_free(flow);
1182 genl_notify(reply, genl_info_net(info), info->snd_pid,
1183 dp_flow_multicast_group.id, info->nlhdr, GFP_KERNEL);
1187 static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1189 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1190 struct datapath *dp;
1192 dp = get_dp(ovs_header->dp_ifindex);
1197 struct sw_flow *flow;
1200 bucket = cb->args[0];
1202 flow = flow_tbl_next(get_table_protected(dp), &bucket, &obj);
1206 if (ovs_flow_cmd_fill_info(flow, dp, skb,
1207 NETLINK_CB(cb->skb).pid,
1208 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1209 OVS_FLOW_CMD_NEW) < 0)
1212 cb->args[0] = bucket;
1218 static struct genl_ops dp_flow_genl_ops[] = {
1219 { .cmd = OVS_FLOW_CMD_NEW,
1220 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1221 .policy = flow_policy,
1222 .doit = ovs_flow_cmd_new_or_set
1224 { .cmd = OVS_FLOW_CMD_DEL,
1225 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1226 .policy = flow_policy,
1227 .doit = ovs_flow_cmd_del
1229 { .cmd = OVS_FLOW_CMD_GET,
1230 .flags = 0, /* OK for unprivileged users. */
1231 .policy = flow_policy,
1232 .doit = ovs_flow_cmd_get,
1233 .dumpit = ovs_flow_cmd_dump
1235 { .cmd = OVS_FLOW_CMD_SET,
1236 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1237 .policy = flow_policy,
1238 .doit = ovs_flow_cmd_new_or_set,
1242 static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1243 #ifdef HAVE_NLA_NUL_STRING
1244 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1246 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1249 static struct genl_family dp_datapath_genl_family = {
1250 .id = GENL_ID_GENERATE,
1251 .hdrsize = sizeof(struct ovs_header),
1252 .name = OVS_DATAPATH_FAMILY,
1253 .version = OVS_DATAPATH_VERSION,
1254 .maxattr = OVS_DP_ATTR_MAX
1257 static struct genl_multicast_group dp_datapath_multicast_group = {
1258 .name = OVS_DATAPATH_MCGROUP
1261 static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
1262 u32 pid, u32 seq, u32 flags, u8 cmd)
1264 struct ovs_header *ovs_header;
1268 ovs_header = genlmsg_put(skb, pid, seq, &dp_datapath_genl_family,
1273 ovs_header->dp_ifindex = get_dpifindex(dp);
1276 err = nla_put_string(skb, OVS_DP_ATTR_NAME, dp_name(dp));
1279 goto nla_put_failure;
1281 nla = nla_reserve(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats));
1283 goto nla_put_failure;
1284 get_dp_stats(dp, nla_data(nla));
1286 return genlmsg_end(skb, ovs_header);
1289 genlmsg_cancel(skb, ovs_header);
1294 static struct sk_buff *ovs_dp_cmd_build_info(struct datapath *dp, u32 pid,
1297 struct sk_buff *skb;
1300 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1302 return ERR_PTR(-ENOMEM);
1304 retval = ovs_dp_cmd_fill_info(dp, skb, pid, seq, 0, cmd);
1307 return ERR_PTR(retval);
1312 static int ovs_dp_cmd_validate(struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1314 return CHECK_NUL_STRING(a[OVS_DP_ATTR_NAME], IFNAMSIZ - 1);
1317 /* Called with genl_mutex and optionally with RTNL lock also. */
1318 static struct datapath *lookup_datapath(struct ovs_header *ovs_header,
1319 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1321 struct datapath *dp;
1323 if (!a[OVS_DP_ATTR_NAME])
1324 dp = get_dp(ovs_header->dp_ifindex);
1326 struct vport *vport;
1329 vport = vport_locate(nla_data(a[OVS_DP_ATTR_NAME]));
1330 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
1333 return dp ? dp : ERR_PTR(-ENODEV);
1336 static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1338 struct nlattr **a = info->attrs;
1339 struct vport_parms parms;
1340 struct sk_buff *reply;
1341 struct datapath *dp;
1342 struct vport *vport;
1346 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1349 err = ovs_dp_cmd_validate(a);
1355 if (!try_module_get(THIS_MODULE))
1356 goto err_unlock_rtnl;
1359 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1361 goto err_put_module;
1362 INIT_LIST_HEAD(&dp->port_list);
1364 /* Initialize kobject for bridge. This will be added as
1365 * /sys/class/net/<devname>/brif later, if sysfs is enabled. */
1366 dp->ifobj.kset = NULL;
1367 kobject_init(&dp->ifobj, &dp_ktype);
1369 /* Allocate table. */
1371 rcu_assign_pointer(dp->table, flow_tbl_alloc(TBL_MIN_BUCKETS));
1375 dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
1376 if (!dp->stats_percpu) {
1378 goto err_destroy_table;
1381 /* Set up our datapath device. */
1382 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1383 parms.type = OVS_VPORT_TYPE_INTERNAL;
1384 parms.options = NULL;
1386 parms.port_no = OVSP_LOCAL;
1387 parms.upcall_pid = nla_get_u32(a[OVS_DP_ATTR_UPCALL_PID]);
1389 vport = new_vport(&parms);
1390 if (IS_ERR(vport)) {
1391 err = PTR_ERR(vport);
1395 goto err_destroy_percpu;
1398 reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
1399 info->snd_seq, OVS_DP_CMD_NEW);
1400 err = PTR_ERR(reply);
1402 goto err_destroy_local_port;
1404 list_add_tail(&dp->list_node, &dps);
1405 dp_sysfs_add_dp(dp);
1409 genl_notify(reply, genl_info_net(info), info->snd_pid,
1410 dp_datapath_multicast_group.id, info->nlhdr, GFP_KERNEL);
1413 err_destroy_local_port:
1414 dp_detach_port(get_vport_protected(dp, OVSP_LOCAL));
1416 free_percpu(dp->stats_percpu);
1418 flow_tbl_destroy(get_table_protected(dp));
1422 module_put(THIS_MODULE);
1429 static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1431 struct vport *vport, *next_vport;
1432 struct sk_buff *reply;
1433 struct datapath *dp;
1436 err = ovs_dp_cmd_validate(info->attrs);
1441 dp = lookup_datapath(info->userhdr, info->attrs);
1446 reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
1447 info->snd_seq, OVS_DP_CMD_DEL);
1448 err = PTR_ERR(reply);
1452 list_for_each_entry_safe(vport, next_vport, &dp->port_list, node)
1453 if (vport->port_no != OVSP_LOCAL)
1454 dp_detach_port(vport);
1456 dp_sysfs_del_dp(dp);
1457 list_del(&dp->list_node);
1458 dp_detach_port(get_vport_protected(dp, OVSP_LOCAL));
1460 /* rtnl_unlock() will wait until all the references to devices that
1461 * are pending unregistration have been dropped. We do it here to
1462 * ensure that any internal devices (which contain DP pointers) are
1463 * fully destroyed before freeing the datapath.
1467 call_rcu(&dp->rcu, destroy_dp_rcu);
1468 module_put(THIS_MODULE);
1470 genl_notify(reply, genl_info_net(info), info->snd_pid,
1471 dp_datapath_multicast_group.id, info->nlhdr, GFP_KERNEL);
1481 static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1483 struct sk_buff *reply;
1484 struct datapath *dp;
1487 err = ovs_dp_cmd_validate(info->attrs);
1491 dp = lookup_datapath(info->userhdr, info->attrs);
1495 reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
1496 info->snd_seq, OVS_DP_CMD_NEW);
1497 if (IS_ERR(reply)) {
1498 err = PTR_ERR(reply);
1499 netlink_set_err(INIT_NET_GENL_SOCK, 0,
1500 dp_datapath_multicast_group.id, err);
1504 genl_notify(reply, genl_info_net(info), info->snd_pid,
1505 dp_datapath_multicast_group.id, info->nlhdr, GFP_KERNEL);
1509 static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1511 struct sk_buff *reply;
1512 struct datapath *dp;
1515 err = ovs_dp_cmd_validate(info->attrs);
1519 dp = lookup_datapath(info->userhdr, info->attrs);
1523 reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
1524 info->snd_seq, OVS_DP_CMD_NEW);
1526 return PTR_ERR(reply);
1528 return genlmsg_reply(reply, info);
1531 static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1533 struct datapath *dp;
1534 int skip = cb->args[0];
1537 list_for_each_entry(dp, &dps, list_node) {
1540 if (ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).pid,
1541 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1542 OVS_DP_CMD_NEW) < 0)
1552 static struct genl_ops dp_datapath_genl_ops[] = {
1553 { .cmd = OVS_DP_CMD_NEW,
1554 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1555 .policy = datapath_policy,
1556 .doit = ovs_dp_cmd_new
1558 { .cmd = OVS_DP_CMD_DEL,
1559 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1560 .policy = datapath_policy,
1561 .doit = ovs_dp_cmd_del
1563 { .cmd = OVS_DP_CMD_GET,
1564 .flags = 0, /* OK for unprivileged users. */
1565 .policy = datapath_policy,
1566 .doit = ovs_dp_cmd_get,
1567 .dumpit = ovs_dp_cmd_dump
1569 { .cmd = OVS_DP_CMD_SET,
1570 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1571 .policy = datapath_policy,
1572 .doit = ovs_dp_cmd_set,
1576 static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
1577 #ifdef HAVE_NLA_NUL_STRING
1578 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1579 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
1580 [OVS_VPORT_ATTR_ADDRESS] = { .len = ETH_ALEN },
1582 [OVS_VPORT_ATTR_STATS] = { .minlen = sizeof(struct ovs_vport_stats) },
1583 [OVS_VPORT_ATTR_ADDRESS] = { .minlen = ETH_ALEN },
1585 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
1586 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
1587 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1588 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
1591 static struct genl_family dp_vport_genl_family = {
1592 .id = GENL_ID_GENERATE,
1593 .hdrsize = sizeof(struct ovs_header),
1594 .name = OVS_VPORT_FAMILY,
1595 .version = OVS_VPORT_VERSION,
1596 .maxattr = OVS_VPORT_ATTR_MAX
1599 struct genl_multicast_group dp_vport_multicast_group = {
1600 .name = OVS_VPORT_MCGROUP
1603 /* Called with RTNL lock or RCU read lock. */
1604 static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
1605 u32 pid, u32 seq, u32 flags, u8 cmd)
1607 struct ovs_header *ovs_header;
1611 ovs_header = genlmsg_put(skb, pid, seq, &dp_vport_genl_family,
1616 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1618 NLA_PUT_U32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no);
1619 NLA_PUT_U32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type);
1620 NLA_PUT_STRING(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport));
1621 NLA_PUT_U32(skb, OVS_VPORT_ATTR_UPCALL_PID, vport->upcall_pid);
1623 nla = nla_reserve(skb, OVS_VPORT_ATTR_STATS,
1624 sizeof(struct ovs_vport_stats));
1626 goto nla_put_failure;
1628 vport_get_stats(vport, nla_data(nla));
1630 NLA_PUT(skb, OVS_VPORT_ATTR_ADDRESS, ETH_ALEN,
1631 vport->ops->get_addr(vport));
1633 err = vport_get_options(vport, skb);
1634 if (err == -EMSGSIZE)
1637 return genlmsg_end(skb, ovs_header);
1642 genlmsg_cancel(skb, ovs_header);
1646 /* Called with RTNL lock or RCU read lock. */
1647 struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 pid,
1650 struct sk_buff *skb;
1653 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1655 return ERR_PTR(-ENOMEM);
1657 retval = ovs_vport_cmd_fill_info(vport, skb, pid, seq, 0, cmd);
1660 return ERR_PTR(retval);
1665 static int ovs_vport_cmd_validate(struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1667 return CHECK_NUL_STRING(a[OVS_VPORT_ATTR_NAME], IFNAMSIZ - 1);
1670 /* Called with RTNL lock or RCU read lock. */
1671 static struct vport *lookup_vport(struct ovs_header *ovs_header,
1672 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1674 struct datapath *dp;
1675 struct vport *vport;
1677 if (a[OVS_VPORT_ATTR_NAME]) {
1678 vport = vport_locate(nla_data(a[OVS_VPORT_ATTR_NAME]));
1680 return ERR_PTR(-ENODEV);
1682 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1683 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1685 if (port_no >= DP_MAX_PORTS)
1686 return ERR_PTR(-EFBIG);
1688 dp = get_dp(ovs_header->dp_ifindex);
1690 return ERR_PTR(-ENODEV);
1692 vport = get_vport_protected(dp, port_no);
1694 return ERR_PTR(-ENOENT);
1697 return ERR_PTR(-EINVAL);
1700 /* Called with RTNL lock. */
1701 static int change_vport(struct vport *vport,
1702 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1706 if (a[OVS_VPORT_ATTR_STATS])
1707 vport_set_stats(vport, nla_data(a[OVS_VPORT_ATTR_STATS]));
1709 if (a[OVS_VPORT_ATTR_ADDRESS])
1710 err = vport_set_addr(vport, nla_data(a[OVS_VPORT_ATTR_ADDRESS]));
1715 static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1717 struct nlattr **a = info->attrs;
1718 struct ovs_header *ovs_header = info->userhdr;
1719 struct vport_parms parms;
1720 struct sk_buff *reply;
1721 struct vport *vport;
1722 struct datapath *dp;
1727 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
1728 !a[OVS_VPORT_ATTR_UPCALL_PID])
1731 err = ovs_vport_cmd_validate(a);
1736 dp = get_dp(ovs_header->dp_ifindex);
1741 if (a[OVS_VPORT_ATTR_PORT_NO]) {
1742 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1745 if (port_no >= DP_MAX_PORTS)
1748 vport = get_vport_protected(dp, port_no);
1753 for (port_no = 1; ; port_no++) {
1754 if (port_no >= DP_MAX_PORTS) {
1758 vport = get_vport_protected(dp, port_no);
1764 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
1765 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1766 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
1768 parms.port_no = port_no;
1769 parms.upcall_pid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
1771 vport = new_vport(&parms);
1772 err = PTR_ERR(vport);
1776 dp_sysfs_add_if(vport);
1778 err = change_vport(vport, a);
1780 reply = ovs_vport_cmd_build_info(vport, info->snd_pid,
1784 err = PTR_ERR(reply);
1787 dp_detach_port(vport);
1790 genl_notify(reply, genl_info_net(info), info->snd_pid,
1791 dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1800 static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
1802 struct nlattr **a = info->attrs;
1803 struct sk_buff *reply;
1804 struct vport *vport;
1807 err = ovs_vport_cmd_validate(a);
1812 vport = lookup_vport(info->userhdr, a);
1813 err = PTR_ERR(vport);
1818 if (a[OVS_VPORT_ATTR_TYPE] &&
1819 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type)
1822 if (!err && a[OVS_VPORT_ATTR_OPTIONS])
1823 err = vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
1825 err = change_vport(vport, a);
1826 if (!err && a[OVS_VPORT_ATTR_UPCALL_PID])
1827 vport->upcall_pid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
1829 reply = ovs_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
1831 if (IS_ERR(reply)) {
1832 err = PTR_ERR(reply);
1833 netlink_set_err(INIT_NET_GENL_SOCK, 0,
1834 dp_vport_multicast_group.id, err);
1838 genl_notify(reply, genl_info_net(info), info->snd_pid,
1839 dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1847 static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
1849 struct nlattr **a = info->attrs;
1850 struct sk_buff *reply;
1851 struct vport *vport;
1854 err = ovs_vport_cmd_validate(a);
1859 vport = lookup_vport(info->userhdr, a);
1860 err = PTR_ERR(vport);
1864 if (vport->port_no == OVSP_LOCAL) {
1869 reply = ovs_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
1871 err = PTR_ERR(reply);
1875 dp_detach_port(vport);
1877 genl_notify(reply, genl_info_net(info), info->snd_pid,
1878 dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1886 static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
1888 struct nlattr **a = info->attrs;
1889 struct ovs_header *ovs_header = info->userhdr;
1890 struct sk_buff *reply;
1891 struct vport *vport;
1894 err = ovs_vport_cmd_validate(a);
1899 vport = lookup_vport(ovs_header, a);
1900 err = PTR_ERR(vport);
1904 reply = ovs_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
1906 err = PTR_ERR(reply);
1912 return genlmsg_reply(reply, info);
1920 static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1922 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1923 struct datapath *dp;
1927 dp = get_dp(ovs_header->dp_ifindex);
1932 for (port_no = cb->args[0]; port_no < DP_MAX_PORTS; port_no++) {
1933 struct vport *vport;
1935 vport = get_vport_protected(dp, port_no);
1939 if (ovs_vport_cmd_fill_info(vport, skb, NETLINK_CB(cb->skb).pid,
1940 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1941 OVS_VPORT_CMD_NEW) < 0)
1946 cb->args[0] = port_no;
1952 static struct genl_ops dp_vport_genl_ops[] = {
1953 { .cmd = OVS_VPORT_CMD_NEW,
1954 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1955 .policy = vport_policy,
1956 .doit = ovs_vport_cmd_new
1958 { .cmd = OVS_VPORT_CMD_DEL,
1959 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1960 .policy = vport_policy,
1961 .doit = ovs_vport_cmd_del
1963 { .cmd = OVS_VPORT_CMD_GET,
1964 .flags = 0, /* OK for unprivileged users. */
1965 .policy = vport_policy,
1966 .doit = ovs_vport_cmd_get,
1967 .dumpit = ovs_vport_cmd_dump
1969 { .cmd = OVS_VPORT_CMD_SET,
1970 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1971 .policy = vport_policy,
1972 .doit = ovs_vport_cmd_set,
1976 struct genl_family_and_ops {
1977 struct genl_family *family;
1978 struct genl_ops *ops;
1980 struct genl_multicast_group *group;
1983 static const struct genl_family_and_ops dp_genl_families[] = {
1984 { &dp_datapath_genl_family,
1985 dp_datapath_genl_ops, ARRAY_SIZE(dp_datapath_genl_ops),
1986 &dp_datapath_multicast_group },
1987 { &dp_vport_genl_family,
1988 dp_vport_genl_ops, ARRAY_SIZE(dp_vport_genl_ops),
1989 &dp_vport_multicast_group },
1990 { &dp_flow_genl_family,
1991 dp_flow_genl_ops, ARRAY_SIZE(dp_flow_genl_ops),
1992 &dp_flow_multicast_group },
1993 { &dp_packet_genl_family,
1994 dp_packet_genl_ops, ARRAY_SIZE(dp_packet_genl_ops),
1998 static void dp_unregister_genl(int n_families)
2002 for (i = 0; i < n_families; i++)
2003 genl_unregister_family(dp_genl_families[i].family);
2006 static int dp_register_genl(void)
2013 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
2014 const struct genl_family_and_ops *f = &dp_genl_families[i];
2016 err = genl_register_family_with_ops(f->family, f->ops,
2023 err = genl_register_mc_group(f->family, f->group);
2032 dp_unregister_genl(n_registered);
2036 static int __init dp_init(void)
2038 struct sk_buff *dummy_skb;
2041 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > sizeof(dummy_skb->cb));
2043 pr_info("Open vSwitch %s, built "__DATE__" "__TIME__"\n",
2052 goto error_tnl_exit;
2056 goto error_flow_exit;
2058 err = register_netdevice_notifier(&dp_device_notifier);
2060 goto error_vport_exit;
2062 err = dp_register_genl();
2064 goto error_unreg_notifier;
2068 error_unreg_notifier:
2069 unregister_netdevice_notifier(&dp_device_notifier);
2080 static void dp_cleanup(void)
2083 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2084 unregister_netdevice_notifier(&dp_device_notifier);
2090 module_init(dp_init);
2091 module_exit(dp_cleanup);
2093 MODULE_DESCRIPTION("Open vSwitch switching datapath");
2094 MODULE_LICENSE("GPL");