2 * Copyright (c) 2007, 2008, 2009, 2010, 2011 Nicira Networks.
3 * Distributed under the terms of the GNU GPL version 2.
5 * Significant portions of this file may be copied from parts of the Linux
6 * kernel, by Linus Torvalds and others.
9 /* Functions for managing the dp interface/device. */
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #include <linux/init.h>
14 #include <linux/module.h>
15 #include <linux/if_arp.h>
16 #include <linux/if_vlan.h>
19 #include <linux/jhash.h>
20 #include <linux/delay.h>
21 #include <linux/time.h>
22 #include <linux/etherdevice.h>
23 #include <linux/genetlink.h>
24 #include <linux/kernel.h>
25 #include <linux/kthread.h>
26 #include <linux/mutex.h>
27 #include <linux/percpu.h>
28 #include <linux/rcupdate.h>
29 #include <linux/tcp.h>
30 #include <linux/udp.h>
31 #include <linux/version.h>
32 #include <linux/ethtool.h>
33 #include <linux/wait.h>
34 #include <asm/system.h>
35 #include <asm/div64.h>
37 #include <linux/highmem.h>
38 #include <linux/netfilter_bridge.h>
39 #include <linux/netfilter_ipv4.h>
40 #include <linux/inetdevice.h>
41 #include <linux/list.h>
42 #include <linux/rculist.h>
43 #include <linux/dmi.h>
44 #include <net/inet_ecn.h>
45 #include <net/genetlink.h>
47 #include "openvswitch/datapath-protocol.h"
54 #include "vport-internal_dev.h"
56 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18) || \
57 LINUX_VERSION_CODE >= KERNEL_VERSION(3,1,0)
58 #error Kernels before 2.6.18 or after 3.0 are not supported by this version of Open vSwitch.
61 int (*dp_ioctl_hook)(struct net_device *dev, struct ifreq *rq, int cmd);
62 EXPORT_SYMBOL(dp_ioctl_hook);
67 * Writes to device state (add/remove datapath, port, set operations on vports,
68 * etc.) are protected by RTNL.
70 * Writes to other state (flow table modifications, set miscellaneous datapath
71 * parameters such as drop frags, etc.) are protected by genl_mutex. The RTNL
72 * lock nests inside genl_mutex.
74 * Reads are protected by RCU.
76 * There are a few special cases (mostly stats) that have their own
77 * synchronization but they nest under all of above and don't interact with
81 /* Global list of datapaths to enable dumping them all out.
82 * Protected by genl_mutex.
84 static LIST_HEAD(dps);
86 static struct vport *new_vport(const struct vport_parms *);
87 static int queue_userspace_packets(struct datapath *, struct sk_buff *,
88 const struct dp_upcall_info *);
90 /* Must be called with rcu_read_lock, genl_mutex, or RTNL lock. */
91 struct datapath *get_dp(int dp_ifindex)
93 struct datapath *dp = NULL;
94 struct net_device *dev;
97 dev = dev_get_by_index_rcu(&init_net, dp_ifindex);
99 struct vport *vport = internal_dev_get_vport(dev);
107 EXPORT_SYMBOL_GPL(get_dp);
109 /* Must be called with genl_mutex. */
110 static struct flow_table *get_table_protected(struct datapath *dp)
112 return rcu_dereference_protected(dp->table, lockdep_genl_is_held());
115 /* Must be called with rcu_read_lock or RTNL lock. */
116 static struct vport *get_vport_protected(struct datapath *dp, u16 port_no)
118 return rcu_dereference_rtnl(dp->ports[port_no]);
121 /* Must be called with rcu_read_lock or RTNL lock. */
122 const char *dp_name(const struct datapath *dp)
124 return vport_get_name(rcu_dereference_rtnl(dp->ports[OVSP_LOCAL]));
127 static inline size_t br_nlmsg_size(void)
129 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
130 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
131 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
132 + nla_total_size(4) /* IFLA_MASTER */
133 + nla_total_size(4) /* IFLA_MTU */
134 + nla_total_size(1); /* IFLA_OPERSTATE */
137 /* Caller must hold RTNL lock. */
138 static int dp_fill_ifinfo(struct sk_buff *skb,
139 const struct vport *port,
140 int event, unsigned int flags)
142 struct datapath *dp = port->dp;
143 int ifindex = vport_get_ifindex(port);
144 struct ifinfomsg *hdr;
145 struct nlmsghdr *nlh;
150 nlh = nlmsg_put(skb, 0, 0, event, sizeof(*hdr), flags);
154 hdr = nlmsg_data(nlh);
155 hdr->ifi_family = AF_BRIDGE;
157 hdr->ifi_type = ARPHRD_ETHER;
158 hdr->ifi_index = ifindex;
159 hdr->ifi_flags = vport_get_flags(port);
162 NLA_PUT_STRING(skb, IFLA_IFNAME, vport_get_name(port));
163 NLA_PUT_U32(skb, IFLA_MASTER,
164 vport_get_ifindex(get_vport_protected(dp, OVSP_LOCAL)));
165 NLA_PUT_U32(skb, IFLA_MTU, vport_get_mtu(port));
166 #ifdef IFLA_OPERSTATE
167 NLA_PUT_U8(skb, IFLA_OPERSTATE,
168 vport_is_running(port)
169 ? vport_get_operstate(port)
173 NLA_PUT(skb, IFLA_ADDRESS, ETH_ALEN, vport_get_addr(port));
175 return nlmsg_end(skb, nlh);
178 nlmsg_cancel(skb, nlh);
182 /* Caller must hold RTNL lock. */
183 static void dp_ifinfo_notify(int event, struct vport *port)
188 skb = nlmsg_new(br_nlmsg_size(), GFP_KERNEL);
192 err = dp_fill_ifinfo(skb, port, event, 0);
194 /* -EMSGSIZE implies BUG in br_nlmsg_size() */
195 WARN_ON(err == -EMSGSIZE);
199 rtnl_notify(skb, &init_net, 0, RTNLGRP_LINK, NULL, GFP_KERNEL);
203 rtnl_set_sk_err(&init_net, RTNLGRP_LINK, err);
206 static void release_dp(struct kobject *kobj)
208 struct datapath *dp = container_of(kobj, struct datapath, ifobj);
212 static struct kobj_type dp_ktype = {
213 .release = release_dp
216 static void destroy_dp_rcu(struct rcu_head *rcu)
218 struct datapath *dp = container_of(rcu, struct datapath, rcu);
220 flow_tbl_destroy(dp->table);
221 free_percpu(dp->stats_percpu);
222 kobject_put(&dp->ifobj);
225 /* Called with RTNL lock and genl_lock. */
226 static struct vport *new_vport(const struct vport_parms *parms)
230 vport = vport_add(parms);
231 if (!IS_ERR(vport)) {
232 struct datapath *dp = parms->dp;
234 rcu_assign_pointer(dp->ports[parms->port_no], vport);
235 list_add(&vport->node, &dp->port_list);
237 dp_ifinfo_notify(RTM_NEWLINK, vport);
243 /* Called with RTNL lock. */
244 void dp_detach_port(struct vport *p)
248 if (p->port_no != OVSP_LOCAL)
250 dp_ifinfo_notify(RTM_DELLINK, p);
252 /* First drop references to device. */
254 rcu_assign_pointer(p->dp->ports[p->port_no], NULL);
256 /* Then destroy it. */
260 /* Must be called with rcu_read_lock. */
261 void dp_process_received_packet(struct vport *p, struct sk_buff *skb)
263 struct datapath *dp = p->dp;
264 struct sw_flow *flow;
265 struct dp_stats_percpu *stats;
266 int stats_counter_off;
269 OVS_CB(skb)->vport = p;
271 if (!OVS_CB(skb)->flow) {
272 struct sw_flow_key key;
276 /* Extract flow from 'skb' into 'key'. */
277 error = flow_extract(skb, p->port_no, &key, &key_len, &is_frag);
278 if (unlikely(error)) {
283 if (is_frag && dp->drop_frags) {
285 stats_counter_off = offsetof(struct dp_stats_percpu, n_frags);
290 flow = flow_tbl_lookup(rcu_dereference(dp->table), &key, key_len);
291 if (unlikely(!flow)) {
292 struct dp_upcall_info upcall;
294 upcall.cmd = OVS_PACKET_CMD_MISS;
297 upcall.sample_pool = 0;
298 upcall.actions = NULL;
299 upcall.actions_len = 0;
300 dp_upcall(dp, skb, &upcall);
301 stats_counter_off = offsetof(struct dp_stats_percpu, n_missed);
305 OVS_CB(skb)->flow = flow;
308 stats_counter_off = offsetof(struct dp_stats_percpu, n_hit);
309 flow_used(OVS_CB(skb)->flow, skb);
310 execute_actions(dp, skb);
313 /* Update datapath statistics. */
315 stats = per_cpu_ptr(dp->stats_percpu, smp_processor_id());
317 write_seqcount_begin(&stats->seqlock);
318 (*(u64 *)((u8 *)stats + stats_counter_off))++;
319 write_seqcount_end(&stats->seqlock);
324 static void copy_and_csum_skb(struct sk_buff *skb, void *to)
326 u16 csum_start, csum_offset;
329 get_skb_csum_pointers(skb, &csum_start, &csum_offset);
330 csum_start -= skb_headroom(skb);
332 skb_copy_bits(skb, 0, to, csum_start);
334 csum = skb_copy_and_csum_bits(skb, csum_start, to + csum_start,
335 skb->len - csum_start, 0);
336 *(__sum16 *)(to + csum_start + csum_offset) = csum_fold(csum);
339 static struct genl_family dp_packet_genl_family = {
340 .id = GENL_ID_GENERATE,
341 .hdrsize = sizeof(struct ovs_header),
342 .name = OVS_PACKET_FAMILY,
344 .maxattr = OVS_PACKET_ATTR_MAX
347 /* Generic Netlink multicast groups for upcalls.
349 * We really want three unique multicast groups per datapath, but we can't even
350 * get one, because genl_register_mc_group() takes genl_lock, which is also
351 * held during Generic Netlink message processing, so trying to acquire
352 * multicast groups during OVS_DP_NEW processing deadlocks. Instead, we
353 * preallocate a few groups and use them round-robin for datapaths. Collision
354 * isn't fatal--multicast listeners should check that the family is the one
355 * that they want and discard others--but it wastes time and memory to receive
358 #define PACKET_N_MC_GROUPS 16
359 static struct genl_multicast_group packet_mc_groups[PACKET_N_MC_GROUPS];
361 static u32 packet_mc_group(struct datapath *dp, u8 cmd)
364 BUILD_BUG_ON_NOT_POWER_OF_2(PACKET_N_MC_GROUPS);
366 idx = jhash_2words(dp->dp_ifindex, cmd, 0) & (PACKET_N_MC_GROUPS - 1);
367 return packet_mc_groups[idx].id;
370 static int packet_register_mc_groups(void)
374 for (i = 0; i < PACKET_N_MC_GROUPS; i++) {
375 struct genl_multicast_group *group = &packet_mc_groups[i];
378 sprintf(group->name, "packet%d", i);
379 error = genl_register_mc_group(&dp_packet_genl_family, group);
386 int dp_upcall(struct datapath *dp, struct sk_buff *skb, const struct dp_upcall_info *upcall_info)
388 struct dp_stats_percpu *stats;
391 WARN_ON_ONCE(skb_shared(skb));
393 forward_ip_summed(skb, true);
395 /* Break apart GSO packets into their component pieces. Otherwise
396 * userspace may try to stuff a 64kB packet into a 1500-byte MTU. */
397 if (skb_is_gso(skb)) {
398 struct sk_buff *nskb = skb_gso_segment(skb, NETIF_F_SG | NETIF_F_HW_CSUM);
409 err = queue_userspace_packets(dp, skb, upcall_info);
417 stats = per_cpu_ptr(dp->stats_percpu, smp_processor_id());
419 write_seqcount_begin(&stats->seqlock);
421 write_seqcount_end(&stats->seqlock);
428 /* Send each packet in the 'skb' list to userspace for 'dp' as directed by
429 * 'upcall_info'. There will be only one packet unless we broke up a GSO
432 static int queue_userspace_packets(struct datapath *dp, struct sk_buff *skb,
433 const struct dp_upcall_info *upcall_info)
435 u32 group = packet_mc_group(dp, upcall_info->cmd);
436 struct sk_buff *nskb;
440 struct ovs_header *upcall;
441 struct sk_buff *user_skb; /* to be queued to userspace */
448 err = vlan_deaccel_tag(skb);
452 if (nla_attr_size(skb->len) > USHRT_MAX)
455 len = sizeof(struct ovs_header);
456 len += nla_total_size(skb->len);
457 len += nla_total_size(FLOW_BUFSIZE);
458 if (upcall_info->userdata)
459 len += nla_total_size(8);
460 if (upcall_info->sample_pool)
461 len += nla_total_size(4);
462 if (upcall_info->actions_len)
463 len += nla_total_size(upcall_info->actions_len);
465 user_skb = genlmsg_new(len, GFP_ATOMIC);
467 netlink_set_err(INIT_NET_GENL_SOCK, 0, group, -ENOBUFS);
471 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family, 0, upcall_info->cmd);
472 upcall->dp_ifindex = dp->dp_ifindex;
474 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
475 flow_to_nlattrs(upcall_info->key, user_skb);
476 nla_nest_end(user_skb, nla);
478 if (upcall_info->userdata)
479 nla_put_u64(user_skb, OVS_PACKET_ATTR_USERDATA, upcall_info->userdata);
480 if (upcall_info->sample_pool)
481 nla_put_u32(user_skb, OVS_PACKET_ATTR_SAMPLE_POOL, upcall_info->sample_pool);
482 if (upcall_info->actions_len) {
483 const struct nlattr *actions = upcall_info->actions;
484 u32 actions_len = upcall_info->actions_len;
486 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_ACTIONS);
487 memcpy(__skb_put(user_skb, actions_len), actions, actions_len);
488 nla_nest_end(user_skb, nla);
491 nla = __nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, skb->len);
492 if (skb->ip_summed == CHECKSUM_PARTIAL)
493 copy_and_csum_skb(skb, nla_data(nla));
495 skb_copy_bits(skb, 0, nla_data(nla), skb->len);
497 err = genlmsg_multicast(user_skb, 0, group, GFP_ATOMIC);
508 while ((skb = nskb) != NULL) {
515 /* Called with genl_mutex. */
516 static int flush_flows(int dp_ifindex)
518 struct flow_table *old_table;
519 struct flow_table *new_table;
522 dp = get_dp(dp_ifindex);
526 old_table = get_table_protected(dp);
527 new_table = flow_tbl_alloc(TBL_MIN_BUCKETS);
531 rcu_assign_pointer(dp->table, new_table);
533 flow_tbl_deferred_destroy(old_table);
537 static int validate_actions(const struct nlattr *attr)
539 const struct nlattr *a;
542 nla_for_each_nested(a, attr, rem) {
543 static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
544 [OVS_ACTION_ATTR_OUTPUT] = 4,
545 [OVS_ACTION_ATTR_USERSPACE] = 8,
546 [OVS_ACTION_ATTR_PUSH_VLAN] = 2,
547 [OVS_ACTION_ATTR_POP_VLAN] = 0,
548 [OVS_ACTION_ATTR_SET_DL_SRC] = ETH_ALEN,
549 [OVS_ACTION_ATTR_SET_DL_DST] = ETH_ALEN,
550 [OVS_ACTION_ATTR_SET_NW_SRC] = 4,
551 [OVS_ACTION_ATTR_SET_NW_DST] = 4,
552 [OVS_ACTION_ATTR_SET_NW_TOS] = 1,
553 [OVS_ACTION_ATTR_SET_TP_SRC] = 2,
554 [OVS_ACTION_ATTR_SET_TP_DST] = 2,
555 [OVS_ACTION_ATTR_SET_TUNNEL] = 8,
556 [OVS_ACTION_ATTR_SET_PRIORITY] = 4,
557 [OVS_ACTION_ATTR_POP_PRIORITY] = 0,
559 int type = nla_type(a);
561 if (type > OVS_ACTION_ATTR_MAX || nla_len(a) != action_lens[type])
565 case OVS_ACTION_ATTR_UNSPEC:
568 case OVS_ACTION_ATTR_USERSPACE:
569 case OVS_ACTION_ATTR_POP_VLAN:
570 case OVS_ACTION_ATTR_SET_DL_SRC:
571 case OVS_ACTION_ATTR_SET_DL_DST:
572 case OVS_ACTION_ATTR_SET_NW_SRC:
573 case OVS_ACTION_ATTR_SET_NW_DST:
574 case OVS_ACTION_ATTR_SET_TP_SRC:
575 case OVS_ACTION_ATTR_SET_TP_DST:
576 case OVS_ACTION_ATTR_SET_TUNNEL:
577 case OVS_ACTION_ATTR_SET_PRIORITY:
578 case OVS_ACTION_ATTR_POP_PRIORITY:
579 /* No validation needed. */
582 case OVS_ACTION_ATTR_OUTPUT:
583 if (nla_get_u32(a) >= DP_MAX_PORTS)
587 case OVS_ACTION_ATTR_PUSH_VLAN:
588 if (nla_get_be16(a) & htons(VLAN_CFI_MASK))
592 case OVS_ACTION_ATTR_SET_NW_TOS:
593 if (nla_get_u8(a) & INET_ECN_MASK)
607 static void clear_stats(struct sw_flow *flow)
611 flow->packet_count = 0;
612 flow->byte_count = 0;
615 static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
617 struct ovs_header *ovs_header = info->userhdr;
618 struct nlattr **a = info->attrs;
619 struct sw_flow_actions *acts;
620 struct sk_buff *packet;
621 struct sw_flow *flow;
630 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
631 !a[OVS_PACKET_ATTR_ACTIONS] ||
632 nla_len(a[OVS_PACKET_ATTR_PACKET]) < ETH_HLEN)
635 err = validate_actions(a[OVS_PACKET_ATTR_ACTIONS]);
639 len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
640 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
644 skb_reserve(packet, NET_IP_ALIGN);
646 memcpy(__skb_put(packet, len), nla_data(a[OVS_PACKET_ATTR_PACKET]), len);
648 skb_reset_mac_header(packet);
649 eth = eth_hdr(packet);
651 /* Normally, setting the skb 'protocol' field would be handled by a
652 * call to eth_type_trans(), but it assumes there's a sending
653 * device, which we may not have. */
654 if (ntohs(eth->h_proto) >= 1536)
655 packet->protocol = eth->h_proto;
657 packet->protocol = htons(ETH_P_802_2);
659 /* Build an sw_flow for sending this packet. */
665 err = flow_extract(packet, -1, &flow->key, &key_len, &is_frag);
669 err = flow_metadata_from_nlattrs(&flow->key.eth.in_port,
670 &flow->key.eth.tun_id,
671 a[OVS_PACKET_ATTR_KEY]);
675 flow->hash = flow_hash(&flow->key, key_len);
677 acts = flow_actions_alloc(a[OVS_PACKET_ATTR_ACTIONS]);
681 rcu_assign_pointer(flow->sf_acts, acts);
683 OVS_CB(packet)->flow = flow;
686 dp = get_dp(ovs_header->dp_ifindex);
691 if (flow->key.eth.in_port < DP_MAX_PORTS)
692 OVS_CB(packet)->vport = get_vport_protected(dp,
693 flow->key.eth.in_port);
695 err = execute_actions(dp, packet);
711 static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
712 [OVS_PACKET_ATTR_PACKET] = { .type = NLA_UNSPEC },
713 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
714 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
717 static struct genl_ops dp_packet_genl_ops[] = {
718 { .cmd = OVS_PACKET_CMD_EXECUTE,
719 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
720 .policy = packet_policy,
721 .doit = ovs_packet_cmd_execute
725 static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats)
728 struct flow_table *table = get_table_protected(dp);
730 stats->n_flows = flow_tbl_count(table);
732 stats->n_frags = stats->n_hit = stats->n_missed = stats->n_lost = 0;
733 for_each_possible_cpu(i) {
734 const struct dp_stats_percpu *percpu_stats;
735 struct dp_stats_percpu local_stats;
738 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
741 seqcount = read_seqcount_begin(&percpu_stats->seqlock);
742 local_stats = *percpu_stats;
743 } while (read_seqcount_retry(&percpu_stats->seqlock, seqcount));
745 stats->n_frags += local_stats.n_frags;
746 stats->n_hit += local_stats.n_hit;
747 stats->n_missed += local_stats.n_missed;
748 stats->n_lost += local_stats.n_lost;
752 static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
753 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
754 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
755 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
758 static struct genl_family dp_flow_genl_family = {
759 .id = GENL_ID_GENERATE,
760 .hdrsize = sizeof(struct ovs_header),
761 .name = OVS_FLOW_FAMILY,
763 .maxattr = OVS_FLOW_ATTR_MAX
766 static struct genl_multicast_group dp_flow_multicast_group = {
767 .name = OVS_FLOW_MCGROUP
770 /* Called with genl_lock. */
771 static int ovs_flow_cmd_fill_info(struct sw_flow *flow, struct datapath *dp,
772 struct sk_buff *skb, u32 pid, u32 seq, u32 flags, u8 cmd)
774 const int skb_orig_len = skb->len;
775 const struct sw_flow_actions *sf_acts;
776 struct ovs_flow_stats stats;
777 struct ovs_header *ovs_header;
783 sf_acts = rcu_dereference_protected(flow->sf_acts,
784 lockdep_genl_is_held());
786 ovs_header = genlmsg_put(skb, pid, seq, &dp_flow_genl_family, flags, cmd);
790 ovs_header->dp_ifindex = dp->dp_ifindex;
792 nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
794 goto nla_put_failure;
795 err = flow_to_nlattrs(&flow->key, skb);
798 nla_nest_end(skb, nla);
800 spin_lock_bh(&flow->lock);
802 stats.n_packets = flow->packet_count;
803 stats.n_bytes = flow->byte_count;
804 tcp_flags = flow->tcp_flags;
805 spin_unlock_bh(&flow->lock);
808 NLA_PUT_U64(skb, OVS_FLOW_ATTR_USED, flow_used_time(used));
811 NLA_PUT(skb, OVS_FLOW_ATTR_STATS, sizeof(struct ovs_flow_stats), &stats);
814 NLA_PUT_U8(skb, OVS_FLOW_ATTR_TCP_FLAGS, tcp_flags);
816 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
817 * this is the first flow to be dumped into 'skb'. This is unusual for
818 * Netlink but individual action lists can be longer than
819 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
820 * The userspace caller can always fetch the actions separately if it
821 * really wants them. (Most userspace callers in fact don't care.)
823 * This can only fail for dump operations because the skb is always
824 * properly sized for single flows.
826 err = nla_put(skb, OVS_FLOW_ATTR_ACTIONS, sf_acts->actions_len,
828 if (err < 0 && skb_orig_len)
831 return genlmsg_end(skb, ovs_header);
836 genlmsg_cancel(skb, ovs_header);
840 static struct sk_buff *ovs_flow_cmd_alloc_info(struct sw_flow *flow)
842 const struct sw_flow_actions *sf_acts;
845 sf_acts = rcu_dereference_protected(flow->sf_acts,
846 lockdep_genl_is_held());
848 len = nla_total_size(FLOW_BUFSIZE); /* OVS_FLOW_ATTR_KEY */
849 len += nla_total_size(sf_acts->actions_len); /* OVS_FLOW_ATTR_ACTIONS */
850 len += nla_total_size(sizeof(struct ovs_flow_stats)); /* OVS_FLOW_ATTR_STATS */
851 len += nla_total_size(1); /* OVS_FLOW_ATTR_TCP_FLAGS */
852 len += nla_total_size(8); /* OVS_FLOW_ATTR_USED */
853 return genlmsg_new(NLMSG_ALIGN(sizeof(struct ovs_header)) + len, GFP_KERNEL);
856 static struct sk_buff *ovs_flow_cmd_build_info(struct sw_flow *flow, struct datapath *dp,
857 u32 pid, u32 seq, u8 cmd)
862 skb = ovs_flow_cmd_alloc_info(flow);
864 return ERR_PTR(-ENOMEM);
866 retval = ovs_flow_cmd_fill_info(flow, dp, skb, pid, seq, 0, cmd);
871 static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info)
873 struct nlattr **a = info->attrs;
874 struct ovs_header *ovs_header = info->userhdr;
875 struct sw_flow_key key;
876 struct sw_flow *flow;
877 struct sk_buff *reply;
879 struct flow_table *table;
885 if (!a[OVS_FLOW_ATTR_KEY])
887 error = flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
891 /* Validate actions. */
892 if (a[OVS_FLOW_ATTR_ACTIONS]) {
893 error = validate_actions(a[OVS_FLOW_ATTR_ACTIONS]);
896 } else if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW) {
901 dp = get_dp(ovs_header->dp_ifindex);
906 table = get_table_protected(dp);
907 flow = flow_tbl_lookup(table, &key, key_len);
909 struct sw_flow_actions *acts;
911 /* Bail out if we're not allowed to create a new flow. */
913 if (info->genlhdr->cmd == OVS_FLOW_CMD_SET)
916 /* Expand table, if necessary, to make room. */
917 if (flow_tbl_need_to_expand(table)) {
918 struct flow_table *new_table;
920 new_table = flow_tbl_expand(table);
921 if (!IS_ERR(new_table)) {
922 rcu_assign_pointer(dp->table, new_table);
923 flow_tbl_deferred_destroy(table);
924 table = get_table_protected(dp);
931 error = PTR_ERR(flow);
937 /* Obtain actions. */
938 acts = flow_actions_alloc(a[OVS_FLOW_ATTR_ACTIONS]);
939 error = PTR_ERR(acts);
941 goto error_free_flow;
942 rcu_assign_pointer(flow->sf_acts, acts);
944 /* Put flow in bucket. */
945 flow->hash = flow_hash(&key, key_len);
946 flow_tbl_insert(table, flow);
948 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_pid,
949 info->snd_seq, OVS_FLOW_CMD_NEW);
951 /* We found a matching flow. */
952 struct sw_flow_actions *old_acts;
954 /* Bail out if we're not allowed to modify an existing flow.
955 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
956 * because Generic Netlink treats the latter as a dump
957 * request. We also accept NLM_F_EXCL in case that bug ever
961 if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW &&
962 info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL))
965 /* Update actions. */
966 old_acts = rcu_dereference_protected(flow->sf_acts,
967 lockdep_genl_is_held());
968 if (a[OVS_FLOW_ATTR_ACTIONS] &&
969 (old_acts->actions_len != nla_len(a[OVS_FLOW_ATTR_ACTIONS]) ||
970 memcmp(old_acts->actions, nla_data(a[OVS_FLOW_ATTR_ACTIONS]),
971 old_acts->actions_len))) {
972 struct sw_flow_actions *new_acts;
974 new_acts = flow_actions_alloc(a[OVS_FLOW_ATTR_ACTIONS]);
975 error = PTR_ERR(new_acts);
976 if (IS_ERR(new_acts))
979 rcu_assign_pointer(flow->sf_acts, new_acts);
980 flow_deferred_free_acts(old_acts);
983 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_pid,
984 info->snd_seq, OVS_FLOW_CMD_NEW);
987 if (a[OVS_FLOW_ATTR_CLEAR]) {
988 spin_lock_bh(&flow->lock);
990 spin_unlock_bh(&flow->lock);
995 genl_notify(reply, genl_info_net(info), info->snd_pid,
996 dp_flow_multicast_group.id, info->nlhdr, GFP_KERNEL);
998 netlink_set_err(INIT_NET_GENL_SOCK, 0,
999 dp_flow_multicast_group.id, PTR_ERR(reply));
1008 static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1010 struct nlattr **a = info->attrs;
1011 struct ovs_header *ovs_header = info->userhdr;
1012 struct sw_flow_key key;
1013 struct sk_buff *reply;
1014 struct sw_flow *flow;
1015 struct datapath *dp;
1016 struct flow_table *table;
1020 if (!a[OVS_FLOW_ATTR_KEY])
1022 err = flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
1026 dp = get_dp(ovs_header->dp_ifindex);
1030 table = get_table_protected(dp);
1031 flow = flow_tbl_lookup(table, &key, key_len);
1035 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_pid, info->snd_seq, OVS_FLOW_CMD_NEW);
1037 return PTR_ERR(reply);
1039 return genlmsg_reply(reply, info);
1042 static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1044 struct nlattr **a = info->attrs;
1045 struct ovs_header *ovs_header = info->userhdr;
1046 struct sw_flow_key key;
1047 struct sk_buff *reply;
1048 struct sw_flow *flow;
1049 struct datapath *dp;
1050 struct flow_table *table;
1054 if (!a[OVS_FLOW_ATTR_KEY])
1055 return flush_flows(ovs_header->dp_ifindex);
1056 err = flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
1060 dp = get_dp(ovs_header->dp_ifindex);
1064 table = get_table_protected(dp);
1065 flow = flow_tbl_lookup(table, &key, key_len);
1069 reply = ovs_flow_cmd_alloc_info(flow);
1073 flow_tbl_remove(table, flow);
1075 err = ovs_flow_cmd_fill_info(flow, dp, reply, info->snd_pid,
1076 info->snd_seq, 0, OVS_FLOW_CMD_DEL);
1079 flow_deferred_free(flow);
1081 genl_notify(reply, genl_info_net(info), info->snd_pid,
1082 dp_flow_multicast_group.id, info->nlhdr, GFP_KERNEL);
1086 static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1088 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1089 struct datapath *dp;
1091 dp = get_dp(ovs_header->dp_ifindex);
1096 struct sw_flow *flow;
1099 bucket = cb->args[0];
1101 flow = flow_tbl_next(get_table_protected(dp), &bucket, &obj);
1105 if (ovs_flow_cmd_fill_info(flow, dp, skb, NETLINK_CB(cb->skb).pid,
1106 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1107 OVS_FLOW_CMD_NEW) < 0)
1110 cb->args[0] = bucket;
1116 static struct genl_ops dp_flow_genl_ops[] = {
1117 { .cmd = OVS_FLOW_CMD_NEW,
1118 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1119 .policy = flow_policy,
1120 .doit = ovs_flow_cmd_new_or_set
1122 { .cmd = OVS_FLOW_CMD_DEL,
1123 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1124 .policy = flow_policy,
1125 .doit = ovs_flow_cmd_del
1127 { .cmd = OVS_FLOW_CMD_GET,
1128 .flags = 0, /* OK for unprivileged users. */
1129 .policy = flow_policy,
1130 .doit = ovs_flow_cmd_get,
1131 .dumpit = ovs_flow_cmd_dump
1133 { .cmd = OVS_FLOW_CMD_SET,
1134 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1135 .policy = flow_policy,
1136 .doit = ovs_flow_cmd_new_or_set,
1140 static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1141 #ifdef HAVE_NLA_NUL_STRING
1142 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1144 [OVS_DP_ATTR_IPV4_FRAGS] = { .type = NLA_U32 },
1145 [OVS_DP_ATTR_SAMPLING] = { .type = NLA_U32 },
1148 static struct genl_family dp_datapath_genl_family = {
1149 .id = GENL_ID_GENERATE,
1150 .hdrsize = sizeof(struct ovs_header),
1151 .name = OVS_DATAPATH_FAMILY,
1153 .maxattr = OVS_DP_ATTR_MAX
1156 static struct genl_multicast_group dp_datapath_multicast_group = {
1157 .name = OVS_DATAPATH_MCGROUP
1160 static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
1161 u32 pid, u32 seq, u32 flags, u8 cmd)
1163 struct ovs_header *ovs_header;
1167 ovs_header = genlmsg_put(skb, pid, seq, &dp_datapath_genl_family,
1172 ovs_header->dp_ifindex = dp->dp_ifindex;
1175 err = nla_put_string(skb, OVS_DP_ATTR_NAME, dp_name(dp));
1178 goto nla_put_failure;
1180 nla = nla_reserve(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats));
1182 goto nla_put_failure;
1183 get_dp_stats(dp, nla_data(nla));
1185 NLA_PUT_U32(skb, OVS_DP_ATTR_IPV4_FRAGS,
1186 dp->drop_frags ? OVS_DP_FRAG_DROP : OVS_DP_FRAG_ZERO);
1188 if (dp->sflow_probability)
1189 NLA_PUT_U32(skb, OVS_DP_ATTR_SAMPLING, dp->sflow_probability);
1191 nla = nla_nest_start(skb, OVS_DP_ATTR_MCGROUPS);
1193 goto nla_put_failure;
1194 NLA_PUT_U32(skb, OVS_PACKET_CMD_MISS, packet_mc_group(dp, OVS_PACKET_CMD_MISS));
1195 NLA_PUT_U32(skb, OVS_PACKET_CMD_ACTION, packet_mc_group(dp, OVS_PACKET_CMD_ACTION));
1196 NLA_PUT_U32(skb, OVS_PACKET_CMD_SAMPLE, packet_mc_group(dp, OVS_PACKET_CMD_SAMPLE));
1197 nla_nest_end(skb, nla);
1199 return genlmsg_end(skb, ovs_header);
1202 genlmsg_cancel(skb, ovs_header);
1207 static struct sk_buff *ovs_dp_cmd_build_info(struct datapath *dp, u32 pid,
1210 struct sk_buff *skb;
1213 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1215 return ERR_PTR(-ENOMEM);
1217 retval = ovs_dp_cmd_fill_info(dp, skb, pid, seq, 0, cmd);
1220 return ERR_PTR(retval);
1225 static int ovs_dp_cmd_validate(struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1227 if (a[OVS_DP_ATTR_IPV4_FRAGS]) {
1228 u32 frags = nla_get_u32(a[OVS_DP_ATTR_IPV4_FRAGS]);
1230 if (frags != OVS_DP_FRAG_ZERO && frags != OVS_DP_FRAG_DROP)
1234 return CHECK_NUL_STRING(a[OVS_DP_ATTR_NAME], IFNAMSIZ - 1);
1237 /* Called with genl_mutex and optionally with RTNL lock also. */
1238 static struct datapath *lookup_datapath(struct ovs_header *ovs_header, struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1240 struct datapath *dp;
1242 if (!a[OVS_DP_ATTR_NAME])
1243 dp = get_dp(ovs_header->dp_ifindex);
1245 struct vport *vport;
1248 vport = vport_locate(nla_data(a[OVS_DP_ATTR_NAME]));
1249 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
1252 return dp ? dp : ERR_PTR(-ENODEV);
1255 /* Called with genl_mutex. */
1256 static void change_datapath(struct datapath *dp, struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1258 if (a[OVS_DP_ATTR_IPV4_FRAGS])
1259 dp->drop_frags = nla_get_u32(a[OVS_DP_ATTR_IPV4_FRAGS]) == OVS_DP_FRAG_DROP;
1260 if (a[OVS_DP_ATTR_SAMPLING])
1261 dp->sflow_probability = nla_get_u32(a[OVS_DP_ATTR_SAMPLING]);
1264 static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1266 struct nlattr **a = info->attrs;
1267 struct vport_parms parms;
1268 struct sk_buff *reply;
1269 struct datapath *dp;
1270 struct vport *vport;
1274 if (!a[OVS_DP_ATTR_NAME])
1277 err = ovs_dp_cmd_validate(a);
1283 if (!try_module_get(THIS_MODULE))
1284 goto err_unlock_rtnl;
1287 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1289 goto err_put_module;
1290 INIT_LIST_HEAD(&dp->port_list);
1292 /* Initialize kobject for bridge. This will be added as
1293 * /sys/class/net/<devname>/brif later, if sysfs is enabled. */
1294 dp->ifobj.kset = NULL;
1295 kobject_init(&dp->ifobj, &dp_ktype);
1297 /* Allocate table. */
1299 rcu_assign_pointer(dp->table, flow_tbl_alloc(TBL_MIN_BUCKETS));
1303 /* Set up our datapath device. */
1304 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1305 parms.type = OVS_VPORT_TYPE_INTERNAL;
1306 parms.options = NULL;
1308 parms.port_no = OVSP_LOCAL;
1309 vport = new_vport(&parms);
1310 if (IS_ERR(vport)) {
1311 err = PTR_ERR(vport);
1315 goto err_destroy_table;
1317 dp->dp_ifindex = vport_get_ifindex(vport);
1320 dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
1321 if (!dp->stats_percpu) {
1323 goto err_destroy_local_port;
1326 change_datapath(dp, a);
1328 reply = ovs_dp_cmd_build_info(dp, info->snd_pid, info->snd_seq, OVS_DP_CMD_NEW);
1329 err = PTR_ERR(reply);
1331 goto err_destroy_local_port;
1333 list_add_tail(&dp->list_node, &dps);
1334 dp_sysfs_add_dp(dp);
1338 genl_notify(reply, genl_info_net(info), info->snd_pid,
1339 dp_datapath_multicast_group.id, info->nlhdr, GFP_KERNEL);
1342 err_destroy_local_port:
1343 dp_detach_port(get_vport_protected(dp, OVSP_LOCAL));
1345 flow_tbl_destroy(get_table_protected(dp));
1349 module_put(THIS_MODULE);
1356 static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1358 struct vport *vport, *next_vport;
1359 struct sk_buff *reply;
1360 struct datapath *dp;
1363 err = ovs_dp_cmd_validate(info->attrs);
1368 dp = lookup_datapath(info->userhdr, info->attrs);
1373 reply = ovs_dp_cmd_build_info(dp, info->snd_pid, info->snd_seq, OVS_DP_CMD_DEL);
1374 err = PTR_ERR(reply);
1378 list_for_each_entry_safe (vport, next_vport, &dp->port_list, node)
1379 if (vport->port_no != OVSP_LOCAL)
1380 dp_detach_port(vport);
1382 dp_sysfs_del_dp(dp);
1383 list_del(&dp->list_node);
1384 dp_detach_port(get_vport_protected(dp, OVSP_LOCAL));
1386 /* rtnl_unlock() will wait until all the references to devices that
1387 * are pending unregistration have been dropped. We do it here to
1388 * ensure that any internal devices (which contain DP pointers) are
1389 * fully destroyed before freeing the datapath.
1393 call_rcu(&dp->rcu, destroy_dp_rcu);
1394 module_put(THIS_MODULE);
1396 genl_notify(reply, genl_info_net(info), info->snd_pid,
1397 dp_datapath_multicast_group.id, info->nlhdr, GFP_KERNEL);
1407 static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1409 struct sk_buff *reply;
1410 struct datapath *dp;
1413 err = ovs_dp_cmd_validate(info->attrs);
1417 dp = lookup_datapath(info->userhdr, info->attrs);
1421 change_datapath(dp, info->attrs);
1423 reply = ovs_dp_cmd_build_info(dp, info->snd_pid, info->snd_seq, OVS_DP_CMD_NEW);
1424 if (IS_ERR(reply)) {
1425 err = PTR_ERR(reply);
1426 netlink_set_err(INIT_NET_GENL_SOCK, 0,
1427 dp_datapath_multicast_group.id, err);
1431 genl_notify(reply, genl_info_net(info), info->snd_pid,
1432 dp_datapath_multicast_group.id, info->nlhdr, GFP_KERNEL);
1436 static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1438 struct sk_buff *reply;
1439 struct datapath *dp;
1442 err = ovs_dp_cmd_validate(info->attrs);
1446 dp = lookup_datapath(info->userhdr, info->attrs);
1450 reply = ovs_dp_cmd_build_info(dp, info->snd_pid, info->snd_seq, OVS_DP_CMD_NEW);
1452 return PTR_ERR(reply);
1454 return genlmsg_reply(reply, info);
1457 static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1459 struct datapath *dp;
1460 int skip = cb->args[0];
1463 list_for_each_entry (dp, &dps, list_node) {
1466 if (ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).pid,
1467 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1468 OVS_DP_CMD_NEW) < 0)
1478 static struct genl_ops dp_datapath_genl_ops[] = {
1479 { .cmd = OVS_DP_CMD_NEW,
1480 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1481 .policy = datapath_policy,
1482 .doit = ovs_dp_cmd_new
1484 { .cmd = OVS_DP_CMD_DEL,
1485 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1486 .policy = datapath_policy,
1487 .doit = ovs_dp_cmd_del
1489 { .cmd = OVS_DP_CMD_GET,
1490 .flags = 0, /* OK for unprivileged users. */
1491 .policy = datapath_policy,
1492 .doit = ovs_dp_cmd_get,
1493 .dumpit = ovs_dp_cmd_dump
1495 { .cmd = OVS_DP_CMD_SET,
1496 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1497 .policy = datapath_policy,
1498 .doit = ovs_dp_cmd_set,
1502 static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
1503 #ifdef HAVE_NLA_NUL_STRING
1504 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1505 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
1506 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
1507 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
1508 [OVS_VPORT_ATTR_ADDRESS] = { .len = ETH_ALEN },
1510 [OVS_VPORT_ATTR_STATS] = { .minlen = sizeof(struct ovs_vport_stats) },
1511 [OVS_VPORT_ATTR_ADDRESS] = { .minlen = ETH_ALEN },
1513 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
1516 static struct genl_family dp_vport_genl_family = {
1517 .id = GENL_ID_GENERATE,
1518 .hdrsize = sizeof(struct ovs_header),
1519 .name = OVS_VPORT_FAMILY,
1521 .maxattr = OVS_VPORT_ATTR_MAX
1524 struct genl_multicast_group dp_vport_multicast_group = {
1525 .name = OVS_VPORT_MCGROUP
1528 /* Called with RTNL lock or RCU read lock. */
1529 static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
1530 u32 pid, u32 seq, u32 flags, u8 cmd)
1532 struct ovs_header *ovs_header;
1537 ovs_header = genlmsg_put(skb, pid, seq, &dp_vport_genl_family,
1542 ovs_header->dp_ifindex = vport->dp->dp_ifindex;
1544 NLA_PUT_U32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no);
1545 NLA_PUT_U32(skb, OVS_VPORT_ATTR_TYPE, vport_get_type(vport));
1546 NLA_PUT_STRING(skb, OVS_VPORT_ATTR_NAME, vport_get_name(vport));
1548 nla = nla_reserve(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats));
1550 goto nla_put_failure;
1552 vport_get_stats(vport, nla_data(nla));
1554 NLA_PUT(skb, OVS_VPORT_ATTR_ADDRESS, ETH_ALEN, vport_get_addr(vport));
1556 err = vport_get_options(vport, skb);
1557 if (err == -EMSGSIZE)
1560 ifindex = vport_get_ifindex(vport);
1562 NLA_PUT_U32(skb, OVS_VPORT_ATTR_IFINDEX, ifindex);
1564 return genlmsg_end(skb, ovs_header);
1569 genlmsg_cancel(skb, ovs_header);
1573 /* Called with RTNL lock or RCU read lock. */
1574 struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 pid,
1577 struct sk_buff *skb;
1580 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1582 return ERR_PTR(-ENOMEM);
1584 retval = ovs_vport_cmd_fill_info(vport, skb, pid, seq, 0, cmd);
1587 return ERR_PTR(retval);
1592 static int ovs_vport_cmd_validate(struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1594 return CHECK_NUL_STRING(a[OVS_VPORT_ATTR_NAME], IFNAMSIZ - 1);
1597 /* Called with RTNL lock or RCU read lock. */
1598 static struct vport *lookup_vport(struct ovs_header *ovs_header,
1599 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1601 struct datapath *dp;
1602 struct vport *vport;
1604 if (a[OVS_VPORT_ATTR_NAME]) {
1605 vport = vport_locate(nla_data(a[OVS_VPORT_ATTR_NAME]));
1607 return ERR_PTR(-ENODEV);
1609 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1610 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1612 if (port_no >= DP_MAX_PORTS)
1613 return ERR_PTR(-EFBIG);
1615 dp = get_dp(ovs_header->dp_ifindex);
1617 return ERR_PTR(-ENODEV);
1619 vport = get_vport_protected(dp, port_no);
1621 return ERR_PTR(-ENOENT);
1624 return ERR_PTR(-EINVAL);
1627 /* Called with RTNL lock. */
1628 static int change_vport(struct vport *vport, struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1632 if (a[OVS_VPORT_ATTR_STATS])
1633 vport_set_stats(vport, nla_data(a[OVS_VPORT_ATTR_STATS]));
1635 if (a[OVS_VPORT_ATTR_ADDRESS])
1636 err = vport_set_addr(vport, nla_data(a[OVS_VPORT_ATTR_ADDRESS]));
1641 static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1643 struct nlattr **a = info->attrs;
1644 struct ovs_header *ovs_header = info->userhdr;
1645 struct vport_parms parms;
1646 struct sk_buff *reply;
1647 struct vport *vport;
1648 struct datapath *dp;
1653 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE])
1656 err = ovs_vport_cmd_validate(a);
1661 dp = get_dp(ovs_header->dp_ifindex);
1666 if (a[OVS_VPORT_ATTR_PORT_NO]) {
1667 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1670 if (port_no >= DP_MAX_PORTS)
1673 vport = get_vport_protected(dp, port_no);
1678 for (port_no = 1; ; port_no++) {
1679 if (port_no >= DP_MAX_PORTS) {
1683 vport = get_vport_protected(dp, port_no);
1689 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
1690 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1691 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
1693 parms.port_no = port_no;
1695 vport = new_vport(&parms);
1696 err = PTR_ERR(vport);
1700 dp_sysfs_add_if(vport);
1702 err = change_vport(vport, a);
1704 reply = ovs_vport_cmd_build_info(vport, info->snd_pid,
1705 info->snd_seq, OVS_VPORT_CMD_NEW);
1707 err = PTR_ERR(reply);
1710 dp_detach_port(vport);
1713 genl_notify(reply, genl_info_net(info), info->snd_pid,
1714 dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1723 static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
1725 struct nlattr **a = info->attrs;
1726 struct sk_buff *reply;
1727 struct vport *vport;
1730 err = ovs_vport_cmd_validate(a);
1735 vport = lookup_vport(info->userhdr, a);
1736 err = PTR_ERR(vport);
1741 if (a[OVS_VPORT_ATTR_OPTIONS])
1742 err = vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
1744 err = change_vport(vport, a);
1746 reply = ovs_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
1748 if (IS_ERR(reply)) {
1749 err = PTR_ERR(reply);
1750 netlink_set_err(INIT_NET_GENL_SOCK, 0,
1751 dp_vport_multicast_group.id, err);
1755 genl_notify(reply, genl_info_net(info), info->snd_pid,
1756 dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1764 static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
1766 struct nlattr **a = info->attrs;
1767 struct sk_buff *reply;
1768 struct vport *vport;
1771 err = ovs_vport_cmd_validate(a);
1776 vport = lookup_vport(info->userhdr, a);
1777 err = PTR_ERR(vport);
1781 if (vport->port_no == OVSP_LOCAL) {
1786 reply = ovs_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
1788 err = PTR_ERR(reply);
1792 dp_detach_port(vport);
1794 genl_notify(reply, genl_info_net(info), info->snd_pid,
1795 dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1803 static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
1805 struct nlattr **a = info->attrs;
1806 struct ovs_header *ovs_header = info->userhdr;
1807 struct sk_buff *reply;
1808 struct vport *vport;
1811 err = ovs_vport_cmd_validate(a);
1816 vport = lookup_vport(ovs_header, a);
1817 err = PTR_ERR(vport);
1821 reply = ovs_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
1823 err = PTR_ERR(reply);
1829 return genlmsg_reply(reply, info);
1837 static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1839 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1840 struct datapath *dp;
1844 dp = get_dp(ovs_header->dp_ifindex);
1849 for (port_no = cb->args[0]; port_no < DP_MAX_PORTS; port_no++) {
1850 struct vport *vport;
1852 vport = get_vport_protected(dp, port_no);
1856 if (ovs_vport_cmd_fill_info(vport, skb, NETLINK_CB(cb->skb).pid,
1857 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1858 OVS_VPORT_CMD_NEW) < 0)
1863 cb->args[0] = port_no;
1869 static struct genl_ops dp_vport_genl_ops[] = {
1870 { .cmd = OVS_VPORT_CMD_NEW,
1871 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1872 .policy = vport_policy,
1873 .doit = ovs_vport_cmd_new
1875 { .cmd = OVS_VPORT_CMD_DEL,
1876 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1877 .policy = vport_policy,
1878 .doit = ovs_vport_cmd_del
1880 { .cmd = OVS_VPORT_CMD_GET,
1881 .flags = 0, /* OK for unprivileged users. */
1882 .policy = vport_policy,
1883 .doit = ovs_vport_cmd_get,
1884 .dumpit = ovs_vport_cmd_dump
1886 { .cmd = OVS_VPORT_CMD_SET,
1887 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1888 .policy = vport_policy,
1889 .doit = ovs_vport_cmd_set,
1893 struct genl_family_and_ops {
1894 struct genl_family *family;
1895 struct genl_ops *ops;
1897 struct genl_multicast_group *group;
1900 static const struct genl_family_and_ops dp_genl_families[] = {
1901 { &dp_datapath_genl_family,
1902 dp_datapath_genl_ops, ARRAY_SIZE(dp_datapath_genl_ops),
1903 &dp_datapath_multicast_group },
1904 { &dp_vport_genl_family,
1905 dp_vport_genl_ops, ARRAY_SIZE(dp_vport_genl_ops),
1906 &dp_vport_multicast_group },
1907 { &dp_flow_genl_family,
1908 dp_flow_genl_ops, ARRAY_SIZE(dp_flow_genl_ops),
1909 &dp_flow_multicast_group },
1910 { &dp_packet_genl_family,
1911 dp_packet_genl_ops, ARRAY_SIZE(dp_packet_genl_ops),
1915 static void dp_unregister_genl(int n_families)
1919 for (i = 0; i < n_families; i++)
1920 genl_unregister_family(dp_genl_families[i].family);
1923 static int dp_register_genl(void)
1930 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
1931 const struct genl_family_and_ops *f = &dp_genl_families[i];
1933 err = genl_register_family_with_ops(f->family, f->ops,
1940 err = genl_register_mc_group(f->family, f->group);
1946 err = packet_register_mc_groups();
1952 dp_unregister_genl(n_registered);
1956 static int __init dp_init(void)
1958 struct sk_buff *dummy_skb;
1961 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > sizeof(dummy_skb->cb));
1963 printk("Open vSwitch %s, built "__DATE__" "__TIME__"\n", VERSION BUILDNR);
1971 goto error_tnl_exit;
1975 goto error_flow_exit;
1977 err = register_netdevice_notifier(&dp_device_notifier);
1979 goto error_vport_exit;
1981 err = dp_register_genl();
1983 goto error_unreg_notifier;
1987 error_unreg_notifier:
1988 unregister_netdevice_notifier(&dp_device_notifier);
1999 static void dp_cleanup(void)
2002 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2003 unregister_netdevice_notifier(&dp_device_notifier);
2009 module_init(dp_init);
2010 module_exit(dp_cleanup);
2012 MODULE_DESCRIPTION("Open vSwitch switching datapath");
2013 MODULE_LICENSE("GPL");