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 (*ovs_dp_ioctl_hook)(struct net_device *dev, struct ifreq *rq, int cmd);
67 EXPORT_SYMBOL(ovs_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 static 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 = ovs_internal_dev_get_vport(dev);
115 /* Must be called with rcu_read_lock or RTNL lock. */
116 const char *ovs_dp_name(const struct datapath *dp)
118 struct vport *vport = rcu_dereference_rtnl(dp->ports[OVSP_LOCAL]);
119 return vport->ops->get_name(vport);
122 static int get_dpifindex(struct datapath *dp)
129 local = rcu_dereference(dp->ports[OVSP_LOCAL]);
131 ifindex = local->ops->get_ifindex(local);
140 static size_t br_nlmsg_size(void)
142 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
143 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
144 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
145 + nla_total_size(4) /* IFLA_MASTER */
146 + nla_total_size(4) /* IFLA_MTU */
147 + nla_total_size(1); /* IFLA_OPERSTATE */
150 /* Caller must hold RTNL lock. */
151 static int dp_fill_ifinfo(struct sk_buff *skb,
152 const struct vport *port,
153 int event, unsigned int flags)
155 struct datapath *dp = port->dp;
156 struct ifinfomsg *hdr;
157 struct nlmsghdr *nlh;
159 if (!port->ops->get_ifindex)
162 nlh = nlmsg_put(skb, 0, 0, event, sizeof(*hdr), flags);
166 hdr = nlmsg_data(nlh);
167 hdr->ifi_family = AF_BRIDGE;
169 hdr->ifi_type = ARPHRD_ETHER;
170 hdr->ifi_index = port->ops->get_ifindex(port);
171 hdr->ifi_flags = port->ops->get_dev_flags(port);
174 NLA_PUT_STRING(skb, IFLA_IFNAME, port->ops->get_name(port));
175 NLA_PUT_U32(skb, IFLA_MASTER, get_dpifindex(dp));
176 NLA_PUT_U32(skb, IFLA_MTU, port->ops->get_mtu(port));
177 #ifdef IFLA_OPERSTATE
178 NLA_PUT_U8(skb, IFLA_OPERSTATE,
179 port->ops->is_running(port)
180 ? port->ops->get_operstate(port)
184 NLA_PUT(skb, IFLA_ADDRESS, ETH_ALEN, port->ops->get_addr(port));
186 return nlmsg_end(skb, nlh);
189 nlmsg_cancel(skb, nlh);
193 /* Caller must hold RTNL lock. */
194 static void dp_ifinfo_notify(int event, struct vport *port)
199 skb = nlmsg_new(br_nlmsg_size(), GFP_KERNEL);
205 err = dp_fill_ifinfo(skb, port, event, 0);
207 if (err == -ENODEV) {
210 /* -EMSGSIZE implies BUG in br_nlmsg_size() */
211 WARN_ON(err == -EMSGSIZE);
216 rtnl_notify(skb, &init_net, 0, RTNLGRP_LINK, NULL, GFP_KERNEL);
220 rtnl_set_sk_err(&init_net, RTNLGRP_LINK, err);
225 static void release_dp(struct kobject *kobj)
227 struct datapath *dp = container_of(kobj, struct datapath, ifobj);
231 static struct kobj_type dp_ktype = {
232 .release = release_dp
235 static void destroy_dp_rcu(struct rcu_head *rcu)
237 struct datapath *dp = container_of(rcu, struct datapath, rcu);
239 ovs_flow_tbl_destroy((__force struct flow_table *)dp->table);
240 free_percpu(dp->stats_percpu);
241 kobject_put(&dp->ifobj);
244 /* Called with RTNL lock and genl_lock. */
245 static struct vport *new_vport(const struct vport_parms *parms)
249 vport = ovs_vport_add(parms);
250 if (!IS_ERR(vport)) {
251 struct datapath *dp = parms->dp;
253 rcu_assign_pointer(dp->ports[parms->port_no], vport);
254 list_add(&vport->node, &dp->port_list);
256 dp_ifinfo_notify(RTM_NEWLINK, vport);
262 /* Called with RTNL lock. */
263 void ovs_dp_detach_port(struct vport *p)
267 if (p->port_no != OVSP_LOCAL)
268 ovs_dp_sysfs_del_if(p);
269 dp_ifinfo_notify(RTM_DELLINK, p);
271 /* First drop references to device. */
273 rcu_assign_pointer(p->dp->ports[p->port_no], NULL);
275 /* Then destroy it. */
279 /* Must be called with rcu_read_lock. */
280 void ovs_dp_process_received_packet(struct vport *p, struct sk_buff *skb)
282 struct datapath *dp = p->dp;
283 struct sw_flow *flow;
284 struct dp_stats_percpu *stats;
288 stats = per_cpu_ptr(dp->stats_percpu, smp_processor_id());
290 if (!OVS_CB(skb)->flow) {
291 struct sw_flow_key key;
294 /* Extract flow from 'skb' into 'key'. */
295 error = ovs_flow_extract(skb, p->port_no, &key, &key_len);
296 if (unlikely(error)) {
302 flow = ovs_flow_tbl_lookup(rcu_dereference(dp->table),
304 if (unlikely(!flow)) {
305 struct dp_upcall_info upcall;
307 upcall.cmd = OVS_PACKET_CMD_MISS;
309 upcall.userdata = NULL;
310 upcall.pid = p->upcall_pid;
311 ovs_dp_upcall(dp, skb, &upcall);
313 stats_counter = &stats->n_missed;
317 OVS_CB(skb)->flow = flow;
320 stats_counter = &stats->n_hit;
321 ovs_flow_used(OVS_CB(skb)->flow, skb);
322 ovs_execute_actions(dp, skb);
325 /* Update datapath statistics. */
326 u64_stats_update_begin(&stats->sync);
328 u64_stats_update_end(&stats->sync);
331 static struct genl_family dp_packet_genl_family = {
332 .id = GENL_ID_GENERATE,
333 .hdrsize = sizeof(struct ovs_header),
334 .name = OVS_PACKET_FAMILY,
335 .version = OVS_PACKET_VERSION,
336 .maxattr = OVS_PACKET_ATTR_MAX
339 int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
340 const struct dp_upcall_info *upcall_info)
342 struct dp_stats_percpu *stats;
346 if (upcall_info->pid == 0) {
351 dp_ifindex = get_dpifindex(dp);
357 forward_ip_summed(skb, true);
359 if (!skb_is_gso(skb))
360 err = queue_userspace_packet(dp_ifindex, skb, upcall_info);
362 err = queue_gso_packets(dp_ifindex, skb, upcall_info);
369 stats = per_cpu_ptr(dp->stats_percpu, smp_processor_id());
371 u64_stats_update_begin(&stats->sync);
373 u64_stats_update_end(&stats->sync);
378 static int queue_gso_packets(int dp_ifindex, struct sk_buff *skb,
379 const struct dp_upcall_info *upcall_info)
381 struct dp_upcall_info later_info;
382 struct sw_flow_key later_key;
383 struct sk_buff *segs, *nskb;
386 segs = skb_gso_segment(skb, NETIF_F_SG | NETIF_F_HW_CSUM);
390 /* Queue all of the segments. */
393 err = queue_userspace_packet(dp_ifindex, skb, upcall_info);
397 if (skb == segs && skb_shinfo(skb)->gso_type & SKB_GSO_UDP) {
398 /* The initial flow key extracted by ovs_flow_extract()
399 * in this case is for a first fragment, so we need to
400 * properly mark later fragments.
402 later_key = *upcall_info->key;
403 later_key.ip.frag = OVS_FRAG_TYPE_LATER;
405 later_info = *upcall_info;
406 later_info.key = &later_key;
407 upcall_info = &later_info;
409 } while ((skb = skb->next));
411 /* Free all of the segments. */
419 } while ((skb = nskb));
423 static int queue_userspace_packet(int dp_ifindex, struct sk_buff *skb,
424 const struct dp_upcall_info *upcall_info)
426 struct ovs_header *upcall;
427 struct sk_buff *nskb = NULL;
428 struct sk_buff *user_skb; /* to be queued to userspace */
433 if (vlan_tx_tag_present(skb)) {
434 nskb = skb_clone(skb, GFP_ATOMIC);
438 err = vlan_deaccel_tag(nskb);
445 if (nla_attr_size(skb->len) > USHRT_MAX) {
450 len = sizeof(struct ovs_header);
451 len += nla_total_size(skb->len);
452 len += nla_total_size(FLOW_BUFSIZE);
453 if (upcall_info->cmd == OVS_PACKET_CMD_ACTION)
454 len += nla_total_size(8);
456 user_skb = genlmsg_new(len, GFP_ATOMIC);
462 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
463 0, upcall_info->cmd);
464 upcall->dp_ifindex = dp_ifindex;
466 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
467 ovs_flow_to_nlattrs(upcall_info->key, user_skb);
468 nla_nest_end(user_skb, nla);
470 if (upcall_info->userdata)
471 nla_put_u64(user_skb, OVS_PACKET_ATTR_USERDATA,
472 nla_get_u64(upcall_info->userdata));
474 nla = __nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, skb->len);
476 skb_copy_and_csum_dev(skb, nla_data(nla));
478 err = genlmsg_unicast(&init_net, user_skb, upcall_info->pid);
485 /* Called with genl_mutex. */
486 static int flush_flows(int dp_ifindex)
488 struct flow_table *old_table;
489 struct flow_table *new_table;
492 dp = get_dp(dp_ifindex);
496 old_table = genl_dereference(dp->table);
497 new_table = ovs_flow_tbl_alloc(TBL_MIN_BUCKETS);
501 rcu_assign_pointer(dp->table, new_table);
503 ovs_flow_tbl_deferred_destroy(old_table);
507 static int validate_actions(const struct nlattr *attr,
508 const struct sw_flow_key *key, int depth);
510 static int validate_sample(const struct nlattr *attr,
511 const struct sw_flow_key *key, int depth)
513 const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
514 const struct nlattr *probability, *actions;
515 const struct nlattr *a;
518 memset(attrs, 0, sizeof(attrs));
519 nla_for_each_nested(a, attr, rem) {
520 int type = nla_type(a);
521 if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
528 probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
529 if (!probability || nla_len(probability) != sizeof(u32))
532 actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
533 if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
535 return validate_actions(actions, key, depth + 1);
538 static int validate_set(const struct nlattr *a,
539 const struct sw_flow_key *flow_key)
541 const struct nlattr *ovs_key = nla_data(a);
542 int key_type = nla_type(ovs_key);
544 /* There can be only one key in a action */
545 if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
548 if (key_type > OVS_KEY_ATTR_MAX ||
549 nla_len(ovs_key) != ovs_key_lens[key_type])
553 const struct ovs_key_ipv4 *ipv4_key;
555 case OVS_KEY_ATTR_PRIORITY:
556 case OVS_KEY_ATTR_TUN_ID:
557 case OVS_KEY_ATTR_ETHERNET:
560 case OVS_KEY_ATTR_IPV4:
561 if (flow_key->eth.type != htons(ETH_P_IP))
564 if (!flow_key->ipv4.addr.src || !flow_key->ipv4.addr.dst)
567 ipv4_key = nla_data(ovs_key);
568 if (ipv4_key->ipv4_proto != flow_key->ip.proto)
571 if (ipv4_key->ipv4_frag != flow_key->ip.frag)
576 case OVS_KEY_ATTR_TCP:
577 if (flow_key->ip.proto != IPPROTO_TCP)
580 if (!flow_key->ipv4.tp.src || !flow_key->ipv4.tp.dst)
585 case OVS_KEY_ATTR_UDP:
586 if (flow_key->ip.proto != IPPROTO_UDP)
589 if (!flow_key->ipv4.tp.src || !flow_key->ipv4.tp.dst)
600 static int validate_userspace(const struct nlattr *attr)
602 static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
603 [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
604 [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_U64 },
606 struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
609 error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
610 attr, userspace_policy);
614 if (!a[OVS_USERSPACE_ATTR_PID] ||
615 !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
621 static int validate_actions(const struct nlattr *attr,
622 const struct sw_flow_key *key, int depth)
624 const struct nlattr *a;
627 if (depth >= SAMPLE_ACTION_DEPTH)
630 nla_for_each_nested(a, attr, rem) {
631 /* Expected argument lengths, (u32)-1 for variable length. */
632 static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
633 [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
634 [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
635 [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
636 [OVS_ACTION_ATTR_POP_VLAN] = 0,
637 [OVS_ACTION_ATTR_SET] = (u32)-1,
638 [OVS_ACTION_ATTR_SAMPLE] = (u32)-1
640 const struct ovs_action_push_vlan *vlan;
641 int type = nla_type(a);
643 if (type > OVS_ACTION_ATTR_MAX ||
644 (action_lens[type] != nla_len(a) &&
645 action_lens[type] != (u32)-1))
649 case OVS_ACTION_ATTR_UNSPEC:
652 case OVS_ACTION_ATTR_USERSPACE:
653 err = validate_userspace(a);
658 case OVS_ACTION_ATTR_OUTPUT:
659 if (nla_get_u32(a) >= DP_MAX_PORTS)
664 case OVS_ACTION_ATTR_POP_VLAN:
667 case OVS_ACTION_ATTR_PUSH_VLAN:
669 if (vlan->vlan_tpid != htons(ETH_P_8021Q))
671 if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
675 case OVS_ACTION_ATTR_SET:
676 err = validate_set(a, key);
681 case OVS_ACTION_ATTR_SAMPLE:
682 err = validate_sample(a, key, depth);
698 static void clear_stats(struct sw_flow *flow)
702 flow->packet_count = 0;
703 flow->byte_count = 0;
706 static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
708 struct ovs_header *ovs_header = info->userhdr;
709 struct nlattr **a = info->attrs;
710 struct sw_flow_actions *acts;
711 struct sk_buff *packet;
712 struct sw_flow *flow;
720 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
721 !a[OVS_PACKET_ATTR_ACTIONS] ||
722 nla_len(a[OVS_PACKET_ATTR_PACKET]) < ETH_HLEN)
725 len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
726 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
730 skb_reserve(packet, NET_IP_ALIGN);
732 memcpy(__skb_put(packet, len), nla_data(a[OVS_PACKET_ATTR_PACKET]), len);
734 skb_reset_mac_header(packet);
735 eth = eth_hdr(packet);
737 /* Normally, setting the skb 'protocol' field would be handled by a
738 * call to eth_type_trans(), but it assumes there's a sending
739 * device, which we may not have. */
740 if (ntohs(eth->h_proto) >= 1536)
741 packet->protocol = eth->h_proto;
743 packet->protocol = htons(ETH_P_802_2);
745 /* Build an sw_flow for sending this packet. */
746 flow = ovs_flow_alloc();
751 err = ovs_flow_extract(packet, -1, &flow->key, &key_len);
755 err = ovs_flow_metadata_from_nlattrs(&flow->key.phy.priority,
756 &flow->key.phy.in_port,
757 &flow->key.phy.tun_id,
758 a[OVS_PACKET_ATTR_KEY]);
762 err = validate_actions(a[OVS_PACKET_ATTR_ACTIONS], &flow->key, 0);
766 flow->hash = ovs_flow_hash(&flow->key, key_len);
768 acts = ovs_flow_actions_alloc(a[OVS_PACKET_ATTR_ACTIONS]);
772 rcu_assign_pointer(flow->sf_acts, acts);
774 OVS_CB(packet)->flow = flow;
775 packet->priority = flow->key.phy.priority;
778 dp = get_dp(ovs_header->dp_ifindex);
784 err = ovs_execute_actions(dp, packet);
801 static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
802 [OVS_PACKET_ATTR_PACKET] = { .type = NLA_UNSPEC },
803 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
804 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
807 static struct genl_ops dp_packet_genl_ops[] = {
808 { .cmd = OVS_PACKET_CMD_EXECUTE,
809 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
810 .policy = packet_policy,
811 .doit = ovs_packet_cmd_execute
815 static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats)
818 struct flow_table *table = genl_dereference(dp->table);
820 stats->n_flows = ovs_flow_tbl_count(table);
822 stats->n_hit = stats->n_missed = stats->n_lost = 0;
823 for_each_possible_cpu(i) {
824 const struct dp_stats_percpu *percpu_stats;
825 struct dp_stats_percpu local_stats;
828 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
831 start = u64_stats_fetch_begin_bh(&percpu_stats->sync);
832 local_stats = *percpu_stats;
833 } while (u64_stats_fetch_retry_bh(&percpu_stats->sync, start));
835 stats->n_hit += local_stats.n_hit;
836 stats->n_missed += local_stats.n_missed;
837 stats->n_lost += local_stats.n_lost;
841 static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
842 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
843 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
844 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
847 static struct genl_family dp_flow_genl_family = {
848 .id = GENL_ID_GENERATE,
849 .hdrsize = sizeof(struct ovs_header),
850 .name = OVS_FLOW_FAMILY,
851 .version = OVS_FLOW_VERSION,
852 .maxattr = OVS_FLOW_ATTR_MAX
855 static struct genl_multicast_group ovs_dp_flow_multicast_group = {
856 .name = OVS_FLOW_MCGROUP
859 /* Called with genl_lock. */
860 static int ovs_flow_cmd_fill_info(struct sw_flow *flow, struct datapath *dp,
861 struct sk_buff *skb, u32 pid,
862 u32 seq, u32 flags, u8 cmd)
864 const int skb_orig_len = skb->len;
865 const struct sw_flow_actions *sf_acts;
866 struct ovs_flow_stats stats;
867 struct ovs_header *ovs_header;
873 sf_acts = rcu_dereference_protected(flow->sf_acts,
874 lockdep_genl_is_held());
876 ovs_header = genlmsg_put(skb, pid, seq, &dp_flow_genl_family, flags, cmd);
880 ovs_header->dp_ifindex = get_dpifindex(dp);
882 nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
884 goto nla_put_failure;
885 err = ovs_flow_to_nlattrs(&flow->key, skb);
888 nla_nest_end(skb, nla);
890 spin_lock_bh(&flow->lock);
892 stats.n_packets = flow->packet_count;
893 stats.n_bytes = flow->byte_count;
894 tcp_flags = flow->tcp_flags;
895 spin_unlock_bh(&flow->lock);
898 NLA_PUT_U64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used));
901 NLA_PUT(skb, OVS_FLOW_ATTR_STATS,
902 sizeof(struct ovs_flow_stats), &stats);
905 NLA_PUT_U8(skb, OVS_FLOW_ATTR_TCP_FLAGS, tcp_flags);
907 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
908 * this is the first flow to be dumped into 'skb'. This is unusual for
909 * Netlink but individual action lists can be longer than
910 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
911 * The userspace caller can always fetch the actions separately if it
912 * really wants them. (Most userspace callers in fact don't care.)
914 * This can only fail for dump operations because the skb is always
915 * properly sized for single flows.
917 err = nla_put(skb, OVS_FLOW_ATTR_ACTIONS, sf_acts->actions_len,
919 if (err < 0 && skb_orig_len)
922 return genlmsg_end(skb, ovs_header);
927 genlmsg_cancel(skb, ovs_header);
931 static struct sk_buff *ovs_flow_cmd_alloc_info(struct sw_flow *flow)
933 const struct sw_flow_actions *sf_acts;
936 sf_acts = rcu_dereference_protected(flow->sf_acts,
937 lockdep_genl_is_held());
939 /* OVS_FLOW_ATTR_KEY */
940 len = nla_total_size(FLOW_BUFSIZE);
941 /* OVS_FLOW_ATTR_ACTIONS */
942 len += nla_total_size(sf_acts->actions_len);
943 /* OVS_FLOW_ATTR_STATS */
944 len += nla_total_size(sizeof(struct ovs_flow_stats));
945 /* OVS_FLOW_ATTR_TCP_FLAGS */
946 len += nla_total_size(1);
947 /* OVS_FLOW_ATTR_USED */
948 len += nla_total_size(8);
950 len += NLMSG_ALIGN(sizeof(struct ovs_header));
952 return genlmsg_new(len, GFP_KERNEL);
955 static struct sk_buff *ovs_flow_cmd_build_info(struct sw_flow *flow,
957 u32 pid, u32 seq, u8 cmd)
962 skb = ovs_flow_cmd_alloc_info(flow);
964 return ERR_PTR(-ENOMEM);
966 retval = ovs_flow_cmd_fill_info(flow, dp, skb, pid, seq, 0, cmd);
971 static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info)
973 struct nlattr **a = info->attrs;
974 struct ovs_header *ovs_header = info->userhdr;
975 struct sw_flow_key key;
976 struct sw_flow *flow;
977 struct sk_buff *reply;
979 struct flow_table *table;
985 if (!a[OVS_FLOW_ATTR_KEY])
987 error = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
991 /* Validate actions. */
992 if (a[OVS_FLOW_ATTR_ACTIONS]) {
993 error = validate_actions(a[OVS_FLOW_ATTR_ACTIONS], &key, 0);
996 } else if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW) {
1001 dp = get_dp(ovs_header->dp_ifindex);
1006 table = genl_dereference(dp->table);
1007 flow = ovs_flow_tbl_lookup(table, &key, key_len);
1009 struct sw_flow_actions *acts;
1011 /* Bail out if we're not allowed to create a new flow. */
1013 if (info->genlhdr->cmd == OVS_FLOW_CMD_SET)
1016 /* Expand table, if necessary, to make room. */
1017 if (ovs_flow_tbl_need_to_expand(table)) {
1018 struct flow_table *new_table;
1020 new_table = ovs_flow_tbl_expand(table);
1021 if (!IS_ERR(new_table)) {
1022 rcu_assign_pointer(dp->table, new_table);
1023 ovs_flow_tbl_deferred_destroy(table);
1024 table = genl_dereference(dp->table);
1028 /* Allocate flow. */
1029 flow = ovs_flow_alloc();
1031 error = PTR_ERR(flow);
1037 /* Obtain actions. */
1038 acts = ovs_flow_actions_alloc(a[OVS_FLOW_ATTR_ACTIONS]);
1039 error = PTR_ERR(acts);
1041 goto error_free_flow;
1042 rcu_assign_pointer(flow->sf_acts, acts);
1044 /* Put flow in bucket. */
1045 flow->hash = ovs_flow_hash(&key, key_len);
1046 ovs_flow_tbl_insert(table, flow);
1048 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_pid,
1052 /* We found a matching flow. */
1053 struct sw_flow_actions *old_acts;
1054 struct nlattr *acts_attrs;
1056 /* Bail out if we're not allowed to modify an existing flow.
1057 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
1058 * because Generic Netlink treats the latter as a dump
1059 * request. We also accept NLM_F_EXCL in case that bug ever
1063 if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW &&
1064 info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL))
1067 /* Update actions. */
1068 old_acts = rcu_dereference_protected(flow->sf_acts,
1069 lockdep_genl_is_held());
1070 acts_attrs = a[OVS_FLOW_ATTR_ACTIONS];
1072 (old_acts->actions_len != nla_len(acts_attrs) ||
1073 memcmp(old_acts->actions, nla_data(acts_attrs),
1074 old_acts->actions_len))) {
1075 struct sw_flow_actions *new_acts;
1077 new_acts = ovs_flow_actions_alloc(acts_attrs);
1078 error = PTR_ERR(new_acts);
1079 if (IS_ERR(new_acts))
1082 rcu_assign_pointer(flow->sf_acts, new_acts);
1083 ovs_flow_deferred_free_acts(old_acts);
1086 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_pid,
1087 info->snd_seq, OVS_FLOW_CMD_NEW);
1090 if (a[OVS_FLOW_ATTR_CLEAR]) {
1091 spin_lock_bh(&flow->lock);
1093 spin_unlock_bh(&flow->lock);
1098 genl_notify(reply, genl_info_net(info), info->snd_pid,
1099 ovs_dp_flow_multicast_group.id, info->nlhdr,
1102 netlink_set_err(INIT_NET_GENL_SOCK, 0,
1103 ovs_dp_flow_multicast_group.id,
1113 static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1115 struct nlattr **a = info->attrs;
1116 struct ovs_header *ovs_header = info->userhdr;
1117 struct sw_flow_key key;
1118 struct sk_buff *reply;
1119 struct sw_flow *flow;
1120 struct datapath *dp;
1121 struct flow_table *table;
1125 if (!a[OVS_FLOW_ATTR_KEY])
1127 err = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
1131 dp = get_dp(ovs_header->dp_ifindex);
1135 table = genl_dereference(dp->table);
1136 flow = ovs_flow_tbl_lookup(table, &key, key_len);
1140 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_pid,
1141 info->snd_seq, OVS_FLOW_CMD_NEW);
1143 return PTR_ERR(reply);
1145 return genlmsg_reply(reply, info);
1148 static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1150 struct nlattr **a = info->attrs;
1151 struct ovs_header *ovs_header = info->userhdr;
1152 struct sw_flow_key key;
1153 struct sk_buff *reply;
1154 struct sw_flow *flow;
1155 struct datapath *dp;
1156 struct flow_table *table;
1160 if (!a[OVS_FLOW_ATTR_KEY])
1161 return flush_flows(ovs_header->dp_ifindex);
1162 err = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
1166 dp = get_dp(ovs_header->dp_ifindex);
1170 table = genl_dereference(dp->table);
1171 flow = ovs_flow_tbl_lookup(table, &key, key_len);
1175 reply = ovs_flow_cmd_alloc_info(flow);
1179 ovs_flow_tbl_remove(table, flow);
1181 err = ovs_flow_cmd_fill_info(flow, dp, reply, info->snd_pid,
1182 info->snd_seq, 0, OVS_FLOW_CMD_DEL);
1185 ovs_flow_deferred_free(flow);
1187 genl_notify(reply, genl_info_net(info), info->snd_pid,
1188 ovs_dp_flow_multicast_group.id, info->nlhdr, GFP_KERNEL);
1192 static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1194 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1195 struct datapath *dp;
1196 struct flow_table *table;
1198 dp = get_dp(ovs_header->dp_ifindex);
1202 table = genl_dereference(dp->table);
1205 struct sw_flow *flow;
1208 bucket = cb->args[0];
1210 flow = ovs_flow_tbl_next(table, &bucket, &obj);
1214 if (ovs_flow_cmd_fill_info(flow, dp, skb,
1215 NETLINK_CB(cb->skb).pid,
1216 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1217 OVS_FLOW_CMD_NEW) < 0)
1220 cb->args[0] = bucket;
1226 static struct genl_ops dp_flow_genl_ops[] = {
1227 { .cmd = OVS_FLOW_CMD_NEW,
1228 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1229 .policy = flow_policy,
1230 .doit = ovs_flow_cmd_new_or_set
1232 { .cmd = OVS_FLOW_CMD_DEL,
1233 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1234 .policy = flow_policy,
1235 .doit = ovs_flow_cmd_del
1237 { .cmd = OVS_FLOW_CMD_GET,
1238 .flags = 0, /* OK for unprivileged users. */
1239 .policy = flow_policy,
1240 .doit = ovs_flow_cmd_get,
1241 .dumpit = ovs_flow_cmd_dump
1243 { .cmd = OVS_FLOW_CMD_SET,
1244 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1245 .policy = flow_policy,
1246 .doit = ovs_flow_cmd_new_or_set,
1250 static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1251 #ifdef HAVE_NLA_NUL_STRING
1252 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1254 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1257 static struct genl_family dp_datapath_genl_family = {
1258 .id = GENL_ID_GENERATE,
1259 .hdrsize = sizeof(struct ovs_header),
1260 .name = OVS_DATAPATH_FAMILY,
1261 .version = OVS_DATAPATH_VERSION,
1262 .maxattr = OVS_DP_ATTR_MAX
1265 static struct genl_multicast_group ovs_dp_datapath_multicast_group = {
1266 .name = OVS_DATAPATH_MCGROUP
1269 static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
1270 u32 pid, u32 seq, u32 flags, u8 cmd)
1272 struct ovs_header *ovs_header;
1273 struct ovs_dp_stats dp_stats;
1276 ovs_header = genlmsg_put(skb, pid, seq, &dp_datapath_genl_family,
1281 ovs_header->dp_ifindex = get_dpifindex(dp);
1284 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
1287 goto nla_put_failure;
1289 get_dp_stats(dp, &dp_stats);
1290 NLA_PUT(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats), &dp_stats);
1292 return genlmsg_end(skb, ovs_header);
1295 genlmsg_cancel(skb, ovs_header);
1300 static struct sk_buff *ovs_dp_cmd_build_info(struct datapath *dp, u32 pid,
1303 struct sk_buff *skb;
1306 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1308 return ERR_PTR(-ENOMEM);
1310 retval = ovs_dp_cmd_fill_info(dp, skb, pid, seq, 0, cmd);
1313 return ERR_PTR(retval);
1318 static int ovs_dp_cmd_validate(struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1320 return CHECK_NUL_STRING(a[OVS_DP_ATTR_NAME], IFNAMSIZ - 1);
1323 /* Called with genl_mutex and optionally with RTNL lock also. */
1324 static struct datapath *lookup_datapath(struct ovs_header *ovs_header,
1325 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1327 struct datapath *dp;
1329 if (!a[OVS_DP_ATTR_NAME])
1330 dp = get_dp(ovs_header->dp_ifindex);
1332 struct vport *vport;
1335 vport = ovs_vport_locate(nla_data(a[OVS_DP_ATTR_NAME]));
1336 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
1339 return dp ? dp : ERR_PTR(-ENODEV);
1342 static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1344 struct nlattr **a = info->attrs;
1345 struct vport_parms parms;
1346 struct sk_buff *reply;
1347 struct datapath *dp;
1348 struct vport *vport;
1352 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1355 err = ovs_dp_cmd_validate(a);
1361 if (!try_module_get(THIS_MODULE))
1362 goto err_unlock_rtnl;
1365 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1367 goto err_put_module;
1368 INIT_LIST_HEAD(&dp->port_list);
1370 /* Initialize kobject for bridge. This will be added as
1371 * /sys/class/net/<devname>/brif later, if sysfs is enabled. */
1372 dp->ifobj.kset = NULL;
1373 kobject_init(&dp->ifobj, &dp_ktype);
1375 /* Allocate table. */
1377 rcu_assign_pointer(dp->table, ovs_flow_tbl_alloc(TBL_MIN_BUCKETS));
1381 dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
1382 if (!dp->stats_percpu) {
1384 goto err_destroy_table;
1387 /* Set up our datapath device. */
1388 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1389 parms.type = OVS_VPORT_TYPE_INTERNAL;
1390 parms.options = NULL;
1392 parms.port_no = OVSP_LOCAL;
1393 parms.upcall_pid = nla_get_u32(a[OVS_DP_ATTR_UPCALL_PID]);
1395 vport = new_vport(&parms);
1396 if (IS_ERR(vport)) {
1397 err = PTR_ERR(vport);
1401 goto err_destroy_percpu;
1404 reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
1405 info->snd_seq, OVS_DP_CMD_NEW);
1406 err = PTR_ERR(reply);
1408 goto err_destroy_local_port;
1410 list_add_tail(&dp->list_node, &dps);
1411 ovs_dp_sysfs_add_dp(dp);
1415 genl_notify(reply, genl_info_net(info), info->snd_pid,
1416 ovs_dp_datapath_multicast_group.id, info->nlhdr,
1420 err_destroy_local_port:
1421 ovs_dp_detach_port(rtnl_dereference(dp->ports[OVSP_LOCAL]));
1423 free_percpu(dp->stats_percpu);
1425 ovs_flow_tbl_destroy(genl_dereference(dp->table));
1429 module_put(THIS_MODULE);
1436 static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1438 struct vport *vport, *next_vport;
1439 struct sk_buff *reply;
1440 struct datapath *dp;
1443 err = ovs_dp_cmd_validate(info->attrs);
1448 dp = lookup_datapath(info->userhdr, info->attrs);
1453 reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
1454 info->snd_seq, OVS_DP_CMD_DEL);
1455 err = PTR_ERR(reply);
1459 list_for_each_entry_safe(vport, next_vport, &dp->port_list, node)
1460 if (vport->port_no != OVSP_LOCAL)
1461 ovs_dp_detach_port(vport);
1463 ovs_dp_sysfs_del_dp(dp);
1464 list_del(&dp->list_node);
1465 ovs_dp_detach_port(rtnl_dereference(dp->ports[OVSP_LOCAL]));
1467 /* rtnl_unlock() will wait until all the references to devices that
1468 * are pending unregistration have been dropped. We do it here to
1469 * ensure that any internal devices (which contain DP pointers) are
1470 * fully destroyed before freeing the datapath.
1474 call_rcu(&dp->rcu, destroy_dp_rcu);
1475 module_put(THIS_MODULE);
1477 genl_notify(reply, genl_info_net(info), info->snd_pid,
1478 ovs_dp_datapath_multicast_group.id, info->nlhdr,
1489 static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1491 struct sk_buff *reply;
1492 struct datapath *dp;
1495 err = ovs_dp_cmd_validate(info->attrs);
1499 dp = lookup_datapath(info->userhdr, info->attrs);
1503 reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
1504 info->snd_seq, OVS_DP_CMD_NEW);
1505 if (IS_ERR(reply)) {
1506 err = PTR_ERR(reply);
1507 netlink_set_err(INIT_NET_GENL_SOCK, 0,
1508 ovs_dp_datapath_multicast_group.id, err);
1512 genl_notify(reply, genl_info_net(info), info->snd_pid,
1513 ovs_dp_datapath_multicast_group.id, info->nlhdr,
1519 static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1521 struct sk_buff *reply;
1522 struct datapath *dp;
1525 err = ovs_dp_cmd_validate(info->attrs);
1529 dp = lookup_datapath(info->userhdr, info->attrs);
1533 reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
1534 info->snd_seq, OVS_DP_CMD_NEW);
1536 return PTR_ERR(reply);
1538 return genlmsg_reply(reply, info);
1541 static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1543 struct datapath *dp;
1544 int skip = cb->args[0];
1547 list_for_each_entry(dp, &dps, list_node) {
1550 if (ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).pid,
1551 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1552 OVS_DP_CMD_NEW) < 0)
1562 static struct genl_ops dp_datapath_genl_ops[] = {
1563 { .cmd = OVS_DP_CMD_NEW,
1564 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1565 .policy = datapath_policy,
1566 .doit = ovs_dp_cmd_new
1568 { .cmd = OVS_DP_CMD_DEL,
1569 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1570 .policy = datapath_policy,
1571 .doit = ovs_dp_cmd_del
1573 { .cmd = OVS_DP_CMD_GET,
1574 .flags = 0, /* OK for unprivileged users. */
1575 .policy = datapath_policy,
1576 .doit = ovs_dp_cmd_get,
1577 .dumpit = ovs_dp_cmd_dump
1579 { .cmd = OVS_DP_CMD_SET,
1580 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1581 .policy = datapath_policy,
1582 .doit = ovs_dp_cmd_set,
1586 static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
1587 #ifdef HAVE_NLA_NUL_STRING
1588 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1589 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
1590 [OVS_VPORT_ATTR_ADDRESS] = { .len = ETH_ALEN },
1592 [OVS_VPORT_ATTR_STATS] = { .minlen = sizeof(struct ovs_vport_stats) },
1593 [OVS_VPORT_ATTR_ADDRESS] = { .minlen = ETH_ALEN },
1595 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
1596 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
1597 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1598 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
1601 static struct genl_family dp_vport_genl_family = {
1602 .id = GENL_ID_GENERATE,
1603 .hdrsize = sizeof(struct ovs_header),
1604 .name = OVS_VPORT_FAMILY,
1605 .version = OVS_VPORT_VERSION,
1606 .maxattr = OVS_VPORT_ATTR_MAX
1609 struct genl_multicast_group ovs_dp_vport_multicast_group = {
1610 .name = OVS_VPORT_MCGROUP
1613 /* Called with RTNL lock or RCU read lock. */
1614 static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
1615 u32 pid, u32 seq, u32 flags, u8 cmd)
1617 struct ovs_header *ovs_header;
1618 struct ovs_vport_stats vport_stats;
1621 ovs_header = genlmsg_put(skb, pid, seq, &dp_vport_genl_family,
1626 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1628 NLA_PUT_U32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no);
1629 NLA_PUT_U32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type);
1630 NLA_PUT_STRING(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport));
1631 NLA_PUT_U32(skb, OVS_VPORT_ATTR_UPCALL_PID, vport->upcall_pid);
1633 ovs_vport_get_stats(vport, &vport_stats);
1634 NLA_PUT(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
1637 NLA_PUT(skb, OVS_VPORT_ATTR_ADDRESS, ETH_ALEN,
1638 vport->ops->get_addr(vport));
1640 err = ovs_vport_get_options(vport, skb);
1641 if (err == -EMSGSIZE)
1644 return genlmsg_end(skb, ovs_header);
1649 genlmsg_cancel(skb, ovs_header);
1653 /* Called with RTNL lock or RCU read lock. */
1654 struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 pid,
1657 struct sk_buff *skb;
1660 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1662 return ERR_PTR(-ENOMEM);
1664 retval = ovs_vport_cmd_fill_info(vport, skb, pid, seq, 0, cmd);
1667 return ERR_PTR(retval);
1672 static int ovs_vport_cmd_validate(struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1674 return CHECK_NUL_STRING(a[OVS_VPORT_ATTR_NAME], IFNAMSIZ - 1);
1677 /* Called with RTNL lock or RCU read lock. */
1678 static struct vport *lookup_vport(struct ovs_header *ovs_header,
1679 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1681 struct datapath *dp;
1682 struct vport *vport;
1684 if (a[OVS_VPORT_ATTR_NAME]) {
1685 vport = ovs_vport_locate(nla_data(a[OVS_VPORT_ATTR_NAME]));
1687 return ERR_PTR(-ENODEV);
1689 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1690 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1692 if (port_no >= DP_MAX_PORTS)
1693 return ERR_PTR(-EFBIG);
1695 dp = get_dp(ovs_header->dp_ifindex);
1697 return ERR_PTR(-ENODEV);
1699 vport = rcu_dereference_rtnl(dp->ports[port_no]);
1701 return ERR_PTR(-ENOENT);
1704 return ERR_PTR(-EINVAL);
1707 /* Called with RTNL lock. */
1708 static int change_vport(struct vport *vport,
1709 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1713 if (a[OVS_VPORT_ATTR_STATS])
1714 ovs_vport_set_stats(vport, nla_data(a[OVS_VPORT_ATTR_STATS]));
1716 if (a[OVS_VPORT_ATTR_ADDRESS])
1717 err = ovs_vport_set_addr(vport, nla_data(a[OVS_VPORT_ATTR_ADDRESS]));
1722 static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1724 struct nlattr **a = info->attrs;
1725 struct ovs_header *ovs_header = info->userhdr;
1726 struct vport_parms parms;
1727 struct sk_buff *reply;
1728 struct vport *vport;
1729 struct datapath *dp;
1734 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
1735 !a[OVS_VPORT_ATTR_UPCALL_PID])
1738 err = ovs_vport_cmd_validate(a);
1743 dp = get_dp(ovs_header->dp_ifindex);
1748 if (a[OVS_VPORT_ATTR_PORT_NO]) {
1749 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1752 if (port_no >= DP_MAX_PORTS)
1755 vport = rtnl_dereference(dp->ports[port_no]);
1760 for (port_no = 1; ; port_no++) {
1761 if (port_no >= DP_MAX_PORTS) {
1765 vport = rtnl_dereference(dp->ports[port_no]);
1771 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
1772 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1773 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
1775 parms.port_no = port_no;
1776 parms.upcall_pid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
1778 vport = new_vport(&parms);
1779 err = PTR_ERR(vport);
1783 ovs_dp_sysfs_add_if(vport);
1785 err = change_vport(vport, a);
1787 reply = ovs_vport_cmd_build_info(vport, info->snd_pid,
1791 err = PTR_ERR(reply);
1794 ovs_dp_detach_port(vport);
1797 genl_notify(reply, genl_info_net(info), info->snd_pid,
1798 ovs_dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1806 static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
1808 struct nlattr **a = info->attrs;
1809 struct sk_buff *reply;
1810 struct vport *vport;
1813 err = ovs_vport_cmd_validate(a);
1818 vport = lookup_vport(info->userhdr, a);
1819 err = PTR_ERR(vport);
1824 if (a[OVS_VPORT_ATTR_TYPE] &&
1825 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type)
1828 if (!err && a[OVS_VPORT_ATTR_OPTIONS])
1829 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
1831 err = change_vport(vport, a);
1832 if (!err && a[OVS_VPORT_ATTR_UPCALL_PID])
1833 vport->upcall_pid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
1835 reply = ovs_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
1837 if (IS_ERR(reply)) {
1838 err = PTR_ERR(reply);
1839 netlink_set_err(INIT_NET_GENL_SOCK, 0,
1840 ovs_dp_vport_multicast_group.id, err);
1844 genl_notify(reply, genl_info_net(info), info->snd_pid,
1845 ovs_dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1853 static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
1855 struct nlattr **a = info->attrs;
1856 struct sk_buff *reply;
1857 struct vport *vport;
1860 err = ovs_vport_cmd_validate(a);
1865 vport = lookup_vport(info->userhdr, a);
1866 err = PTR_ERR(vport);
1870 if (vport->port_no == OVSP_LOCAL) {
1875 reply = ovs_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
1877 err = PTR_ERR(reply);
1881 ovs_dp_detach_port(vport);
1883 genl_notify(reply, genl_info_net(info), info->snd_pid,
1884 ovs_dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1892 static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
1894 struct nlattr **a = info->attrs;
1895 struct ovs_header *ovs_header = info->userhdr;
1896 struct sk_buff *reply;
1897 struct vport *vport;
1900 err = ovs_vport_cmd_validate(a);
1905 vport = lookup_vport(ovs_header, a);
1906 err = PTR_ERR(vport);
1910 reply = ovs_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
1912 err = PTR_ERR(reply);
1918 return genlmsg_reply(reply, info);
1926 static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1928 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1929 struct datapath *dp;
1933 dp = get_dp(ovs_header->dp_ifindex);
1938 for (port_no = cb->args[0]; port_no < DP_MAX_PORTS; port_no++) {
1939 struct vport *vport;
1941 vport = rcu_dereference(dp->ports[port_no]);
1945 if (ovs_vport_cmd_fill_info(vport, skb, NETLINK_CB(cb->skb).pid,
1946 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1947 OVS_VPORT_CMD_NEW) < 0)
1952 cb->args[0] = port_no;
1958 static struct genl_ops dp_vport_genl_ops[] = {
1959 { .cmd = OVS_VPORT_CMD_NEW,
1960 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1961 .policy = vport_policy,
1962 .doit = ovs_vport_cmd_new
1964 { .cmd = OVS_VPORT_CMD_DEL,
1965 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1966 .policy = vport_policy,
1967 .doit = ovs_vport_cmd_del
1969 { .cmd = OVS_VPORT_CMD_GET,
1970 .flags = 0, /* OK for unprivileged users. */
1971 .policy = vport_policy,
1972 .doit = ovs_vport_cmd_get,
1973 .dumpit = ovs_vport_cmd_dump
1975 { .cmd = OVS_VPORT_CMD_SET,
1976 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1977 .policy = vport_policy,
1978 .doit = ovs_vport_cmd_set,
1982 struct genl_family_and_ops {
1983 struct genl_family *family;
1984 struct genl_ops *ops;
1986 struct genl_multicast_group *group;
1989 static const struct genl_family_and_ops dp_genl_families[] = {
1990 { &dp_datapath_genl_family,
1991 dp_datapath_genl_ops, ARRAY_SIZE(dp_datapath_genl_ops),
1992 &ovs_dp_datapath_multicast_group },
1993 { &dp_vport_genl_family,
1994 dp_vport_genl_ops, ARRAY_SIZE(dp_vport_genl_ops),
1995 &ovs_dp_vport_multicast_group },
1996 { &dp_flow_genl_family,
1997 dp_flow_genl_ops, ARRAY_SIZE(dp_flow_genl_ops),
1998 &ovs_dp_flow_multicast_group },
1999 { &dp_packet_genl_family,
2000 dp_packet_genl_ops, ARRAY_SIZE(dp_packet_genl_ops),
2004 static void dp_unregister_genl(int n_families)
2008 for (i = 0; i < n_families; i++)
2009 genl_unregister_family(dp_genl_families[i].family);
2012 static int dp_register_genl(void)
2019 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
2020 const struct genl_family_and_ops *f = &dp_genl_families[i];
2022 err = genl_register_family_with_ops(f->family, f->ops,
2029 err = genl_register_mc_group(f->family, f->group);
2038 dp_unregister_genl(n_registered);
2042 static int __init dp_init(void)
2044 struct sk_buff *dummy_skb;
2047 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > sizeof(dummy_skb->cb));
2049 pr_info("Open vSwitch switching datapath %s, built "__DATE__" "__TIME__"\n",
2052 err = ovs_tnl_init();
2056 err = ovs_flow_init();
2058 goto error_tnl_exit;
2060 err = ovs_vport_init();
2062 goto error_flow_exit;
2064 err = register_netdevice_notifier(&ovs_dp_device_notifier);
2066 goto error_vport_exit;
2068 err = dp_register_genl();
2070 goto error_unreg_notifier;
2074 error_unreg_notifier:
2075 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2086 static void dp_cleanup(void)
2089 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2090 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2096 module_init(dp_init);
2097 module_exit(dp_cleanup);
2099 MODULE_DESCRIPTION("Open vSwitch switching datapath");
2100 MODULE_LICENSE("GPL");