2 * Copyright (c) 2007-2014 Nicira, Inc.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of version 2 of the GNU General Public
6 * License as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21 #include <linux/init.h>
22 #include <linux/module.h>
23 #include <linux/if_arp.h>
24 #include <linux/if_vlan.h>
27 #include <linux/jhash.h>
28 #include <linux/delay.h>
29 #include <linux/time.h>
30 #include <linux/etherdevice.h>
31 #include <linux/genetlink.h>
32 #include <linux/kernel.h>
33 #include <linux/kthread.h>
34 #include <linux/mutex.h>
35 #include <linux/percpu.h>
36 #include <linux/rcupdate.h>
37 #include <linux/tcp.h>
38 #include <linux/udp.h>
39 #include <linux/ethtool.h>
40 #include <linux/wait.h>
41 #include <asm/div64.h>
42 #include <linux/highmem.h>
43 #include <linux/netfilter_bridge.h>
44 #include <linux/netfilter_ipv4.h>
45 #include <linux/inetdevice.h>
46 #include <linux/list.h>
47 #include <linux/openvswitch.h>
48 #include <linux/rculist.h>
49 #include <linux/dmi.h>
50 #include <net/genetlink.h>
51 #include <net/net_namespace.h>
52 #include <net/netns/generic.h>
56 #include "flow_table.h"
57 #include "flow_netlink.h"
58 #include "vport-internal_dev.h"
59 #include "vport-netdev.h"
61 int ovs_net_id __read_mostly
;
62 EXPORT_SYMBOL_GPL(ovs_net_id
);
64 static struct genl_family dp_packet_genl_family
;
65 static struct genl_family dp_flow_genl_family
;
66 static struct genl_family dp_datapath_genl_family
;
68 static const struct nla_policy flow_policy
[];
70 static const struct genl_multicast_group ovs_dp_flow_multicast_group
= {
71 .name
= OVS_FLOW_MCGROUP
,
74 static const struct genl_multicast_group ovs_dp_datapath_multicast_group
= {
75 .name
= OVS_DATAPATH_MCGROUP
,
78 static const struct genl_multicast_group ovs_dp_vport_multicast_group
= {
79 .name
= OVS_VPORT_MCGROUP
,
82 /* Check if need to build a reply message.
83 * OVS userspace sets the NLM_F_ECHO flag if it needs the reply. */
84 static bool ovs_must_notify(struct genl_family
*family
, struct genl_info
*info
,
87 return info
->nlhdr
->nlmsg_flags
& NLM_F_ECHO
||
88 genl_has_listeners(family
, genl_info_net(info
), group
);
91 static void ovs_notify(struct genl_family
*family
,
92 struct sk_buff
*skb
, struct genl_info
*info
)
94 genl_notify(family
, skb
, genl_info_net(info
), info
->snd_portid
,
95 0, info
->nlhdr
, GFP_KERNEL
);
101 * All writes e.g. Writes to device state (add/remove datapath, port, set
102 * operations on vports, etc.), Writes to other state (flow table
103 * modifications, set miscellaneous datapath parameters, etc.) are protected
106 * Reads are protected by RCU.
108 * There are a few special cases (mostly stats) that have their own
109 * synchronization but they nest under all of above and don't interact with
112 * The RTNL lock nests inside ovs_mutex.
115 static DEFINE_MUTEX(ovs_mutex
);
119 mutex_lock(&ovs_mutex
);
122 void ovs_unlock(void)
124 mutex_unlock(&ovs_mutex
);
127 #ifdef CONFIG_LOCKDEP
128 int lockdep_ovsl_is_held(void)
131 return lockdep_is_held(&ovs_mutex
);
135 EXPORT_SYMBOL_GPL(lockdep_ovsl_is_held
);
138 static struct vport
*new_vport(const struct vport_parms
*);
139 static int queue_gso_packets(struct datapath
*dp
, struct sk_buff
*,
140 const struct sw_flow_key
*,
141 const struct dp_upcall_info
*);
142 static int queue_userspace_packet(struct datapath
*dp
, struct sk_buff
*,
143 const struct sw_flow_key
*,
144 const struct dp_upcall_info
*);
146 /* Must be called with rcu_read_lock. */
147 static struct datapath
*get_dp_rcu(struct net
*net
, int dp_ifindex
)
149 struct net_device
*dev
= dev_get_by_index_rcu(net
, dp_ifindex
);
152 struct vport
*vport
= ovs_internal_dev_get_vport(dev
);
160 /* The caller must hold either ovs_mutex or rcu_read_lock to keep the
161 * returned dp pointer valid.
163 static inline struct datapath
*get_dp(struct net
*net
, int dp_ifindex
)
167 WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_ovsl_is_held());
169 dp
= get_dp_rcu(net
, dp_ifindex
);
175 /* Must be called with rcu_read_lock or ovs_mutex. */
176 const char *ovs_dp_name(const struct datapath
*dp
)
178 struct vport
*vport
= ovs_vport_ovsl_rcu(dp
, OVSP_LOCAL
);
179 return vport
->ops
->get_name(vport
);
182 static int get_dpifindex(const struct datapath
*dp
)
189 local
= ovs_vport_rcu(dp
, OVSP_LOCAL
);
191 ifindex
= netdev_vport_priv(local
)->dev
->ifindex
;
200 static void destroy_dp_rcu(struct rcu_head
*rcu
)
202 struct datapath
*dp
= container_of(rcu
, struct datapath
, rcu
);
204 ovs_flow_tbl_destroy(&dp
->table
);
205 free_percpu(dp
->stats_percpu
);
206 release_net(ovs_dp_get_net(dp
));
211 static struct hlist_head
*vport_hash_bucket(const struct datapath
*dp
,
214 return &dp
->ports
[port_no
& (DP_VPORT_HASH_BUCKETS
- 1)];
217 /* Called with ovs_mutex or RCU read lock. */
218 struct vport
*ovs_lookup_vport(const struct datapath
*dp
, u16 port_no
)
221 struct hlist_head
*head
;
223 head
= vport_hash_bucket(dp
, port_no
);
224 hlist_for_each_entry_rcu(vport
, head
, dp_hash_node
) {
225 if (vport
->port_no
== port_no
)
231 /* Called with ovs_mutex. */
232 static struct vport
*new_vport(const struct vport_parms
*parms
)
236 vport
= ovs_vport_add(parms
);
237 if (!IS_ERR(vport
)) {
238 struct datapath
*dp
= parms
->dp
;
239 struct hlist_head
*head
= vport_hash_bucket(dp
, vport
->port_no
);
241 hlist_add_head_rcu(&vport
->dp_hash_node
, head
);
246 void ovs_dp_detach_port(struct vport
*p
)
250 /* First drop references to device. */
251 hlist_del_rcu(&p
->dp_hash_node
);
253 /* Then destroy it. */
257 /* Must be called with rcu_read_lock. */
258 void ovs_dp_process_packet(struct sk_buff
*skb
, struct sw_flow_key
*key
)
260 const struct vport
*p
= OVS_CB(skb
)->input_vport
;
261 struct datapath
*dp
= p
->dp
;
262 struct sw_flow
*flow
;
263 struct sw_flow_actions
*sf_acts
;
264 struct dp_stats_percpu
*stats
;
268 stats
= this_cpu_ptr(dp
->stats_percpu
);
271 flow
= ovs_flow_tbl_lookup_stats(&dp
->table
, key
, &n_mask_hit
);
272 if (unlikely(!flow
)) {
273 struct dp_upcall_info upcall
;
276 upcall
.cmd
= OVS_PACKET_CMD_MISS
;
277 upcall
.userdata
= NULL
;
278 upcall
.portid
= ovs_vport_find_upcall_portid(p
, skb
);
279 upcall
.egress_tun_info
= NULL
;
280 error
= ovs_dp_upcall(dp
, skb
, key
, &upcall
);
285 stats_counter
= &stats
->n_missed
;
289 ovs_flow_stats_update(flow
, key
->tp
.flags
, skb
);
290 sf_acts
= rcu_dereference(flow
->sf_acts
);
291 ovs_execute_actions(dp
, skb
, sf_acts
, key
);
293 stats_counter
= &stats
->n_hit
;
296 /* Update datapath statistics. */
297 u64_stats_update_begin(&stats
->syncp
);
299 stats
->n_mask_hit
+= n_mask_hit
;
300 u64_stats_update_end(&stats
->syncp
);
303 int ovs_dp_upcall(struct datapath
*dp
, struct sk_buff
*skb
,
304 const struct sw_flow_key
*key
,
305 const struct dp_upcall_info
*upcall_info
)
307 struct dp_stats_percpu
*stats
;
310 if (upcall_info
->portid
== 0) {
315 if (!skb_is_gso(skb
))
316 err
= queue_userspace_packet(dp
, skb
, key
, upcall_info
);
318 err
= queue_gso_packets(dp
, skb
, key
, upcall_info
);
325 stats
= this_cpu_ptr(dp
->stats_percpu
);
327 u64_stats_update_begin(&stats
->syncp
);
329 u64_stats_update_end(&stats
->syncp
);
334 static int queue_gso_packets(struct datapath
*dp
, struct sk_buff
*skb
,
335 const struct sw_flow_key
*key
,
336 const struct dp_upcall_info
*upcall_info
)
338 unsigned short gso_type
= skb_shinfo(skb
)->gso_type
;
339 struct sw_flow_key later_key
;
340 struct sk_buff
*segs
, *nskb
;
341 struct ovs_skb_cb ovs_cb
;
344 ovs_cb
= *OVS_CB(skb
);
345 segs
= __skb_gso_segment(skb
, NETIF_F_SG
, false);
346 *OVS_CB(skb
) = ovs_cb
;
348 return PTR_ERR(segs
);
352 if (gso_type
& SKB_GSO_UDP
) {
353 /* The initial flow key extracted by ovs_flow_key_extract()
354 * in this case is for a first fragment, so we need to
355 * properly mark later fragments.
358 later_key
.ip
.frag
= OVS_FRAG_TYPE_LATER
;
361 /* Queue all of the segments. */
364 *OVS_CB(skb
) = ovs_cb
;
365 if (gso_type
& SKB_GSO_UDP
&& skb
!= segs
)
368 err
= queue_userspace_packet(dp
, skb
, key
, upcall_info
);
372 } while ((skb
= skb
->next
));
374 /* Free all of the segments. */
382 } while ((skb
= nskb
));
386 static size_t upcall_msg_size(const struct dp_upcall_info
*upcall_info
,
389 size_t size
= NLMSG_ALIGN(sizeof(struct ovs_header
))
390 + nla_total_size(hdrlen
) /* OVS_PACKET_ATTR_PACKET */
391 + nla_total_size(ovs_key_attr_size()); /* OVS_PACKET_ATTR_KEY */
393 /* OVS_PACKET_ATTR_USERDATA */
394 if (upcall_info
->userdata
)
395 size
+= NLA_ALIGN(upcall_info
->userdata
->nla_len
);
397 /* OVS_PACKET_ATTR_EGRESS_TUN_KEY */
398 if (upcall_info
->egress_tun_info
)
399 size
+= nla_total_size(ovs_tun_key_attr_size());
404 static int queue_userspace_packet(struct datapath
*dp
, struct sk_buff
*skb
,
405 const struct sw_flow_key
*key
,
406 const struct dp_upcall_info
*upcall_info
)
408 struct ovs_header
*upcall
;
409 struct sk_buff
*nskb
= NULL
;
410 struct sk_buff
*user_skb
= NULL
; /* to be queued to userspace */
412 struct genl_info info
= {
413 .dst_sk
= ovs_dp_get_net(dp
)->genl_sock
,
414 .snd_portid
= upcall_info
->portid
,
420 dp_ifindex
= get_dpifindex(dp
);
424 if (skb_vlan_tag_present(skb
)) {
425 nskb
= skb_clone(skb
, GFP_ATOMIC
);
429 nskb
= __vlan_hwaccel_push_inside(nskb
);
436 if (nla_attr_size(skb
->len
) > USHRT_MAX
) {
441 /* Complete checksum if needed */
442 if (skb
->ip_summed
== CHECKSUM_PARTIAL
&&
443 (err
= skb_checksum_help(skb
)))
446 /* Older versions of OVS user space enforce alignment of the last
447 * Netlink attribute to NLA_ALIGNTO which would require extensive
448 * padding logic. Only perform zerocopy if padding is not required.
450 if (dp
->user_features
& OVS_DP_F_UNALIGNED
)
451 hlen
= skb_zerocopy_headlen(skb
);
455 len
= upcall_msg_size(upcall_info
, hlen
);
456 user_skb
= genlmsg_new_unicast(len
, &info
, 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 err
= ovs_nla_put_key(key
, key
, OVS_PACKET_ATTR_KEY
, false, user_skb
);
469 if (upcall_info
->userdata
)
470 __nla_put(user_skb
, OVS_PACKET_ATTR_USERDATA
,
471 nla_len(upcall_info
->userdata
),
472 nla_data(upcall_info
->userdata
));
474 if (upcall_info
->egress_tun_info
) {
475 nla
= nla_nest_start(user_skb
, OVS_PACKET_ATTR_EGRESS_TUN_KEY
);
476 err
= ovs_nla_put_egress_tunnel_key(user_skb
,
477 upcall_info
->egress_tun_info
);
479 nla_nest_end(user_skb
, nla
);
482 /* Only reserve room for attribute header, packet data is added
483 * in skb_zerocopy() */
484 if (!(nla
= nla_reserve(user_skb
, OVS_PACKET_ATTR_PACKET
, 0))) {
488 nla
->nla_len
= nla_attr_size(skb
->len
);
490 err
= skb_zerocopy(user_skb
, skb
, skb
->len
, hlen
);
494 /* Pad OVS_PACKET_ATTR_PACKET if linear copy was performed */
495 if (!(dp
->user_features
& OVS_DP_F_UNALIGNED
)) {
496 size_t plen
= NLA_ALIGN(user_skb
->len
) - user_skb
->len
;
499 memset(skb_put(user_skb
, plen
), 0, plen
);
502 ((struct nlmsghdr
*) user_skb
->data
)->nlmsg_len
= user_skb
->len
;
504 err
= genlmsg_unicast(ovs_dp_get_net(dp
), user_skb
, upcall_info
->portid
);
514 static int ovs_packet_cmd_execute(struct sk_buff
*skb
, struct genl_info
*info
)
516 struct ovs_header
*ovs_header
= info
->userhdr
;
517 struct nlattr
**a
= info
->attrs
;
518 struct sw_flow_actions
*acts
;
519 struct sk_buff
*packet
;
520 struct sw_flow
*flow
;
521 struct sw_flow_actions
*sf_acts
;
524 struct vport
*input_vport
;
527 bool log
= !a
[OVS_PACKET_ATTR_PROBE
];
530 if (!a
[OVS_PACKET_ATTR_PACKET
] || !a
[OVS_PACKET_ATTR_KEY
] ||
531 !a
[OVS_PACKET_ATTR_ACTIONS
])
534 len
= nla_len(a
[OVS_PACKET_ATTR_PACKET
]);
535 packet
= __dev_alloc_skb(NET_IP_ALIGN
+ len
, GFP_KERNEL
);
539 skb_reserve(packet
, NET_IP_ALIGN
);
541 nla_memcpy(__skb_put(packet
, len
), a
[OVS_PACKET_ATTR_PACKET
], len
);
543 skb_reset_mac_header(packet
);
544 eth
= eth_hdr(packet
);
546 /* Normally, setting the skb 'protocol' field would be handled by a
547 * call to eth_type_trans(), but it assumes there's a sending
548 * device, which we may not have. */
549 if (ntohs(eth
->h_proto
) >= ETH_P_802_3_MIN
)
550 packet
->protocol
= eth
->h_proto
;
552 packet
->protocol
= htons(ETH_P_802_2
);
554 /* Build an sw_flow for sending this packet. */
555 flow
= ovs_flow_alloc();
560 err
= ovs_flow_key_extract_userspace(a
[OVS_PACKET_ATTR_KEY
], packet
,
565 err
= ovs_nla_copy_actions(a
[OVS_PACKET_ATTR_ACTIONS
],
566 &flow
->key
, &acts
, log
);
570 rcu_assign_pointer(flow
->sf_acts
, acts
);
571 OVS_CB(packet
)->egress_tun_info
= NULL
;
572 packet
->priority
= flow
->key
.phy
.priority
;
573 packet
->mark
= flow
->key
.phy
.skb_mark
;
576 dp
= get_dp_rcu(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
581 input_vport
= ovs_vport_rcu(dp
, flow
->key
.phy
.in_port
);
583 input_vport
= ovs_vport_rcu(dp
, OVSP_LOCAL
);
588 OVS_CB(packet
)->input_vport
= input_vport
;
589 sf_acts
= rcu_dereference(flow
->sf_acts
);
592 err
= ovs_execute_actions(dp
, packet
, sf_acts
, &flow
->key
);
596 ovs_flow_free(flow
, false);
602 ovs_flow_free(flow
, false);
609 static const struct nla_policy packet_policy
[OVS_PACKET_ATTR_MAX
+ 1] = {
610 [OVS_PACKET_ATTR_PACKET
] = { .len
= ETH_HLEN
},
611 [OVS_PACKET_ATTR_KEY
] = { .type
= NLA_NESTED
},
612 [OVS_PACKET_ATTR_ACTIONS
] = { .type
= NLA_NESTED
},
613 [OVS_PACKET_ATTR_PROBE
] = { .type
= NLA_FLAG
},
616 static const struct genl_ops dp_packet_genl_ops
[] = {
617 { .cmd
= OVS_PACKET_CMD_EXECUTE
,
618 .flags
= GENL_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
619 .policy
= packet_policy
,
620 .doit
= ovs_packet_cmd_execute
624 static struct genl_family dp_packet_genl_family
= {
625 .id
= GENL_ID_GENERATE
,
626 .hdrsize
= sizeof(struct ovs_header
),
627 .name
= OVS_PACKET_FAMILY
,
628 .version
= OVS_PACKET_VERSION
,
629 .maxattr
= OVS_PACKET_ATTR_MAX
,
631 .parallel_ops
= true,
632 .ops
= dp_packet_genl_ops
,
633 .n_ops
= ARRAY_SIZE(dp_packet_genl_ops
),
636 static void get_dp_stats(const struct datapath
*dp
, struct ovs_dp_stats
*stats
,
637 struct ovs_dp_megaflow_stats
*mega_stats
)
641 memset(mega_stats
, 0, sizeof(*mega_stats
));
643 stats
->n_flows
= ovs_flow_tbl_count(&dp
->table
);
644 mega_stats
->n_masks
= ovs_flow_tbl_num_masks(&dp
->table
);
646 stats
->n_hit
= stats
->n_missed
= stats
->n_lost
= 0;
648 for_each_possible_cpu(i
) {
649 const struct dp_stats_percpu
*percpu_stats
;
650 struct dp_stats_percpu local_stats
;
653 percpu_stats
= per_cpu_ptr(dp
->stats_percpu
, i
);
656 start
= u64_stats_fetch_begin_irq(&percpu_stats
->syncp
);
657 local_stats
= *percpu_stats
;
658 } while (u64_stats_fetch_retry_irq(&percpu_stats
->syncp
, start
));
660 stats
->n_hit
+= local_stats
.n_hit
;
661 stats
->n_missed
+= local_stats
.n_missed
;
662 stats
->n_lost
+= local_stats
.n_lost
;
663 mega_stats
->n_mask_hit
+= local_stats
.n_mask_hit
;
667 static bool should_fill_key(const struct sw_flow_id
*sfid
, uint32_t ufid_flags
)
669 return ovs_identifier_is_ufid(sfid
) &&
670 !(ufid_flags
& OVS_UFID_F_OMIT_KEY
);
673 static bool should_fill_mask(uint32_t ufid_flags
)
675 return !(ufid_flags
& OVS_UFID_F_OMIT_MASK
);
678 static bool should_fill_actions(uint32_t ufid_flags
)
680 return !(ufid_flags
& OVS_UFID_F_OMIT_ACTIONS
);
683 static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions
*acts
,
684 const struct sw_flow_id
*sfid
,
687 size_t len
= NLMSG_ALIGN(sizeof(struct ovs_header
));
689 /* OVS_FLOW_ATTR_UFID */
690 if (sfid
&& ovs_identifier_is_ufid(sfid
))
691 len
+= nla_total_size(sfid
->ufid_len
);
693 /* OVS_FLOW_ATTR_KEY */
694 if (!sfid
|| should_fill_key(sfid
, ufid_flags
))
695 len
+= nla_total_size(ovs_key_attr_size());
697 /* OVS_FLOW_ATTR_MASK */
698 if (should_fill_mask(ufid_flags
))
699 len
+= nla_total_size(ovs_key_attr_size());
701 /* OVS_FLOW_ATTR_ACTIONS */
702 if (should_fill_actions(ufid_flags
))
703 len
+= nla_total_size(acts
->actions_len
);
706 + nla_total_size(sizeof(struct ovs_flow_stats
)) /* OVS_FLOW_ATTR_STATS */
707 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
708 + nla_total_size(8); /* OVS_FLOW_ATTR_USED */
711 /* Called with ovs_mutex or RCU read lock. */
712 static int ovs_flow_cmd_fill_stats(const struct sw_flow
*flow
,
715 struct ovs_flow_stats stats
;
719 ovs_flow_stats_get(flow
, &stats
, &used
, &tcp_flags
);
722 nla_put_u64(skb
, OVS_FLOW_ATTR_USED
, ovs_flow_used_time(used
)))
725 if (stats
.n_packets
&&
726 nla_put(skb
, OVS_FLOW_ATTR_STATS
, sizeof(struct ovs_flow_stats
), &stats
))
729 if ((u8
)ntohs(tcp_flags
) &&
730 nla_put_u8(skb
, OVS_FLOW_ATTR_TCP_FLAGS
, (u8
)ntohs(tcp_flags
)))
736 /* Called with ovs_mutex or RCU read lock. */
737 static int ovs_flow_cmd_fill_actions(const struct sw_flow
*flow
,
738 struct sk_buff
*skb
, int skb_orig_len
)
740 struct nlattr
*start
;
743 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
744 * this is the first flow to be dumped into 'skb'. This is unusual for
745 * Netlink but individual action lists can be longer than
746 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
747 * The userspace caller can always fetch the actions separately if it
748 * really wants them. (Most userspace callers in fact don't care.)
750 * This can only fail for dump operations because the skb is always
751 * properly sized for single flows.
753 start
= nla_nest_start(skb
, OVS_FLOW_ATTR_ACTIONS
);
755 const struct sw_flow_actions
*sf_acts
;
757 sf_acts
= rcu_dereference_ovsl(flow
->sf_acts
);
758 err
= ovs_nla_put_actions(sf_acts
->actions
,
759 sf_acts
->actions_len
, skb
);
762 nla_nest_end(skb
, start
);
767 nla_nest_cancel(skb
, start
);
769 } else if (skb_orig_len
) {
776 /* Called with ovs_mutex or RCU read lock. */
777 static int ovs_flow_cmd_fill_info(const struct sw_flow
*flow
, int dp_ifindex
,
778 struct sk_buff
*skb
, u32 portid
,
779 u32 seq
, u32 flags
, u8 cmd
, u32 ufid_flags
)
781 const int skb_orig_len
= skb
->len
;
782 struct ovs_header
*ovs_header
;
785 ovs_header
= genlmsg_put(skb
, portid
, seq
, &dp_flow_genl_family
,
790 ovs_header
->dp_ifindex
= dp_ifindex
;
792 err
= ovs_nla_put_identifier(flow
, skb
);
796 if (should_fill_key(&flow
->id
, ufid_flags
)) {
797 err
= ovs_nla_put_masked_key(flow
, skb
);
802 if (should_fill_mask(ufid_flags
)) {
803 err
= ovs_nla_put_mask(flow
, skb
);
808 err
= ovs_flow_cmd_fill_stats(flow
, skb
);
812 if (should_fill_actions(ufid_flags
)) {
813 err
= ovs_flow_cmd_fill_actions(flow
, skb
, skb_orig_len
);
818 genlmsg_end(skb
, ovs_header
);
822 genlmsg_cancel(skb
, ovs_header
);
826 /* May not be called with RCU read lock. */
827 static struct sk_buff
*ovs_flow_cmd_alloc_info(const struct sw_flow_actions
*acts
,
828 const struct sw_flow_id
*sfid
,
829 struct genl_info
*info
,
836 if (!always
&& !ovs_must_notify(&dp_flow_genl_family
, info
, 0))
839 len
= ovs_flow_cmd_msg_size(acts
, sfid
, ufid_flags
);
840 skb
= genlmsg_new_unicast(len
, info
, GFP_KERNEL
);
842 return ERR_PTR(-ENOMEM
);
847 /* Called with ovs_mutex. */
848 static struct sk_buff
*ovs_flow_cmd_build_info(const struct sw_flow
*flow
,
850 struct genl_info
*info
, u8 cmd
,
851 bool always
, u32 ufid_flags
)
856 skb
= ovs_flow_cmd_alloc_info(ovsl_dereference(flow
->sf_acts
),
857 &flow
->id
, info
, always
, ufid_flags
);
858 if (IS_ERR_OR_NULL(skb
))
861 retval
= ovs_flow_cmd_fill_info(flow
, dp_ifindex
, skb
,
862 info
->snd_portid
, info
->snd_seq
, 0,
868 static int ovs_flow_cmd_new(struct sk_buff
*skb
, struct genl_info
*info
)
870 struct nlattr
**a
= info
->attrs
;
871 struct ovs_header
*ovs_header
= info
->userhdr
;
872 struct sw_flow
*flow
= NULL
, *new_flow
;
873 struct sw_flow_mask mask
;
874 struct sk_buff
*reply
;
876 struct sw_flow_key key
;
877 struct sw_flow_actions
*acts
;
878 struct sw_flow_match match
;
879 u32 ufid_flags
= ovs_nla_get_ufid_flags(a
[OVS_FLOW_ATTR_UFID_FLAGS
]);
881 bool log
= !a
[OVS_FLOW_ATTR_PROBE
];
883 /* Must have key and actions. */
885 if (!a
[OVS_FLOW_ATTR_KEY
]) {
886 OVS_NLERR(log
, "Flow key attr not present in new flow.");
889 if (!a
[OVS_FLOW_ATTR_ACTIONS
]) {
890 OVS_NLERR(log
, "Flow actions attr not present in new flow.");
894 /* Most of the time we need to allocate a new flow, do it before
897 new_flow
= ovs_flow_alloc();
898 if (IS_ERR(new_flow
)) {
899 error
= PTR_ERR(new_flow
);
904 ovs_match_init(&match
, &key
, &mask
);
905 error
= ovs_nla_get_match(&match
, a
[OVS_FLOW_ATTR_KEY
],
906 a
[OVS_FLOW_ATTR_MASK
], log
);
910 ovs_flow_mask_key(&new_flow
->key
, &key
, &mask
);
912 /* Extract flow identifier. */
913 error
= ovs_nla_get_identifier(&new_flow
->id
, a
[OVS_FLOW_ATTR_UFID
],
918 /* Validate actions. */
919 error
= ovs_nla_copy_actions(a
[OVS_FLOW_ATTR_ACTIONS
], &new_flow
->key
,
922 OVS_NLERR(log
, "Flow actions may not be safe on all matching packets.");
926 reply
= ovs_flow_cmd_alloc_info(acts
, &new_flow
->id
, info
, false,
929 error
= PTR_ERR(reply
);
934 dp
= get_dp(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
940 /* Check if this is a duplicate flow */
941 if (ovs_identifier_is_ufid(&new_flow
->id
))
942 flow
= ovs_flow_tbl_lookup_ufid(&dp
->table
, &new_flow
->id
);
944 flow
= ovs_flow_tbl_lookup(&dp
->table
, &key
);
946 rcu_assign_pointer(new_flow
->sf_acts
, acts
);
948 /* Put flow in bucket. */
949 error
= ovs_flow_tbl_insert(&dp
->table
, new_flow
, &mask
);
950 if (unlikely(error
)) {
955 if (unlikely(reply
)) {
956 error
= ovs_flow_cmd_fill_info(new_flow
,
957 ovs_header
->dp_ifindex
,
958 reply
, info
->snd_portid
,
966 struct sw_flow_actions
*old_acts
;
968 /* Bail out if we're not allowed to modify an existing flow.
969 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
970 * because Generic Netlink treats the latter as a dump
971 * request. We also accept NLM_F_EXCL in case that bug ever
974 if (unlikely(info
->nlhdr
->nlmsg_flags
& (NLM_F_CREATE
979 /* The flow identifier has to be the same for flow updates.
980 * Look for any overlapping flow.
982 if (unlikely(!ovs_flow_cmp(flow
, &match
))) {
983 if (ovs_identifier_is_key(&flow
->id
))
984 flow
= ovs_flow_tbl_lookup_exact(&dp
->table
,
986 else /* UFID matches but key is different */
993 /* Update actions. */
994 old_acts
= ovsl_dereference(flow
->sf_acts
);
995 rcu_assign_pointer(flow
->sf_acts
, acts
);
997 if (unlikely(reply
)) {
998 error
= ovs_flow_cmd_fill_info(flow
,
999 ovs_header
->dp_ifindex
,
1000 reply
, info
->snd_portid
,
1008 ovs_nla_free_flow_actions(old_acts
);
1009 ovs_flow_free(new_flow
, false);
1013 ovs_notify(&dp_flow_genl_family
, reply
, info
);
1022 ovs_flow_free(new_flow
, false);
1027 /* Factor out action copy to avoid "Wframe-larger-than=1024" warning. */
1028 static struct sw_flow_actions
*get_flow_actions(const struct nlattr
*a
,
1029 const struct sw_flow_key
*key
,
1030 const struct sw_flow_mask
*mask
,
1033 struct sw_flow_actions
*acts
;
1034 struct sw_flow_key masked_key
;
1037 ovs_flow_mask_key(&masked_key
, key
, mask
);
1038 error
= ovs_nla_copy_actions(a
, &masked_key
, &acts
, log
);
1041 "Actions may not be safe on all matching packets");
1042 return ERR_PTR(error
);
1048 static int ovs_flow_cmd_set(struct sk_buff
*skb
, struct genl_info
*info
)
1050 struct nlattr
**a
= info
->attrs
;
1051 struct ovs_header
*ovs_header
= info
->userhdr
;
1052 struct sw_flow_key key
;
1053 struct sw_flow
*flow
;
1054 struct sw_flow_mask mask
;
1055 struct sk_buff
*reply
= NULL
;
1056 struct datapath
*dp
;
1057 struct sw_flow_actions
*old_acts
= NULL
, *acts
= NULL
;
1058 struct sw_flow_match match
;
1059 struct sw_flow_id sfid
;
1060 u32 ufid_flags
= ovs_nla_get_ufid_flags(a
[OVS_FLOW_ATTR_UFID_FLAGS
]);
1062 bool log
= !a
[OVS_FLOW_ATTR_PROBE
];
1067 if (!a
[OVS_FLOW_ATTR_KEY
]) {
1068 OVS_NLERR(log
, "Flow key attribute not present in set flow.");
1072 ufid_present
= ovs_nla_get_ufid(&sfid
, a
[OVS_FLOW_ATTR_UFID
], log
);
1073 ovs_match_init(&match
, &key
, &mask
);
1074 error
= ovs_nla_get_match(&match
, a
[OVS_FLOW_ATTR_KEY
],
1075 a
[OVS_FLOW_ATTR_MASK
], log
);
1079 /* Validate actions. */
1080 if (a
[OVS_FLOW_ATTR_ACTIONS
]) {
1081 acts
= get_flow_actions(a
[OVS_FLOW_ATTR_ACTIONS
], &key
, &mask
,
1084 error
= PTR_ERR(acts
);
1088 /* Can allocate before locking if have acts. */
1089 reply
= ovs_flow_cmd_alloc_info(acts
, &sfid
, info
, false,
1091 if (IS_ERR(reply
)) {
1092 error
= PTR_ERR(reply
);
1093 goto err_kfree_acts
;
1098 dp
= get_dp(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
1099 if (unlikely(!dp
)) {
1101 goto err_unlock_ovs
;
1103 /* Check that the flow exists. */
1105 flow
= ovs_flow_tbl_lookup_ufid(&dp
->table
, &sfid
);
1107 flow
= ovs_flow_tbl_lookup_exact(&dp
->table
, &match
);
1108 if (unlikely(!flow
)) {
1110 goto err_unlock_ovs
;
1113 /* Update actions, if present. */
1115 old_acts
= ovsl_dereference(flow
->sf_acts
);
1116 rcu_assign_pointer(flow
->sf_acts
, acts
);
1118 if (unlikely(reply
)) {
1119 error
= ovs_flow_cmd_fill_info(flow
,
1120 ovs_header
->dp_ifindex
,
1121 reply
, info
->snd_portid
,
1128 /* Could not alloc without acts before locking. */
1129 reply
= ovs_flow_cmd_build_info(flow
, ovs_header
->dp_ifindex
,
1130 info
, OVS_FLOW_CMD_NEW
, false,
1133 if (unlikely(IS_ERR(reply
))) {
1134 error
= PTR_ERR(reply
);
1135 goto err_unlock_ovs
;
1140 if (a
[OVS_FLOW_ATTR_CLEAR
])
1141 ovs_flow_stats_clear(flow
);
1145 ovs_notify(&dp_flow_genl_family
, reply
, info
);
1147 ovs_nla_free_flow_actions(old_acts
);
1160 static int ovs_flow_cmd_get(struct sk_buff
*skb
, struct genl_info
*info
)
1162 struct nlattr
**a
= info
->attrs
;
1163 struct ovs_header
*ovs_header
= info
->userhdr
;
1164 struct sw_flow_key key
;
1165 struct sk_buff
*reply
;
1166 struct sw_flow
*flow
;
1167 struct datapath
*dp
;
1168 struct sw_flow_match match
;
1169 struct sw_flow_id ufid
;
1170 u32 ufid_flags
= ovs_nla_get_ufid_flags(a
[OVS_FLOW_ATTR_UFID_FLAGS
]);
1172 bool log
= !a
[OVS_FLOW_ATTR_PROBE
];
1175 ufid_present
= ovs_nla_get_ufid(&ufid
, a
[OVS_FLOW_ATTR_UFID
], log
);
1176 if (a
[OVS_FLOW_ATTR_KEY
]) {
1177 ovs_match_init(&match
, &key
, NULL
);
1178 err
= ovs_nla_get_match(&match
, a
[OVS_FLOW_ATTR_KEY
], NULL
,
1180 } else if (!ufid_present
) {
1182 "Flow get message rejected, Key attribute missing.");
1189 dp
= get_dp(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
1196 flow
= ovs_flow_tbl_lookup_ufid(&dp
->table
, &ufid
);
1198 flow
= ovs_flow_tbl_lookup_exact(&dp
->table
, &match
);
1204 reply
= ovs_flow_cmd_build_info(flow
, ovs_header
->dp_ifindex
, info
,
1205 OVS_FLOW_CMD_NEW
, true, ufid_flags
);
1206 if (IS_ERR(reply
)) {
1207 err
= PTR_ERR(reply
);
1212 return genlmsg_reply(reply
, info
);
1218 static int ovs_flow_cmd_del(struct sk_buff
*skb
, struct genl_info
*info
)
1220 struct nlattr
**a
= info
->attrs
;
1221 struct ovs_header
*ovs_header
= info
->userhdr
;
1222 struct sw_flow_key key
;
1223 struct sk_buff
*reply
;
1224 struct sw_flow
*flow
= NULL
;
1225 struct datapath
*dp
;
1226 struct sw_flow_match match
;
1227 struct sw_flow_id ufid
;
1228 u32 ufid_flags
= ovs_nla_get_ufid_flags(a
[OVS_FLOW_ATTR_UFID_FLAGS
]);
1230 bool log
= !a
[OVS_FLOW_ATTR_PROBE
];
1233 ufid_present
= ovs_nla_get_ufid(&ufid
, a
[OVS_FLOW_ATTR_UFID
], log
);
1234 if (a
[OVS_FLOW_ATTR_KEY
]) {
1235 ovs_match_init(&match
, &key
, NULL
);
1236 err
= ovs_nla_get_match(&match
, a
[OVS_FLOW_ATTR_KEY
], NULL
,
1243 dp
= get_dp(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
1244 if (unlikely(!dp
)) {
1249 if (unlikely(!a
[OVS_FLOW_ATTR_KEY
] && !ufid_present
)) {
1250 err
= ovs_flow_tbl_flush(&dp
->table
);
1255 flow
= ovs_flow_tbl_lookup_ufid(&dp
->table
, &ufid
);
1257 flow
= ovs_flow_tbl_lookup_exact(&dp
->table
, &match
);
1258 if (unlikely(!flow
)) {
1263 ovs_flow_tbl_remove(&dp
->table
, flow
);
1266 reply
= ovs_flow_cmd_alloc_info((const struct sw_flow_actions __force
*) flow
->sf_acts
,
1267 &flow
->id
, info
, false, ufid_flags
);
1268 if (likely(reply
)) {
1269 if (likely(!IS_ERR(reply
))) {
1270 rcu_read_lock(); /*To keep RCU checker happy. */
1271 err
= ovs_flow_cmd_fill_info(flow
, ovs_header
->dp_ifindex
,
1272 reply
, info
->snd_portid
,
1279 ovs_notify(&dp_flow_genl_family
, reply
, info
);
1281 netlink_set_err(sock_net(skb
->sk
)->genl_sock
, 0, 0, PTR_ERR(reply
));
1285 ovs_flow_free(flow
, true);
1292 static int ovs_flow_cmd_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1294 struct nlattr
*a
[__OVS_FLOW_ATTR_MAX
];
1295 struct ovs_header
*ovs_header
= genlmsg_data(nlmsg_data(cb
->nlh
));
1296 struct table_instance
*ti
;
1297 struct datapath
*dp
;
1301 err
= genlmsg_parse(cb
->nlh
, &dp_flow_genl_family
, a
,
1302 OVS_FLOW_ATTR_MAX
, flow_policy
);
1305 ufid_flags
= ovs_nla_get_ufid_flags(a
[OVS_FLOW_ATTR_UFID_FLAGS
]);
1308 dp
= get_dp_rcu(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
1314 ti
= rcu_dereference(dp
->table
.ti
);
1316 struct sw_flow
*flow
;
1319 bucket
= cb
->args
[0];
1321 flow
= ovs_flow_tbl_dump_next(ti
, &bucket
, &obj
);
1325 if (ovs_flow_cmd_fill_info(flow
, ovs_header
->dp_ifindex
, skb
,
1326 NETLINK_CB(cb
->skb
).portid
,
1327 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
,
1328 OVS_FLOW_CMD_NEW
, ufid_flags
) < 0)
1331 cb
->args
[0] = bucket
;
1338 static const struct nla_policy flow_policy
[OVS_FLOW_ATTR_MAX
+ 1] = {
1339 [OVS_FLOW_ATTR_KEY
] = { .type
= NLA_NESTED
},
1340 [OVS_FLOW_ATTR_MASK
] = { .type
= NLA_NESTED
},
1341 [OVS_FLOW_ATTR_ACTIONS
] = { .type
= NLA_NESTED
},
1342 [OVS_FLOW_ATTR_CLEAR
] = { .type
= NLA_FLAG
},
1343 [OVS_FLOW_ATTR_PROBE
] = { .type
= NLA_FLAG
},
1344 [OVS_FLOW_ATTR_UFID
] = { .type
= NLA_UNSPEC
, .len
= 1 },
1345 [OVS_FLOW_ATTR_UFID_FLAGS
] = { .type
= NLA_U32
},
1348 static const struct genl_ops dp_flow_genl_ops
[] = {
1349 { .cmd
= OVS_FLOW_CMD_NEW
,
1350 .flags
= GENL_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1351 .policy
= flow_policy
,
1352 .doit
= ovs_flow_cmd_new
1354 { .cmd
= OVS_FLOW_CMD_DEL
,
1355 .flags
= GENL_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1356 .policy
= flow_policy
,
1357 .doit
= ovs_flow_cmd_del
1359 { .cmd
= OVS_FLOW_CMD_GET
,
1360 .flags
= 0, /* OK for unprivileged users. */
1361 .policy
= flow_policy
,
1362 .doit
= ovs_flow_cmd_get
,
1363 .dumpit
= ovs_flow_cmd_dump
1365 { .cmd
= OVS_FLOW_CMD_SET
,
1366 .flags
= GENL_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1367 .policy
= flow_policy
,
1368 .doit
= ovs_flow_cmd_set
,
1372 static struct genl_family dp_flow_genl_family
= {
1373 .id
= GENL_ID_GENERATE
,
1374 .hdrsize
= sizeof(struct ovs_header
),
1375 .name
= OVS_FLOW_FAMILY
,
1376 .version
= OVS_FLOW_VERSION
,
1377 .maxattr
= OVS_FLOW_ATTR_MAX
,
1379 .parallel_ops
= true,
1380 .ops
= dp_flow_genl_ops
,
1381 .n_ops
= ARRAY_SIZE(dp_flow_genl_ops
),
1382 .mcgrps
= &ovs_dp_flow_multicast_group
,
1386 static size_t ovs_dp_cmd_msg_size(void)
1388 size_t msgsize
= NLMSG_ALIGN(sizeof(struct ovs_header
));
1390 msgsize
+= nla_total_size(IFNAMSIZ
);
1391 msgsize
+= nla_total_size(sizeof(struct ovs_dp_stats
));
1392 msgsize
+= nla_total_size(sizeof(struct ovs_dp_megaflow_stats
));
1393 msgsize
+= nla_total_size(sizeof(u32
)); /* OVS_DP_ATTR_USER_FEATURES */
1398 /* Called with ovs_mutex. */
1399 static int ovs_dp_cmd_fill_info(struct datapath
*dp
, struct sk_buff
*skb
,
1400 u32 portid
, u32 seq
, u32 flags
, u8 cmd
)
1402 struct ovs_header
*ovs_header
;
1403 struct ovs_dp_stats dp_stats
;
1404 struct ovs_dp_megaflow_stats dp_megaflow_stats
;
1407 ovs_header
= genlmsg_put(skb
, portid
, seq
, &dp_datapath_genl_family
,
1412 ovs_header
->dp_ifindex
= get_dpifindex(dp
);
1414 err
= nla_put_string(skb
, OVS_DP_ATTR_NAME
, ovs_dp_name(dp
));
1416 goto nla_put_failure
;
1418 get_dp_stats(dp
, &dp_stats
, &dp_megaflow_stats
);
1419 if (nla_put(skb
, OVS_DP_ATTR_STATS
, sizeof(struct ovs_dp_stats
),
1421 goto nla_put_failure
;
1423 if (nla_put(skb
, OVS_DP_ATTR_MEGAFLOW_STATS
,
1424 sizeof(struct ovs_dp_megaflow_stats
),
1425 &dp_megaflow_stats
))
1426 goto nla_put_failure
;
1428 if (nla_put_u32(skb
, OVS_DP_ATTR_USER_FEATURES
, dp
->user_features
))
1429 goto nla_put_failure
;
1431 genlmsg_end(skb
, ovs_header
);
1435 genlmsg_cancel(skb
, ovs_header
);
1440 static struct sk_buff
*ovs_dp_cmd_alloc_info(struct genl_info
*info
)
1442 return genlmsg_new_unicast(ovs_dp_cmd_msg_size(), info
, GFP_KERNEL
);
1445 /* Called with rcu_read_lock or ovs_mutex. */
1446 static struct datapath
*lookup_datapath(struct net
*net
,
1447 const struct ovs_header
*ovs_header
,
1448 struct nlattr
*a
[OVS_DP_ATTR_MAX
+ 1])
1450 struct datapath
*dp
;
1452 if (!a
[OVS_DP_ATTR_NAME
])
1453 dp
= get_dp(net
, ovs_header
->dp_ifindex
);
1455 struct vport
*vport
;
1457 vport
= ovs_vport_locate(net
, nla_data(a
[OVS_DP_ATTR_NAME
]));
1458 dp
= vport
&& vport
->port_no
== OVSP_LOCAL
? vport
->dp
: NULL
;
1460 return dp
? dp
: ERR_PTR(-ENODEV
);
1463 static void ovs_dp_reset_user_features(struct sk_buff
*skb
, struct genl_info
*info
)
1465 struct datapath
*dp
;
1467 dp
= lookup_datapath(sock_net(skb
->sk
), info
->userhdr
, info
->attrs
);
1471 WARN(dp
->user_features
, "Dropping previously announced user features\n");
1472 dp
->user_features
= 0;
1475 static void ovs_dp_change(struct datapath
*dp
, struct nlattr
*a
[])
1477 if (a
[OVS_DP_ATTR_USER_FEATURES
])
1478 dp
->user_features
= nla_get_u32(a
[OVS_DP_ATTR_USER_FEATURES
]);
1481 static int ovs_dp_cmd_new(struct sk_buff
*skb
, struct genl_info
*info
)
1483 struct nlattr
**a
= info
->attrs
;
1484 struct vport_parms parms
;
1485 struct sk_buff
*reply
;
1486 struct datapath
*dp
;
1487 struct vport
*vport
;
1488 struct ovs_net
*ovs_net
;
1492 if (!a
[OVS_DP_ATTR_NAME
] || !a
[OVS_DP_ATTR_UPCALL_PID
])
1495 reply
= ovs_dp_cmd_alloc_info(info
);
1500 dp
= kzalloc(sizeof(*dp
), GFP_KERNEL
);
1502 goto err_free_reply
;
1504 ovs_dp_set_net(dp
, hold_net(sock_net(skb
->sk
)));
1506 /* Allocate table. */
1507 err
= ovs_flow_tbl_init(&dp
->table
);
1511 dp
->stats_percpu
= netdev_alloc_pcpu_stats(struct dp_stats_percpu
);
1512 if (!dp
->stats_percpu
) {
1514 goto err_destroy_table
;
1517 dp
->ports
= kmalloc(DP_VPORT_HASH_BUCKETS
* sizeof(struct hlist_head
),
1521 goto err_destroy_percpu
;
1524 for (i
= 0; i
< DP_VPORT_HASH_BUCKETS
; i
++)
1525 INIT_HLIST_HEAD(&dp
->ports
[i
]);
1527 /* Set up our datapath device. */
1528 parms
.name
= nla_data(a
[OVS_DP_ATTR_NAME
]);
1529 parms
.type
= OVS_VPORT_TYPE_INTERNAL
;
1530 parms
.options
= NULL
;
1532 parms
.port_no
= OVSP_LOCAL
;
1533 parms
.upcall_portids
= a
[OVS_DP_ATTR_UPCALL_PID
];
1535 ovs_dp_change(dp
, a
);
1537 /* So far only local changes have been made, now need the lock. */
1540 vport
= new_vport(&parms
);
1541 if (IS_ERR(vport
)) {
1542 err
= PTR_ERR(vport
);
1546 if (err
== -EEXIST
) {
1547 /* An outdated user space instance that does not understand
1548 * the concept of user_features has attempted to create a new
1549 * datapath and is likely to reuse it. Drop all user features.
1551 if (info
->genlhdr
->version
< OVS_DP_VER_FEATURES
)
1552 ovs_dp_reset_user_features(skb
, info
);
1555 goto err_destroy_ports_array
;
1558 err
= ovs_dp_cmd_fill_info(dp
, reply
, info
->snd_portid
,
1559 info
->snd_seq
, 0, OVS_DP_CMD_NEW
);
1562 ovs_net
= net_generic(ovs_dp_get_net(dp
), ovs_net_id
);
1563 list_add_tail_rcu(&dp
->list_node
, &ovs_net
->dps
);
1567 ovs_notify(&dp_datapath_genl_family
, reply
, info
);
1570 err_destroy_ports_array
:
1574 free_percpu(dp
->stats_percpu
);
1576 ovs_flow_tbl_destroy(&dp
->table
);
1578 release_net(ovs_dp_get_net(dp
));
1586 /* Called with ovs_mutex. */
1587 static void __dp_destroy(struct datapath
*dp
)
1591 for (i
= 0; i
< DP_VPORT_HASH_BUCKETS
; i
++) {
1592 struct vport
*vport
;
1593 struct hlist_node
*n
;
1595 hlist_for_each_entry_safe(vport
, n
, &dp
->ports
[i
], dp_hash_node
)
1596 if (vport
->port_no
!= OVSP_LOCAL
)
1597 ovs_dp_detach_port(vport
);
1600 list_del_rcu(&dp
->list_node
);
1602 /* OVSP_LOCAL is datapath internal port. We need to make sure that
1603 * all ports in datapath are destroyed first before freeing datapath.
1605 ovs_dp_detach_port(ovs_vport_ovsl(dp
, OVSP_LOCAL
));
1607 /* RCU destroy the flow table */
1608 call_rcu(&dp
->rcu
, destroy_dp_rcu
);
1611 static int ovs_dp_cmd_del(struct sk_buff
*skb
, struct genl_info
*info
)
1613 struct sk_buff
*reply
;
1614 struct datapath
*dp
;
1617 reply
= ovs_dp_cmd_alloc_info(info
);
1622 dp
= lookup_datapath(sock_net(skb
->sk
), info
->userhdr
, info
->attrs
);
1625 goto err_unlock_free
;
1627 err
= ovs_dp_cmd_fill_info(dp
, reply
, info
->snd_portid
,
1628 info
->snd_seq
, 0, OVS_DP_CMD_DEL
);
1634 ovs_notify(&dp_datapath_genl_family
, reply
, info
);
1644 static int ovs_dp_cmd_set(struct sk_buff
*skb
, struct genl_info
*info
)
1646 struct sk_buff
*reply
;
1647 struct datapath
*dp
;
1650 reply
= ovs_dp_cmd_alloc_info(info
);
1655 dp
= lookup_datapath(sock_net(skb
->sk
), info
->userhdr
, info
->attrs
);
1658 goto err_unlock_free
;
1660 ovs_dp_change(dp
, info
->attrs
);
1662 err
= ovs_dp_cmd_fill_info(dp
, reply
, info
->snd_portid
,
1663 info
->snd_seq
, 0, OVS_DP_CMD_NEW
);
1667 ovs_notify(&dp_datapath_genl_family
, reply
, info
);
1677 static int ovs_dp_cmd_get(struct sk_buff
*skb
, struct genl_info
*info
)
1679 struct sk_buff
*reply
;
1680 struct datapath
*dp
;
1683 reply
= ovs_dp_cmd_alloc_info(info
);
1688 dp
= lookup_datapath(sock_net(skb
->sk
), info
->userhdr
, info
->attrs
);
1691 goto err_unlock_free
;
1693 err
= ovs_dp_cmd_fill_info(dp
, reply
, info
->snd_portid
,
1694 info
->snd_seq
, 0, OVS_DP_CMD_NEW
);
1698 return genlmsg_reply(reply
, info
);
1706 static int ovs_dp_cmd_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1708 struct ovs_net
*ovs_net
= net_generic(sock_net(skb
->sk
), ovs_net_id
);
1709 struct datapath
*dp
;
1710 int skip
= cb
->args
[0];
1714 list_for_each_entry(dp
, &ovs_net
->dps
, list_node
) {
1716 ovs_dp_cmd_fill_info(dp
, skb
, NETLINK_CB(cb
->skb
).portid
,
1717 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
,
1718 OVS_DP_CMD_NEW
) < 0)
1729 static const struct nla_policy datapath_policy
[OVS_DP_ATTR_MAX
+ 1] = {
1730 [OVS_DP_ATTR_NAME
] = { .type
= NLA_NUL_STRING
, .len
= IFNAMSIZ
- 1 },
1731 [OVS_DP_ATTR_UPCALL_PID
] = { .type
= NLA_U32
},
1732 [OVS_DP_ATTR_USER_FEATURES
] = { .type
= NLA_U32
},
1735 static const struct genl_ops dp_datapath_genl_ops
[] = {
1736 { .cmd
= OVS_DP_CMD_NEW
,
1737 .flags
= GENL_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1738 .policy
= datapath_policy
,
1739 .doit
= ovs_dp_cmd_new
1741 { .cmd
= OVS_DP_CMD_DEL
,
1742 .flags
= GENL_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1743 .policy
= datapath_policy
,
1744 .doit
= ovs_dp_cmd_del
1746 { .cmd
= OVS_DP_CMD_GET
,
1747 .flags
= 0, /* OK for unprivileged users. */
1748 .policy
= datapath_policy
,
1749 .doit
= ovs_dp_cmd_get
,
1750 .dumpit
= ovs_dp_cmd_dump
1752 { .cmd
= OVS_DP_CMD_SET
,
1753 .flags
= GENL_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1754 .policy
= datapath_policy
,
1755 .doit
= ovs_dp_cmd_set
,
1759 static struct genl_family dp_datapath_genl_family
= {
1760 .id
= GENL_ID_GENERATE
,
1761 .hdrsize
= sizeof(struct ovs_header
),
1762 .name
= OVS_DATAPATH_FAMILY
,
1763 .version
= OVS_DATAPATH_VERSION
,
1764 .maxattr
= OVS_DP_ATTR_MAX
,
1766 .parallel_ops
= true,
1767 .ops
= dp_datapath_genl_ops
,
1768 .n_ops
= ARRAY_SIZE(dp_datapath_genl_ops
),
1769 .mcgrps
= &ovs_dp_datapath_multicast_group
,
1773 /* Called with ovs_mutex or RCU read lock. */
1774 static int ovs_vport_cmd_fill_info(struct vport
*vport
, struct sk_buff
*skb
,
1775 u32 portid
, u32 seq
, u32 flags
, u8 cmd
)
1777 struct ovs_header
*ovs_header
;
1778 struct ovs_vport_stats vport_stats
;
1781 ovs_header
= genlmsg_put(skb
, portid
, seq
, &dp_vport_genl_family
,
1786 ovs_header
->dp_ifindex
= get_dpifindex(vport
->dp
);
1788 if (nla_put_u32(skb
, OVS_VPORT_ATTR_PORT_NO
, vport
->port_no
) ||
1789 nla_put_u32(skb
, OVS_VPORT_ATTR_TYPE
, vport
->ops
->type
) ||
1790 nla_put_string(skb
, OVS_VPORT_ATTR_NAME
,
1791 vport
->ops
->get_name(vport
)))
1792 goto nla_put_failure
;
1794 ovs_vport_get_stats(vport
, &vport_stats
);
1795 if (nla_put(skb
, OVS_VPORT_ATTR_STATS
, sizeof(struct ovs_vport_stats
),
1797 goto nla_put_failure
;
1799 if (ovs_vport_get_upcall_portids(vport
, skb
))
1800 goto nla_put_failure
;
1802 err
= ovs_vport_get_options(vport
, skb
);
1803 if (err
== -EMSGSIZE
)
1806 genlmsg_end(skb
, ovs_header
);
1812 genlmsg_cancel(skb
, ovs_header
);
1816 static struct sk_buff
*ovs_vport_cmd_alloc_info(void)
1818 return nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
1821 /* Called with ovs_mutex, only via ovs_dp_notify_wq(). */
1822 struct sk_buff
*ovs_vport_cmd_build_info(struct vport
*vport
, u32 portid
,
1825 struct sk_buff
*skb
;
1828 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_ATOMIC
);
1830 return ERR_PTR(-ENOMEM
);
1832 retval
= ovs_vport_cmd_fill_info(vport
, skb
, portid
, seq
, 0, cmd
);
1838 /* Called with ovs_mutex or RCU read lock. */
1839 static struct vport
*lookup_vport(struct net
*net
,
1840 const struct ovs_header
*ovs_header
,
1841 struct nlattr
*a
[OVS_VPORT_ATTR_MAX
+ 1])
1843 struct datapath
*dp
;
1844 struct vport
*vport
;
1846 if (a
[OVS_VPORT_ATTR_NAME
]) {
1847 vport
= ovs_vport_locate(net
, nla_data(a
[OVS_VPORT_ATTR_NAME
]));
1849 return ERR_PTR(-ENODEV
);
1850 if (ovs_header
->dp_ifindex
&&
1851 ovs_header
->dp_ifindex
!= get_dpifindex(vport
->dp
))
1852 return ERR_PTR(-ENODEV
);
1854 } else if (a
[OVS_VPORT_ATTR_PORT_NO
]) {
1855 u32 port_no
= nla_get_u32(a
[OVS_VPORT_ATTR_PORT_NO
]);
1857 if (port_no
>= DP_MAX_PORTS
)
1858 return ERR_PTR(-EFBIG
);
1860 dp
= get_dp(net
, ovs_header
->dp_ifindex
);
1862 return ERR_PTR(-ENODEV
);
1864 vport
= ovs_vport_ovsl_rcu(dp
, port_no
);
1866 return ERR_PTR(-ENODEV
);
1869 return ERR_PTR(-EINVAL
);
1872 static int ovs_vport_cmd_new(struct sk_buff
*skb
, struct genl_info
*info
)
1874 struct nlattr
**a
= info
->attrs
;
1875 struct ovs_header
*ovs_header
= info
->userhdr
;
1876 struct vport_parms parms
;
1877 struct sk_buff
*reply
;
1878 struct vport
*vport
;
1879 struct datapath
*dp
;
1883 if (!a
[OVS_VPORT_ATTR_NAME
] || !a
[OVS_VPORT_ATTR_TYPE
] ||
1884 !a
[OVS_VPORT_ATTR_UPCALL_PID
])
1887 port_no
= a
[OVS_VPORT_ATTR_PORT_NO
]
1888 ? nla_get_u32(a
[OVS_VPORT_ATTR_PORT_NO
]) : 0;
1889 if (port_no
>= DP_MAX_PORTS
)
1892 reply
= ovs_vport_cmd_alloc_info();
1898 dp
= get_dp(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
1901 goto exit_unlock_free
;
1904 vport
= ovs_vport_ovsl(dp
, port_no
);
1907 goto exit_unlock_free
;
1909 for (port_no
= 1; ; port_no
++) {
1910 if (port_no
>= DP_MAX_PORTS
) {
1912 goto exit_unlock_free
;
1914 vport
= ovs_vport_ovsl(dp
, port_no
);
1920 parms
.name
= nla_data(a
[OVS_VPORT_ATTR_NAME
]);
1921 parms
.type
= nla_get_u32(a
[OVS_VPORT_ATTR_TYPE
]);
1922 parms
.options
= a
[OVS_VPORT_ATTR_OPTIONS
];
1924 parms
.port_no
= port_no
;
1925 parms
.upcall_portids
= a
[OVS_VPORT_ATTR_UPCALL_PID
];
1927 vport
= new_vport(&parms
);
1928 err
= PTR_ERR(vport
);
1929 if (IS_ERR(vport
)) {
1932 goto exit_unlock_free
;
1935 err
= ovs_vport_cmd_fill_info(vport
, reply
, info
->snd_portid
,
1936 info
->snd_seq
, 0, OVS_VPORT_CMD_NEW
);
1940 ovs_notify(&dp_vport_genl_family
, reply
, info
);
1949 static int ovs_vport_cmd_set(struct sk_buff
*skb
, struct genl_info
*info
)
1951 struct nlattr
**a
= info
->attrs
;
1952 struct sk_buff
*reply
;
1953 struct vport
*vport
;
1956 reply
= ovs_vport_cmd_alloc_info();
1961 vport
= lookup_vport(sock_net(skb
->sk
), info
->userhdr
, a
);
1962 err
= PTR_ERR(vport
);
1964 goto exit_unlock_free
;
1966 if (a
[OVS_VPORT_ATTR_TYPE
] &&
1967 nla_get_u32(a
[OVS_VPORT_ATTR_TYPE
]) != vport
->ops
->type
) {
1969 goto exit_unlock_free
;
1972 if (a
[OVS_VPORT_ATTR_OPTIONS
]) {
1973 err
= ovs_vport_set_options(vport
, a
[OVS_VPORT_ATTR_OPTIONS
]);
1975 goto exit_unlock_free
;
1979 if (a
[OVS_VPORT_ATTR_UPCALL_PID
]) {
1980 struct nlattr
*ids
= a
[OVS_VPORT_ATTR_UPCALL_PID
];
1982 err
= ovs_vport_set_upcall_portids(vport
, ids
);
1984 goto exit_unlock_free
;
1987 err
= ovs_vport_cmd_fill_info(vport
, reply
, info
->snd_portid
,
1988 info
->snd_seq
, 0, OVS_VPORT_CMD_NEW
);
1992 ovs_notify(&dp_vport_genl_family
, reply
, info
);
2001 static int ovs_vport_cmd_del(struct sk_buff
*skb
, struct genl_info
*info
)
2003 struct nlattr
**a
= info
->attrs
;
2004 struct sk_buff
*reply
;
2005 struct vport
*vport
;
2008 reply
= ovs_vport_cmd_alloc_info();
2013 vport
= lookup_vport(sock_net(skb
->sk
), info
->userhdr
, a
);
2014 err
= PTR_ERR(vport
);
2016 goto exit_unlock_free
;
2018 if (vport
->port_no
== OVSP_LOCAL
) {
2020 goto exit_unlock_free
;
2023 err
= ovs_vport_cmd_fill_info(vport
, reply
, info
->snd_portid
,
2024 info
->snd_seq
, 0, OVS_VPORT_CMD_DEL
);
2026 ovs_dp_detach_port(vport
);
2029 ovs_notify(&dp_vport_genl_family
, reply
, info
);
2038 static int ovs_vport_cmd_get(struct sk_buff
*skb
, struct genl_info
*info
)
2040 struct nlattr
**a
= info
->attrs
;
2041 struct ovs_header
*ovs_header
= info
->userhdr
;
2042 struct sk_buff
*reply
;
2043 struct vport
*vport
;
2046 reply
= ovs_vport_cmd_alloc_info();
2051 vport
= lookup_vport(sock_net(skb
->sk
), ovs_header
, a
);
2052 err
= PTR_ERR(vport
);
2054 goto exit_unlock_free
;
2055 err
= ovs_vport_cmd_fill_info(vport
, reply
, info
->snd_portid
,
2056 info
->snd_seq
, 0, OVS_VPORT_CMD_NEW
);
2060 return genlmsg_reply(reply
, info
);
2068 static int ovs_vport_cmd_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
2070 struct ovs_header
*ovs_header
= genlmsg_data(nlmsg_data(cb
->nlh
));
2071 struct datapath
*dp
;
2072 int bucket
= cb
->args
[0], skip
= cb
->args
[1];
2076 dp
= get_dp_rcu(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
2081 for (i
= bucket
; i
< DP_VPORT_HASH_BUCKETS
; i
++) {
2082 struct vport
*vport
;
2085 hlist_for_each_entry_rcu(vport
, &dp
->ports
[i
], dp_hash_node
) {
2087 ovs_vport_cmd_fill_info(vport
, skb
,
2088 NETLINK_CB(cb
->skb
).portid
,
2091 OVS_VPORT_CMD_NEW
) < 0)
2107 static const struct nla_policy vport_policy
[OVS_VPORT_ATTR_MAX
+ 1] = {
2108 [OVS_VPORT_ATTR_NAME
] = { .type
= NLA_NUL_STRING
, .len
= IFNAMSIZ
- 1 },
2109 [OVS_VPORT_ATTR_STATS
] = { .len
= sizeof(struct ovs_vport_stats
) },
2110 [OVS_VPORT_ATTR_PORT_NO
] = { .type
= NLA_U32
},
2111 [OVS_VPORT_ATTR_TYPE
] = { .type
= NLA_U32
},
2112 [OVS_VPORT_ATTR_UPCALL_PID
] = { .type
= NLA_U32
},
2113 [OVS_VPORT_ATTR_OPTIONS
] = { .type
= NLA_NESTED
},
2116 static const struct genl_ops dp_vport_genl_ops
[] = {
2117 { .cmd
= OVS_VPORT_CMD_NEW
,
2118 .flags
= GENL_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
2119 .policy
= vport_policy
,
2120 .doit
= ovs_vport_cmd_new
2122 { .cmd
= OVS_VPORT_CMD_DEL
,
2123 .flags
= GENL_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
2124 .policy
= vport_policy
,
2125 .doit
= ovs_vport_cmd_del
2127 { .cmd
= OVS_VPORT_CMD_GET
,
2128 .flags
= 0, /* OK for unprivileged users. */
2129 .policy
= vport_policy
,
2130 .doit
= ovs_vport_cmd_get
,
2131 .dumpit
= ovs_vport_cmd_dump
2133 { .cmd
= OVS_VPORT_CMD_SET
,
2134 .flags
= GENL_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
2135 .policy
= vport_policy
,
2136 .doit
= ovs_vport_cmd_set
,
2140 struct genl_family dp_vport_genl_family
= {
2141 .id
= GENL_ID_GENERATE
,
2142 .hdrsize
= sizeof(struct ovs_header
),
2143 .name
= OVS_VPORT_FAMILY
,
2144 .version
= OVS_VPORT_VERSION
,
2145 .maxattr
= OVS_VPORT_ATTR_MAX
,
2147 .parallel_ops
= true,
2148 .ops
= dp_vport_genl_ops
,
2149 .n_ops
= ARRAY_SIZE(dp_vport_genl_ops
),
2150 .mcgrps
= &ovs_dp_vport_multicast_group
,
2154 static struct genl_family
* const dp_genl_families
[] = {
2155 &dp_datapath_genl_family
,
2156 &dp_vport_genl_family
,
2157 &dp_flow_genl_family
,
2158 &dp_packet_genl_family
,
2161 static void dp_unregister_genl(int n_families
)
2165 for (i
= 0; i
< n_families
; i
++)
2166 genl_unregister_family(dp_genl_families
[i
]);
2169 static int dp_register_genl(void)
2174 for (i
= 0; i
< ARRAY_SIZE(dp_genl_families
); i
++) {
2176 err
= genl_register_family(dp_genl_families
[i
]);
2184 dp_unregister_genl(i
);
2188 static int __net_init
ovs_init_net(struct net
*net
)
2190 struct ovs_net
*ovs_net
= net_generic(net
, ovs_net_id
);
2192 INIT_LIST_HEAD(&ovs_net
->dps
);
2193 INIT_WORK(&ovs_net
->dp_notify_work
, ovs_dp_notify_wq
);
2197 static void __net_exit
ovs_exit_net(struct net
*net
)
2199 struct datapath
*dp
, *dp_next
;
2200 struct ovs_net
*ovs_net
= net_generic(net
, ovs_net_id
);
2203 list_for_each_entry_safe(dp
, dp_next
, &ovs_net
->dps
, list_node
)
2207 cancel_work_sync(&ovs_net
->dp_notify_work
);
2210 static struct pernet_operations ovs_net_ops
= {
2211 .init
= ovs_init_net
,
2212 .exit
= ovs_exit_net
,
2214 .size
= sizeof(struct ovs_net
),
2217 static int __init
dp_init(void)
2221 BUILD_BUG_ON(sizeof(struct ovs_skb_cb
) > FIELD_SIZEOF(struct sk_buff
, cb
));
2223 pr_info("Open vSwitch switching datapath\n");
2225 err
= action_fifos_init();
2229 err
= ovs_internal_dev_rtnl_link_register();
2231 goto error_action_fifos_exit
;
2233 err
= ovs_flow_init();
2235 goto error_unreg_rtnl_link
;
2237 err
= ovs_vport_init();
2239 goto error_flow_exit
;
2241 err
= register_pernet_device(&ovs_net_ops
);
2243 goto error_vport_exit
;
2245 err
= register_netdevice_notifier(&ovs_dp_device_notifier
);
2247 goto error_netns_exit
;
2249 err
= ovs_netdev_init();
2251 goto error_unreg_notifier
;
2253 err
= dp_register_genl();
2255 goto error_unreg_netdev
;
2261 error_unreg_notifier
:
2262 unregister_netdevice_notifier(&ovs_dp_device_notifier
);
2264 unregister_pernet_device(&ovs_net_ops
);
2269 error_unreg_rtnl_link
:
2270 ovs_internal_dev_rtnl_link_unregister();
2271 error_action_fifos_exit
:
2272 action_fifos_exit();
2277 static void dp_cleanup(void)
2279 dp_unregister_genl(ARRAY_SIZE(dp_genl_families
));
2281 unregister_netdevice_notifier(&ovs_dp_device_notifier
);
2282 unregister_pernet_device(&ovs_net_ops
);
2286 ovs_internal_dev_rtnl_link_unregister();
2287 action_fifos_exit();
2290 module_init(dp_init
);
2291 module_exit(dp_cleanup
);
2293 MODULE_DESCRIPTION("Open vSwitch switching datapath");
2294 MODULE_LICENSE("GPL");