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
;
63 static struct genl_family dp_packet_genl_family
;
64 static struct genl_family dp_flow_genl_family
;
65 static struct genl_family dp_datapath_genl_family
;
67 static const struct genl_multicast_group ovs_dp_flow_multicast_group
= {
68 .name
= OVS_FLOW_MCGROUP
,
71 static const struct genl_multicast_group ovs_dp_datapath_multicast_group
= {
72 .name
= OVS_DATAPATH_MCGROUP
,
75 static const struct genl_multicast_group ovs_dp_vport_multicast_group
= {
76 .name
= OVS_VPORT_MCGROUP
,
79 /* Check if need to build a reply message.
80 * OVS userspace sets the NLM_F_ECHO flag if it needs the reply. */
81 static bool ovs_must_notify(struct genl_info
*info
,
82 const struct genl_multicast_group
*grp
)
84 return info
->nlhdr
->nlmsg_flags
& NLM_F_ECHO
||
85 netlink_has_listeners(genl_info_net(info
)->genl_sock
, 0);
88 static void ovs_notify(struct genl_family
*family
,
89 struct sk_buff
*skb
, struct genl_info
*info
)
91 genl_notify(family
, skb
, genl_info_net(info
), info
->snd_portid
,
92 0, info
->nlhdr
, GFP_KERNEL
);
98 * All writes e.g. Writes to device state (add/remove datapath, port, set
99 * operations on vports, etc.), Writes to other state (flow table
100 * modifications, set miscellaneous datapath parameters, etc.) are protected
103 * Reads are protected by RCU.
105 * There are a few special cases (mostly stats) that have their own
106 * synchronization but they nest under all of above and don't interact with
109 * The RTNL lock nests inside ovs_mutex.
112 static DEFINE_MUTEX(ovs_mutex
);
116 mutex_lock(&ovs_mutex
);
119 void ovs_unlock(void)
121 mutex_unlock(&ovs_mutex
);
124 #ifdef CONFIG_LOCKDEP
125 int lockdep_ovsl_is_held(void)
128 return lockdep_is_held(&ovs_mutex
);
134 static struct vport
*new_vport(const struct vport_parms
*);
135 static int queue_gso_packets(struct datapath
*dp
, struct sk_buff
*,
136 const struct dp_upcall_info
*);
137 static int queue_userspace_packet(struct datapath
*dp
, struct sk_buff
*,
138 const struct dp_upcall_info
*);
140 /* Must be called with rcu_read_lock or ovs_mutex. */
141 static struct datapath
*get_dp(struct net
*net
, int dp_ifindex
)
143 struct datapath
*dp
= NULL
;
144 struct net_device
*dev
;
147 dev
= dev_get_by_index_rcu(net
, dp_ifindex
);
149 struct vport
*vport
= ovs_internal_dev_get_vport(dev
);
158 /* Must be called with rcu_read_lock or ovs_mutex. */
159 static const char *ovs_dp_name(const struct datapath
*dp
)
161 struct vport
*vport
= ovs_vport_ovsl_rcu(dp
, OVSP_LOCAL
);
162 return vport
->ops
->get_name(vport
);
165 static int get_dpifindex(struct datapath
*dp
)
172 local
= ovs_vport_rcu(dp
, OVSP_LOCAL
);
174 ifindex
= netdev_vport_priv(local
)->dev
->ifindex
;
183 static void destroy_dp_rcu(struct rcu_head
*rcu
)
185 struct datapath
*dp
= container_of(rcu
, struct datapath
, rcu
);
187 free_percpu(dp
->stats_percpu
);
188 release_net(ovs_dp_get_net(dp
));
193 static struct hlist_head
*vport_hash_bucket(const struct datapath
*dp
,
196 return &dp
->ports
[port_no
& (DP_VPORT_HASH_BUCKETS
- 1)];
199 /* Called with ovs_mutex or RCU read lock. */
200 struct vport
*ovs_lookup_vport(const struct datapath
*dp
, u16 port_no
)
203 struct hlist_head
*head
;
205 head
= vport_hash_bucket(dp
, port_no
);
206 hlist_for_each_entry_rcu(vport
, head
, dp_hash_node
) {
207 if (vport
->port_no
== port_no
)
213 /* Called with ovs_mutex. */
214 static struct vport
*new_vport(const struct vport_parms
*parms
)
218 vport
= ovs_vport_add(parms
);
219 if (!IS_ERR(vport
)) {
220 struct datapath
*dp
= parms
->dp
;
221 struct hlist_head
*head
= vport_hash_bucket(dp
, vport
->port_no
);
223 hlist_add_head_rcu(&vport
->dp_hash_node
, head
);
228 void ovs_dp_detach_port(struct vport
*p
)
232 /* First drop references to device. */
233 hlist_del_rcu(&p
->dp_hash_node
);
235 /* Then destroy it. */
239 /* Must be called with rcu_read_lock. */
240 void ovs_dp_process_received_packet(struct vport
*p
, struct sk_buff
*skb
)
242 struct datapath
*dp
= p
->dp
;
243 struct sw_flow
*flow
;
244 struct dp_stats_percpu
*stats
;
245 struct sw_flow_key key
;
250 stats
= this_cpu_ptr(dp
->stats_percpu
);
252 /* Extract flow from 'skb' into 'key'. */
253 error
= ovs_flow_extract(skb
, p
->port_no
, &key
);
254 if (unlikely(error
)) {
260 flow
= ovs_flow_tbl_lookup_stats(&dp
->table
, &key
, &n_mask_hit
);
261 if (unlikely(!flow
)) {
262 struct dp_upcall_info upcall
;
264 upcall
.cmd
= OVS_PACKET_CMD_MISS
;
266 upcall
.userdata
= NULL
;
267 upcall
.portid
= ovs_vport_find_upcall_portid(p
, skb
);
268 error
= ovs_dp_upcall(dp
, skb
, &upcall
);
273 stats_counter
= &stats
->n_missed
;
277 OVS_CB(skb
)->flow
= flow
;
278 OVS_CB(skb
)->pkt_key
= &key
;
280 ovs_flow_stats_update(OVS_CB(skb
)->flow
, key
.tp
.flags
, skb
);
281 ovs_execute_actions(dp
, skb
);
282 stats_counter
= &stats
->n_hit
;
285 /* Update datapath statistics. */
286 u64_stats_update_begin(&stats
->syncp
);
288 stats
->n_mask_hit
+= n_mask_hit
;
289 u64_stats_update_end(&stats
->syncp
);
292 int ovs_dp_upcall(struct datapath
*dp
, struct sk_buff
*skb
,
293 const struct dp_upcall_info
*upcall_info
)
295 struct dp_stats_percpu
*stats
;
298 if (upcall_info
->portid
== 0) {
303 if (!skb_is_gso(skb
))
304 err
= queue_userspace_packet(dp
, skb
, upcall_info
);
306 err
= queue_gso_packets(dp
, skb
, upcall_info
);
313 stats
= this_cpu_ptr(dp
->stats_percpu
);
315 u64_stats_update_begin(&stats
->syncp
);
317 u64_stats_update_end(&stats
->syncp
);
322 static int queue_gso_packets(struct datapath
*dp
, struct sk_buff
*skb
,
323 const struct dp_upcall_info
*upcall_info
)
325 unsigned short gso_type
= skb_shinfo(skb
)->gso_type
;
326 struct dp_upcall_info later_info
;
327 struct sw_flow_key later_key
;
328 struct sk_buff
*segs
, *nskb
;
331 segs
= __skb_gso_segment(skb
, NETIF_F_SG
, false);
333 return PTR_ERR(segs
);
335 /* Queue all of the segments. */
338 err
= queue_userspace_packet(dp
, skb
, upcall_info
);
342 if (skb
== segs
&& gso_type
& SKB_GSO_UDP
) {
343 /* The initial flow key extracted by ovs_flow_extract()
344 * in this case is for a first fragment, so we need to
345 * properly mark later fragments.
347 later_key
= *upcall_info
->key
;
348 later_key
.ip
.frag
= OVS_FRAG_TYPE_LATER
;
350 later_info
= *upcall_info
;
351 later_info
.key
= &later_key
;
352 upcall_info
= &later_info
;
354 } while ((skb
= skb
->next
));
356 /* Free all of the segments. */
364 } while ((skb
= nskb
));
368 static size_t key_attr_size(void)
370 return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */
371 + nla_total_size(0) /* OVS_KEY_ATTR_TUNNEL */
372 + nla_total_size(8) /* OVS_TUNNEL_KEY_ATTR_ID */
373 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_SRC */
374 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_DST */
375 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TOS */
376 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TTL */
377 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT */
378 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_CSUM */
379 + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */
380 + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
381 + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
382 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
383 + nla_total_size(4) /* OVS_KEY_ATTR_8021Q */
384 + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
385 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
386 + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
387 + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
388 + nla_total_size(28); /* OVS_KEY_ATTR_ND */
391 static size_t upcall_msg_size(const struct nlattr
*userdata
,
394 size_t size
= NLMSG_ALIGN(sizeof(struct ovs_header
))
395 + nla_total_size(hdrlen
) /* OVS_PACKET_ATTR_PACKET */
396 + nla_total_size(key_attr_size()); /* OVS_PACKET_ATTR_KEY */
398 /* OVS_PACKET_ATTR_USERDATA */
400 size
+= NLA_ALIGN(userdata
->nla_len
);
405 static int queue_userspace_packet(struct datapath
*dp
, struct sk_buff
*skb
,
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 (vlan_tx_tag_present(skb
)) {
425 nskb
= skb_clone(skb
, GFP_ATOMIC
);
429 nskb
= __vlan_put_tag(nskb
, nskb
->vlan_proto
, vlan_tx_tag_get(nskb
));
437 if (nla_attr_size(skb
->len
) > USHRT_MAX
) {
442 /* Complete checksum if needed */
443 if (skb
->ip_summed
== CHECKSUM_PARTIAL
&&
444 (err
= skb_checksum_help(skb
)))
447 /* Older versions of OVS user space enforce alignment of the last
448 * Netlink attribute to NLA_ALIGNTO which would require extensive
449 * padding logic. Only perform zerocopy if padding is not required.
451 if (dp
->user_features
& OVS_DP_F_UNALIGNED
)
452 hlen
= skb_zerocopy_headlen(skb
);
456 len
= upcall_msg_size(upcall_info
->userdata
, hlen
);
457 user_skb
= genlmsg_new_unicast(len
, &info
, GFP_ATOMIC
);
463 upcall
= genlmsg_put(user_skb
, 0, 0, &dp_packet_genl_family
,
464 0, upcall_info
->cmd
);
465 upcall
->dp_ifindex
= dp_ifindex
;
467 nla
= nla_nest_start(user_skb
, OVS_PACKET_ATTR_KEY
);
468 err
= ovs_nla_put_flow(upcall_info
->key
, upcall_info
->key
, user_skb
);
470 nla_nest_end(user_skb
, nla
);
472 if (upcall_info
->userdata
)
473 __nla_put(user_skb
, OVS_PACKET_ATTR_USERDATA
,
474 nla_len(upcall_info
->userdata
),
475 nla_data(upcall_info
->userdata
));
477 /* Only reserve room for attribute header, packet data is added
478 * in skb_zerocopy() */
479 if (!(nla
= nla_reserve(user_skb
, OVS_PACKET_ATTR_PACKET
, 0))) {
483 nla
->nla_len
= nla_attr_size(skb
->len
);
485 err
= skb_zerocopy(user_skb
, skb
, skb
->len
, hlen
);
489 /* Pad OVS_PACKET_ATTR_PACKET if linear copy was performed */
490 if (!(dp
->user_features
& OVS_DP_F_UNALIGNED
)) {
491 size_t plen
= NLA_ALIGN(user_skb
->len
) - user_skb
->len
;
494 memset(skb_put(user_skb
, plen
), 0, plen
);
497 ((struct nlmsghdr
*) user_skb
->data
)->nlmsg_len
= user_skb
->len
;
499 err
= genlmsg_unicast(ovs_dp_get_net(dp
), user_skb
, upcall_info
->portid
);
509 static int ovs_packet_cmd_execute(struct sk_buff
*skb
, struct genl_info
*info
)
511 struct ovs_header
*ovs_header
= info
->userhdr
;
512 struct nlattr
**a
= info
->attrs
;
513 struct sw_flow_actions
*acts
;
514 struct sk_buff
*packet
;
515 struct sw_flow
*flow
;
522 if (!a
[OVS_PACKET_ATTR_PACKET
] || !a
[OVS_PACKET_ATTR_KEY
] ||
523 !a
[OVS_PACKET_ATTR_ACTIONS
])
526 len
= nla_len(a
[OVS_PACKET_ATTR_PACKET
]);
527 packet
= __dev_alloc_skb(NET_IP_ALIGN
+ len
, GFP_KERNEL
);
531 skb_reserve(packet
, NET_IP_ALIGN
);
533 nla_memcpy(__skb_put(packet
, len
), a
[OVS_PACKET_ATTR_PACKET
], len
);
535 skb_reset_mac_header(packet
);
536 eth
= eth_hdr(packet
);
538 /* Normally, setting the skb 'protocol' field would be handled by a
539 * call to eth_type_trans(), but it assumes there's a sending
540 * device, which we may not have. */
541 if (ntohs(eth
->h_proto
) >= ETH_P_802_3_MIN
)
542 packet
->protocol
= eth
->h_proto
;
544 packet
->protocol
= htons(ETH_P_802_2
);
546 /* Build an sw_flow for sending this packet. */
547 flow
= ovs_flow_alloc();
552 err
= ovs_flow_extract(packet
, -1, &flow
->key
);
556 err
= ovs_nla_get_flow_metadata(flow
, a
[OVS_PACKET_ATTR_KEY
]);
559 acts
= ovs_nla_alloc_flow_actions(nla_len(a
[OVS_PACKET_ATTR_ACTIONS
]));
564 err
= ovs_nla_copy_actions(a
[OVS_PACKET_ATTR_ACTIONS
],
565 &flow
->key
, 0, &acts
);
566 rcu_assign_pointer(flow
->sf_acts
, acts
);
570 OVS_CB(packet
)->flow
= flow
;
571 OVS_CB(packet
)->pkt_key
= &flow
->key
;
572 packet
->priority
= flow
->key
.phy
.priority
;
573 packet
->mark
= flow
->key
.phy
.skb_mark
;
576 dp
= get_dp(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
582 err
= ovs_execute_actions(dp
, packet
);
586 ovs_flow_free(flow
, false);
592 ovs_flow_free(flow
, false);
599 static const struct nla_policy packet_policy
[OVS_PACKET_ATTR_MAX
+ 1] = {
600 [OVS_PACKET_ATTR_PACKET
] = { .len
= ETH_HLEN
},
601 [OVS_PACKET_ATTR_KEY
] = { .type
= NLA_NESTED
},
602 [OVS_PACKET_ATTR_ACTIONS
] = { .type
= NLA_NESTED
},
605 static const struct genl_ops dp_packet_genl_ops
[] = {
606 { .cmd
= OVS_PACKET_CMD_EXECUTE
,
607 .flags
= GENL_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
608 .policy
= packet_policy
,
609 .doit
= ovs_packet_cmd_execute
613 static struct genl_family dp_packet_genl_family
= {
614 .id
= GENL_ID_GENERATE
,
615 .hdrsize
= sizeof(struct ovs_header
),
616 .name
= OVS_PACKET_FAMILY
,
617 .version
= OVS_PACKET_VERSION
,
618 .maxattr
= OVS_PACKET_ATTR_MAX
,
620 .parallel_ops
= true,
621 .ops
= dp_packet_genl_ops
,
622 .n_ops
= ARRAY_SIZE(dp_packet_genl_ops
),
625 static void get_dp_stats(struct datapath
*dp
, struct ovs_dp_stats
*stats
,
626 struct ovs_dp_megaflow_stats
*mega_stats
)
630 memset(mega_stats
, 0, sizeof(*mega_stats
));
632 stats
->n_flows
= ovs_flow_tbl_count(&dp
->table
);
633 mega_stats
->n_masks
= ovs_flow_tbl_num_masks(&dp
->table
);
635 stats
->n_hit
= stats
->n_missed
= stats
->n_lost
= 0;
637 for_each_possible_cpu(i
) {
638 const struct dp_stats_percpu
*percpu_stats
;
639 struct dp_stats_percpu local_stats
;
642 percpu_stats
= per_cpu_ptr(dp
->stats_percpu
, i
);
645 start
= u64_stats_fetch_begin_irq(&percpu_stats
->syncp
);
646 local_stats
= *percpu_stats
;
647 } while (u64_stats_fetch_retry_irq(&percpu_stats
->syncp
, start
));
649 stats
->n_hit
+= local_stats
.n_hit
;
650 stats
->n_missed
+= local_stats
.n_missed
;
651 stats
->n_lost
+= local_stats
.n_lost
;
652 mega_stats
->n_mask_hit
+= local_stats
.n_mask_hit
;
656 static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions
*acts
)
658 return NLMSG_ALIGN(sizeof(struct ovs_header
))
659 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_KEY */
660 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_MASK */
661 + nla_total_size(sizeof(struct ovs_flow_stats
)) /* OVS_FLOW_ATTR_STATS */
662 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
663 + nla_total_size(8) /* OVS_FLOW_ATTR_USED */
664 + nla_total_size(acts
->actions_len
); /* OVS_FLOW_ATTR_ACTIONS */
667 /* Called with ovs_mutex or RCU read lock. */
668 static int ovs_flow_cmd_fill_info(const struct sw_flow
*flow
, int dp_ifindex
,
669 struct sk_buff
*skb
, u32 portid
,
670 u32 seq
, u32 flags
, u8 cmd
)
672 const int skb_orig_len
= skb
->len
;
673 struct nlattr
*start
;
674 struct ovs_flow_stats stats
;
677 struct ovs_header
*ovs_header
;
681 ovs_header
= genlmsg_put(skb
, portid
, seq
, &dp_flow_genl_family
, flags
, cmd
);
685 ovs_header
->dp_ifindex
= dp_ifindex
;
688 nla
= nla_nest_start(skb
, OVS_FLOW_ATTR_KEY
);
690 goto nla_put_failure
;
692 err
= ovs_nla_put_flow(&flow
->unmasked_key
, &flow
->unmasked_key
, skb
);
695 nla_nest_end(skb
, nla
);
697 nla
= nla_nest_start(skb
, OVS_FLOW_ATTR_MASK
);
699 goto nla_put_failure
;
701 err
= ovs_nla_put_flow(&flow
->key
, &flow
->mask
->key
, skb
);
705 nla_nest_end(skb
, nla
);
707 ovs_flow_stats_get(flow
, &stats
, &used
, &tcp_flags
);
710 nla_put_u64(skb
, OVS_FLOW_ATTR_USED
, ovs_flow_used_time(used
)))
711 goto nla_put_failure
;
713 if (stats
.n_packets
&&
714 nla_put(skb
, OVS_FLOW_ATTR_STATS
, sizeof(struct ovs_flow_stats
), &stats
))
715 goto nla_put_failure
;
717 if ((u8
)ntohs(tcp_flags
) &&
718 nla_put_u8(skb
, OVS_FLOW_ATTR_TCP_FLAGS
, (u8
)ntohs(tcp_flags
)))
719 goto nla_put_failure
;
721 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
722 * this is the first flow to be dumped into 'skb'. This is unusual for
723 * Netlink but individual action lists can be longer than
724 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
725 * The userspace caller can always fetch the actions separately if it
726 * really wants them. (Most userspace callers in fact don't care.)
728 * This can only fail for dump operations because the skb is always
729 * properly sized for single flows.
731 start
= nla_nest_start(skb
, OVS_FLOW_ATTR_ACTIONS
);
733 const struct sw_flow_actions
*sf_acts
;
735 sf_acts
= rcu_dereference_ovsl(flow
->sf_acts
);
736 err
= ovs_nla_put_actions(sf_acts
->actions
,
737 sf_acts
->actions_len
, skb
);
740 nla_nest_end(skb
, start
);
745 nla_nest_cancel(skb
, start
);
747 } else if (skb_orig_len
)
748 goto nla_put_failure
;
750 return genlmsg_end(skb
, ovs_header
);
755 genlmsg_cancel(skb
, ovs_header
);
759 /* May not be called with RCU read lock. */
760 static struct sk_buff
*ovs_flow_cmd_alloc_info(const struct sw_flow_actions
*acts
,
761 struct genl_info
*info
,
766 if (!always
&& !ovs_must_notify(info
, &ovs_dp_flow_multicast_group
))
769 skb
= genlmsg_new_unicast(ovs_flow_cmd_msg_size(acts
), info
, GFP_KERNEL
);
771 return ERR_PTR(-ENOMEM
);
776 /* Called with ovs_mutex. */
777 static struct sk_buff
*ovs_flow_cmd_build_info(const struct sw_flow
*flow
,
779 struct genl_info
*info
, u8 cmd
,
785 skb
= ovs_flow_cmd_alloc_info(ovsl_dereference(flow
->sf_acts
), info
,
787 if (IS_ERR_OR_NULL(skb
))
790 retval
= ovs_flow_cmd_fill_info(flow
, dp_ifindex
, skb
,
791 info
->snd_portid
, info
->snd_seq
, 0,
797 static int ovs_flow_cmd_new(struct sk_buff
*skb
, struct genl_info
*info
)
799 struct nlattr
**a
= info
->attrs
;
800 struct ovs_header
*ovs_header
= info
->userhdr
;
801 struct sw_flow
*flow
, *new_flow
;
802 struct sw_flow_mask mask
;
803 struct sk_buff
*reply
;
805 struct sw_flow_actions
*acts
;
806 struct sw_flow_match match
;
809 /* Must have key and actions. */
811 if (!a
[OVS_FLOW_ATTR_KEY
])
813 if (!a
[OVS_FLOW_ATTR_ACTIONS
])
816 /* Most of the time we need to allocate a new flow, do it before
819 new_flow
= ovs_flow_alloc();
820 if (IS_ERR(new_flow
)) {
821 error
= PTR_ERR(new_flow
);
826 ovs_match_init(&match
, &new_flow
->unmasked_key
, &mask
);
827 error
= ovs_nla_get_match(&match
,
828 a
[OVS_FLOW_ATTR_KEY
], a
[OVS_FLOW_ATTR_MASK
]);
832 ovs_flow_mask_key(&new_flow
->key
, &new_flow
->unmasked_key
, &mask
);
834 /* Validate actions. */
835 acts
= ovs_nla_alloc_flow_actions(nla_len(a
[OVS_FLOW_ATTR_ACTIONS
]));
836 error
= PTR_ERR(acts
);
840 error
= ovs_nla_copy_actions(a
[OVS_FLOW_ATTR_ACTIONS
], &new_flow
->key
,
843 OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
847 reply
= ovs_flow_cmd_alloc_info(acts
, info
, false);
849 error
= PTR_ERR(reply
);
854 dp
= get_dp(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
859 /* Check if this is a duplicate flow */
860 flow
= ovs_flow_tbl_lookup(&dp
->table
, &new_flow
->unmasked_key
);
862 rcu_assign_pointer(new_flow
->sf_acts
, acts
);
864 /* Put flow in bucket. */
865 error
= ovs_flow_tbl_insert(&dp
->table
, new_flow
, &mask
);
866 if (unlikely(error
)) {
871 if (unlikely(reply
)) {
872 error
= ovs_flow_cmd_fill_info(new_flow
,
873 ovs_header
->dp_ifindex
,
874 reply
, info
->snd_portid
,
881 struct sw_flow_actions
*old_acts
;
883 /* Bail out if we're not allowed to modify an existing flow.
884 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
885 * because Generic Netlink treats the latter as a dump
886 * request. We also accept NLM_F_EXCL in case that bug ever
889 if (unlikely(info
->nlhdr
->nlmsg_flags
& (NLM_F_CREATE
894 /* The unmasked key has to be the same for flow updates. */
895 if (unlikely(!ovs_flow_cmp_unmasked_key(flow
, &match
))) {
896 flow
= ovs_flow_tbl_lookup_exact(&dp
->table
, &match
);
902 /* Update actions. */
903 old_acts
= ovsl_dereference(flow
->sf_acts
);
904 rcu_assign_pointer(flow
->sf_acts
, acts
);
906 if (unlikely(reply
)) {
907 error
= ovs_flow_cmd_fill_info(flow
,
908 ovs_header
->dp_ifindex
,
909 reply
, info
->snd_portid
,
916 ovs_nla_free_flow_actions(old_acts
);
917 ovs_flow_free(new_flow
, false);
921 ovs_notify(&dp_flow_genl_family
, reply
, info
);
930 ovs_flow_free(new_flow
, false);
935 static int ovs_flow_cmd_set(struct sk_buff
*skb
, struct genl_info
*info
)
937 struct nlattr
**a
= info
->attrs
;
938 struct ovs_header
*ovs_header
= info
->userhdr
;
939 struct sw_flow_key key
, masked_key
;
940 struct sw_flow
*flow
;
941 struct sw_flow_mask mask
;
942 struct sk_buff
*reply
= NULL
;
944 struct sw_flow_actions
*old_acts
= NULL
, *acts
= NULL
;
945 struct sw_flow_match match
;
950 if (!a
[OVS_FLOW_ATTR_KEY
])
953 ovs_match_init(&match
, &key
, &mask
);
954 error
= ovs_nla_get_match(&match
,
955 a
[OVS_FLOW_ATTR_KEY
], a
[OVS_FLOW_ATTR_MASK
]);
959 /* Validate actions. */
960 if (a
[OVS_FLOW_ATTR_ACTIONS
]) {
961 acts
= ovs_nla_alloc_flow_actions(nla_len(a
[OVS_FLOW_ATTR_ACTIONS
]));
962 error
= PTR_ERR(acts
);
966 ovs_flow_mask_key(&masked_key
, &key
, &mask
);
967 error
= ovs_nla_copy_actions(a
[OVS_FLOW_ATTR_ACTIONS
],
968 &masked_key
, 0, &acts
);
970 OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
975 /* Can allocate before locking if have acts. */
977 reply
= ovs_flow_cmd_alloc_info(acts
, info
, false);
979 error
= PTR_ERR(reply
);
985 dp
= get_dp(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
990 /* Check that the flow exists. */
991 flow
= ovs_flow_tbl_lookup_exact(&dp
->table
, &match
);
992 if (unlikely(!flow
)) {
997 /* Update actions, if present. */
999 old_acts
= ovsl_dereference(flow
->sf_acts
);
1000 rcu_assign_pointer(flow
->sf_acts
, acts
);
1002 if (unlikely(reply
)) {
1003 error
= ovs_flow_cmd_fill_info(flow
,
1004 ovs_header
->dp_ifindex
,
1005 reply
, info
->snd_portid
,
1011 /* Could not alloc without acts before locking. */
1012 reply
= ovs_flow_cmd_build_info(flow
, ovs_header
->dp_ifindex
,
1013 info
, OVS_FLOW_CMD_NEW
, false);
1014 if (unlikely(IS_ERR(reply
))) {
1015 error
= PTR_ERR(reply
);
1016 goto err_unlock_ovs
;
1021 if (a
[OVS_FLOW_ATTR_CLEAR
])
1022 ovs_flow_stats_clear(flow
);
1026 ovs_notify(&dp_flow_genl_family
, reply
, info
);
1028 ovs_nla_free_flow_actions(old_acts
);
1041 static int ovs_flow_cmd_get(struct sk_buff
*skb
, struct genl_info
*info
)
1043 struct nlattr
**a
= info
->attrs
;
1044 struct ovs_header
*ovs_header
= info
->userhdr
;
1045 struct sw_flow_key key
;
1046 struct sk_buff
*reply
;
1047 struct sw_flow
*flow
;
1048 struct datapath
*dp
;
1049 struct sw_flow_match match
;
1052 if (!a
[OVS_FLOW_ATTR_KEY
]) {
1053 OVS_NLERR("Flow get message rejected, Key attribute missing.\n");
1057 ovs_match_init(&match
, &key
, NULL
);
1058 err
= ovs_nla_get_match(&match
, a
[OVS_FLOW_ATTR_KEY
], NULL
);
1063 dp
= get_dp(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
1069 flow
= ovs_flow_tbl_lookup_exact(&dp
->table
, &match
);
1075 reply
= ovs_flow_cmd_build_info(flow
, ovs_header
->dp_ifindex
, info
,
1076 OVS_FLOW_CMD_NEW
, true);
1077 if (IS_ERR(reply
)) {
1078 err
= PTR_ERR(reply
);
1083 return genlmsg_reply(reply
, info
);
1089 static int ovs_flow_cmd_del(struct sk_buff
*skb
, struct genl_info
*info
)
1091 struct nlattr
**a
= info
->attrs
;
1092 struct ovs_header
*ovs_header
= info
->userhdr
;
1093 struct sw_flow_key key
;
1094 struct sk_buff
*reply
;
1095 struct sw_flow
*flow
;
1096 struct datapath
*dp
;
1097 struct sw_flow_match match
;
1100 if (likely(a
[OVS_FLOW_ATTR_KEY
])) {
1101 ovs_match_init(&match
, &key
, NULL
);
1102 err
= ovs_nla_get_match(&match
, a
[OVS_FLOW_ATTR_KEY
], NULL
);
1108 dp
= get_dp(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
1109 if (unlikely(!dp
)) {
1114 if (unlikely(!a
[OVS_FLOW_ATTR_KEY
])) {
1115 err
= ovs_flow_tbl_flush(&dp
->table
);
1119 flow
= ovs_flow_tbl_lookup_exact(&dp
->table
, &match
);
1120 if (unlikely(!flow
)) {
1125 ovs_flow_tbl_remove(&dp
->table
, flow
);
1128 reply
= ovs_flow_cmd_alloc_info((const struct sw_flow_actions __force
*) flow
->sf_acts
,
1130 if (likely(reply
)) {
1131 if (likely(!IS_ERR(reply
))) {
1132 rcu_read_lock(); /*To keep RCU checker happy. */
1133 err
= ovs_flow_cmd_fill_info(flow
, ovs_header
->dp_ifindex
,
1134 reply
, info
->snd_portid
,
1140 ovs_notify(&dp_flow_genl_family
, reply
, info
);
1142 netlink_set_err(sock_net(skb
->sk
)->genl_sock
, 0, 0, PTR_ERR(reply
));
1146 ovs_flow_free(flow
, true);
1153 static int ovs_flow_cmd_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1155 struct ovs_header
*ovs_header
= genlmsg_data(nlmsg_data(cb
->nlh
));
1156 struct table_instance
*ti
;
1157 struct datapath
*dp
;
1160 dp
= get_dp(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
1166 ti
= rcu_dereference(dp
->table
.ti
);
1168 struct sw_flow
*flow
;
1171 bucket
= cb
->args
[0];
1173 flow
= ovs_flow_tbl_dump_next(ti
, &bucket
, &obj
);
1177 if (ovs_flow_cmd_fill_info(flow
, ovs_header
->dp_ifindex
, skb
,
1178 NETLINK_CB(cb
->skb
).portid
,
1179 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
,
1180 OVS_FLOW_CMD_NEW
) < 0)
1183 cb
->args
[0] = bucket
;
1190 static const struct nla_policy flow_policy
[OVS_FLOW_ATTR_MAX
+ 1] = {
1191 [OVS_FLOW_ATTR_KEY
] = { .type
= NLA_NESTED
},
1192 [OVS_FLOW_ATTR_ACTIONS
] = { .type
= NLA_NESTED
},
1193 [OVS_FLOW_ATTR_CLEAR
] = { .type
= NLA_FLAG
},
1196 static const struct genl_ops dp_flow_genl_ops
[] = {
1197 { .cmd
= OVS_FLOW_CMD_NEW
,
1198 .flags
= GENL_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1199 .policy
= flow_policy
,
1200 .doit
= ovs_flow_cmd_new
1202 { .cmd
= OVS_FLOW_CMD_DEL
,
1203 .flags
= GENL_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1204 .policy
= flow_policy
,
1205 .doit
= ovs_flow_cmd_del
1207 { .cmd
= OVS_FLOW_CMD_GET
,
1208 .flags
= 0, /* OK for unprivileged users. */
1209 .policy
= flow_policy
,
1210 .doit
= ovs_flow_cmd_get
,
1211 .dumpit
= ovs_flow_cmd_dump
1213 { .cmd
= OVS_FLOW_CMD_SET
,
1214 .flags
= GENL_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1215 .policy
= flow_policy
,
1216 .doit
= ovs_flow_cmd_set
,
1220 static struct genl_family dp_flow_genl_family
= {
1221 .id
= GENL_ID_GENERATE
,
1222 .hdrsize
= sizeof(struct ovs_header
),
1223 .name
= OVS_FLOW_FAMILY
,
1224 .version
= OVS_FLOW_VERSION
,
1225 .maxattr
= OVS_FLOW_ATTR_MAX
,
1227 .parallel_ops
= true,
1228 .ops
= dp_flow_genl_ops
,
1229 .n_ops
= ARRAY_SIZE(dp_flow_genl_ops
),
1230 .mcgrps
= &ovs_dp_flow_multicast_group
,
1234 static size_t ovs_dp_cmd_msg_size(void)
1236 size_t msgsize
= NLMSG_ALIGN(sizeof(struct ovs_header
));
1238 msgsize
+= nla_total_size(IFNAMSIZ
);
1239 msgsize
+= nla_total_size(sizeof(struct ovs_dp_stats
));
1240 msgsize
+= nla_total_size(sizeof(struct ovs_dp_megaflow_stats
));
1241 msgsize
+= nla_total_size(sizeof(u32
)); /* OVS_DP_ATTR_USER_FEATURES */
1246 /* Called with ovs_mutex or RCU read lock. */
1247 static int ovs_dp_cmd_fill_info(struct datapath
*dp
, struct sk_buff
*skb
,
1248 u32 portid
, u32 seq
, u32 flags
, u8 cmd
)
1250 struct ovs_header
*ovs_header
;
1251 struct ovs_dp_stats dp_stats
;
1252 struct ovs_dp_megaflow_stats dp_megaflow_stats
;
1255 ovs_header
= genlmsg_put(skb
, portid
, seq
, &dp_datapath_genl_family
,
1260 ovs_header
->dp_ifindex
= get_dpifindex(dp
);
1262 err
= nla_put_string(skb
, OVS_DP_ATTR_NAME
, ovs_dp_name(dp
));
1264 goto nla_put_failure
;
1266 get_dp_stats(dp
, &dp_stats
, &dp_megaflow_stats
);
1267 if (nla_put(skb
, OVS_DP_ATTR_STATS
, sizeof(struct ovs_dp_stats
),
1269 goto nla_put_failure
;
1271 if (nla_put(skb
, OVS_DP_ATTR_MEGAFLOW_STATS
,
1272 sizeof(struct ovs_dp_megaflow_stats
),
1273 &dp_megaflow_stats
))
1274 goto nla_put_failure
;
1276 if (nla_put_u32(skb
, OVS_DP_ATTR_USER_FEATURES
, dp
->user_features
))
1277 goto nla_put_failure
;
1279 return genlmsg_end(skb
, ovs_header
);
1282 genlmsg_cancel(skb
, ovs_header
);
1287 static struct sk_buff
*ovs_dp_cmd_alloc_info(struct genl_info
*info
)
1289 return genlmsg_new_unicast(ovs_dp_cmd_msg_size(), info
, GFP_KERNEL
);
1292 /* Called with rcu_read_lock or ovs_mutex. */
1293 static struct datapath
*lookup_datapath(struct net
*net
,
1294 struct ovs_header
*ovs_header
,
1295 struct nlattr
*a
[OVS_DP_ATTR_MAX
+ 1])
1297 struct datapath
*dp
;
1299 if (!a
[OVS_DP_ATTR_NAME
])
1300 dp
= get_dp(net
, ovs_header
->dp_ifindex
);
1302 struct vport
*vport
;
1304 vport
= ovs_vport_locate(net
, nla_data(a
[OVS_DP_ATTR_NAME
]));
1305 dp
= vport
&& vport
->port_no
== OVSP_LOCAL
? vport
->dp
: NULL
;
1307 return dp
? dp
: ERR_PTR(-ENODEV
);
1310 static void ovs_dp_reset_user_features(struct sk_buff
*skb
, struct genl_info
*info
)
1312 struct datapath
*dp
;
1314 dp
= lookup_datapath(sock_net(skb
->sk
), info
->userhdr
, info
->attrs
);
1318 WARN(dp
->user_features
, "Dropping previously announced user features\n");
1319 dp
->user_features
= 0;
1322 static void ovs_dp_change(struct datapath
*dp
, struct nlattr
**a
)
1324 if (a
[OVS_DP_ATTR_USER_FEATURES
])
1325 dp
->user_features
= nla_get_u32(a
[OVS_DP_ATTR_USER_FEATURES
]);
1328 static int ovs_dp_cmd_new(struct sk_buff
*skb
, struct genl_info
*info
)
1330 struct nlattr
**a
= info
->attrs
;
1331 struct vport_parms parms
;
1332 struct sk_buff
*reply
;
1333 struct datapath
*dp
;
1334 struct vport
*vport
;
1335 struct ovs_net
*ovs_net
;
1339 if (!a
[OVS_DP_ATTR_NAME
] || !a
[OVS_DP_ATTR_UPCALL_PID
])
1342 reply
= ovs_dp_cmd_alloc_info(info
);
1347 dp
= kzalloc(sizeof(*dp
), GFP_KERNEL
);
1349 goto err_free_reply
;
1351 ovs_dp_set_net(dp
, hold_net(sock_net(skb
->sk
)));
1353 /* Allocate table. */
1354 err
= ovs_flow_tbl_init(&dp
->table
);
1358 dp
->stats_percpu
= netdev_alloc_pcpu_stats(struct dp_stats_percpu
);
1359 if (!dp
->stats_percpu
) {
1361 goto err_destroy_table
;
1364 dp
->ports
= kmalloc(DP_VPORT_HASH_BUCKETS
* sizeof(struct hlist_head
),
1368 goto err_destroy_percpu
;
1371 for (i
= 0; i
< DP_VPORT_HASH_BUCKETS
; i
++)
1372 INIT_HLIST_HEAD(&dp
->ports
[i
]);
1374 /* Set up our datapath device. */
1375 parms
.name
= nla_data(a
[OVS_DP_ATTR_NAME
]);
1376 parms
.type
= OVS_VPORT_TYPE_INTERNAL
;
1377 parms
.options
= NULL
;
1379 parms
.port_no
= OVSP_LOCAL
;
1380 parms
.upcall_portids
= a
[OVS_DP_ATTR_UPCALL_PID
];
1382 ovs_dp_change(dp
, a
);
1384 /* So far only local changes have been made, now need the lock. */
1387 vport
= new_vport(&parms
);
1388 if (IS_ERR(vport
)) {
1389 err
= PTR_ERR(vport
);
1393 if (err
== -EEXIST
) {
1394 /* An outdated user space instance that does not understand
1395 * the concept of user_features has attempted to create a new
1396 * datapath and is likely to reuse it. Drop all user features.
1398 if (info
->genlhdr
->version
< OVS_DP_VER_FEATURES
)
1399 ovs_dp_reset_user_features(skb
, info
);
1402 goto err_destroy_ports_array
;
1405 err
= ovs_dp_cmd_fill_info(dp
, reply
, info
->snd_portid
,
1406 info
->snd_seq
, 0, OVS_DP_CMD_NEW
);
1409 ovs_net
= net_generic(ovs_dp_get_net(dp
), ovs_net_id
);
1410 list_add_tail_rcu(&dp
->list_node
, &ovs_net
->dps
);
1414 ovs_notify(&dp_datapath_genl_family
, reply
, info
);
1417 err_destroy_ports_array
:
1421 free_percpu(dp
->stats_percpu
);
1423 ovs_flow_tbl_destroy(&dp
->table
, false);
1425 release_net(ovs_dp_get_net(dp
));
1433 /* Called with ovs_mutex. */
1434 static void __dp_destroy(struct datapath
*dp
)
1438 for (i
= 0; i
< DP_VPORT_HASH_BUCKETS
; i
++) {
1439 struct vport
*vport
;
1440 struct hlist_node
*n
;
1442 hlist_for_each_entry_safe(vport
, n
, &dp
->ports
[i
], dp_hash_node
)
1443 if (vport
->port_no
!= OVSP_LOCAL
)
1444 ovs_dp_detach_port(vport
);
1447 list_del_rcu(&dp
->list_node
);
1449 /* OVSP_LOCAL is datapath internal port. We need to make sure that
1450 * all ports in datapath are destroyed first before freeing datapath.
1452 ovs_dp_detach_port(ovs_vport_ovsl(dp
, OVSP_LOCAL
));
1454 /* RCU destroy the flow table */
1455 ovs_flow_tbl_destroy(&dp
->table
, true);
1457 call_rcu(&dp
->rcu
, destroy_dp_rcu
);
1460 static int ovs_dp_cmd_del(struct sk_buff
*skb
, struct genl_info
*info
)
1462 struct sk_buff
*reply
;
1463 struct datapath
*dp
;
1466 reply
= ovs_dp_cmd_alloc_info(info
);
1471 dp
= lookup_datapath(sock_net(skb
->sk
), info
->userhdr
, info
->attrs
);
1474 goto err_unlock_free
;
1476 err
= ovs_dp_cmd_fill_info(dp
, reply
, info
->snd_portid
,
1477 info
->snd_seq
, 0, OVS_DP_CMD_DEL
);
1483 ovs_notify(&dp_datapath_genl_family
, reply
, info
);
1493 static int ovs_dp_cmd_set(struct sk_buff
*skb
, struct genl_info
*info
)
1495 struct sk_buff
*reply
;
1496 struct datapath
*dp
;
1499 reply
= ovs_dp_cmd_alloc_info(info
);
1504 dp
= lookup_datapath(sock_net(skb
->sk
), info
->userhdr
, info
->attrs
);
1507 goto err_unlock_free
;
1509 ovs_dp_change(dp
, info
->attrs
);
1511 err
= ovs_dp_cmd_fill_info(dp
, reply
, info
->snd_portid
,
1512 info
->snd_seq
, 0, OVS_DP_CMD_NEW
);
1516 ovs_notify(&dp_datapath_genl_family
, reply
, info
);
1526 static int ovs_dp_cmd_get(struct sk_buff
*skb
, struct genl_info
*info
)
1528 struct sk_buff
*reply
;
1529 struct datapath
*dp
;
1532 reply
= ovs_dp_cmd_alloc_info(info
);
1537 dp
= lookup_datapath(sock_net(skb
->sk
), info
->userhdr
, info
->attrs
);
1540 goto err_unlock_free
;
1542 err
= ovs_dp_cmd_fill_info(dp
, reply
, info
->snd_portid
,
1543 info
->snd_seq
, 0, OVS_DP_CMD_NEW
);
1547 return genlmsg_reply(reply
, info
);
1555 static int ovs_dp_cmd_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1557 struct ovs_net
*ovs_net
= net_generic(sock_net(skb
->sk
), ovs_net_id
);
1558 struct datapath
*dp
;
1559 int skip
= cb
->args
[0];
1563 list_for_each_entry_rcu(dp
, &ovs_net
->dps
, list_node
) {
1565 ovs_dp_cmd_fill_info(dp
, skb
, NETLINK_CB(cb
->skb
).portid
,
1566 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
,
1567 OVS_DP_CMD_NEW
) < 0)
1578 static const struct nla_policy datapath_policy
[OVS_DP_ATTR_MAX
+ 1] = {
1579 [OVS_DP_ATTR_NAME
] = { .type
= NLA_NUL_STRING
, .len
= IFNAMSIZ
- 1 },
1580 [OVS_DP_ATTR_UPCALL_PID
] = { .type
= NLA_U32
},
1581 [OVS_DP_ATTR_USER_FEATURES
] = { .type
= NLA_U32
},
1584 static const struct genl_ops dp_datapath_genl_ops
[] = {
1585 { .cmd
= OVS_DP_CMD_NEW
,
1586 .flags
= GENL_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1587 .policy
= datapath_policy
,
1588 .doit
= ovs_dp_cmd_new
1590 { .cmd
= OVS_DP_CMD_DEL
,
1591 .flags
= GENL_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1592 .policy
= datapath_policy
,
1593 .doit
= ovs_dp_cmd_del
1595 { .cmd
= OVS_DP_CMD_GET
,
1596 .flags
= 0, /* OK for unprivileged users. */
1597 .policy
= datapath_policy
,
1598 .doit
= ovs_dp_cmd_get
,
1599 .dumpit
= ovs_dp_cmd_dump
1601 { .cmd
= OVS_DP_CMD_SET
,
1602 .flags
= GENL_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1603 .policy
= datapath_policy
,
1604 .doit
= ovs_dp_cmd_set
,
1608 static struct genl_family dp_datapath_genl_family
= {
1609 .id
= GENL_ID_GENERATE
,
1610 .hdrsize
= sizeof(struct ovs_header
),
1611 .name
= OVS_DATAPATH_FAMILY
,
1612 .version
= OVS_DATAPATH_VERSION
,
1613 .maxattr
= OVS_DP_ATTR_MAX
,
1615 .parallel_ops
= true,
1616 .ops
= dp_datapath_genl_ops
,
1617 .n_ops
= ARRAY_SIZE(dp_datapath_genl_ops
),
1618 .mcgrps
= &ovs_dp_datapath_multicast_group
,
1622 /* Called with ovs_mutex or RCU read lock. */
1623 static int ovs_vport_cmd_fill_info(struct vport
*vport
, struct sk_buff
*skb
,
1624 u32 portid
, u32 seq
, u32 flags
, u8 cmd
)
1626 struct ovs_header
*ovs_header
;
1627 struct ovs_vport_stats vport_stats
;
1630 ovs_header
= genlmsg_put(skb
, portid
, seq
, &dp_vport_genl_family
,
1635 ovs_header
->dp_ifindex
= get_dpifindex(vport
->dp
);
1637 if (nla_put_u32(skb
, OVS_VPORT_ATTR_PORT_NO
, vport
->port_no
) ||
1638 nla_put_u32(skb
, OVS_VPORT_ATTR_TYPE
, vport
->ops
->type
) ||
1639 nla_put_string(skb
, OVS_VPORT_ATTR_NAME
,
1640 vport
->ops
->get_name(vport
)))
1641 goto nla_put_failure
;
1643 ovs_vport_get_stats(vport
, &vport_stats
);
1644 if (nla_put(skb
, OVS_VPORT_ATTR_STATS
, sizeof(struct ovs_vport_stats
),
1646 goto nla_put_failure
;
1648 if (ovs_vport_get_upcall_portids(vport
, skb
))
1649 goto nla_put_failure
;
1651 err
= ovs_vport_get_options(vport
, skb
);
1652 if (err
== -EMSGSIZE
)
1655 return genlmsg_end(skb
, ovs_header
);
1660 genlmsg_cancel(skb
, ovs_header
);
1664 static struct sk_buff
*ovs_vport_cmd_alloc_info(void)
1666 return nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
1669 /* Called with ovs_mutex, only via ovs_dp_notify_wq(). */
1670 struct sk_buff
*ovs_vport_cmd_build_info(struct vport
*vport
, u32 portid
,
1673 struct sk_buff
*skb
;
1676 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_ATOMIC
);
1678 return ERR_PTR(-ENOMEM
);
1680 retval
= ovs_vport_cmd_fill_info(vport
, skb
, portid
, seq
, 0, cmd
);
1686 /* Called with ovs_mutex or RCU read lock. */
1687 static struct vport
*lookup_vport(struct net
*net
,
1688 struct ovs_header
*ovs_header
,
1689 struct nlattr
*a
[OVS_VPORT_ATTR_MAX
+ 1])
1691 struct datapath
*dp
;
1692 struct vport
*vport
;
1694 if (a
[OVS_VPORT_ATTR_NAME
]) {
1695 vport
= ovs_vport_locate(net
, nla_data(a
[OVS_VPORT_ATTR_NAME
]));
1697 return ERR_PTR(-ENODEV
);
1698 if (ovs_header
->dp_ifindex
&&
1699 ovs_header
->dp_ifindex
!= get_dpifindex(vport
->dp
))
1700 return ERR_PTR(-ENODEV
);
1702 } else if (a
[OVS_VPORT_ATTR_PORT_NO
]) {
1703 u32 port_no
= nla_get_u32(a
[OVS_VPORT_ATTR_PORT_NO
]);
1705 if (port_no
>= DP_MAX_PORTS
)
1706 return ERR_PTR(-EFBIG
);
1708 dp
= get_dp(net
, ovs_header
->dp_ifindex
);
1710 return ERR_PTR(-ENODEV
);
1712 vport
= ovs_vport_ovsl_rcu(dp
, port_no
);
1714 return ERR_PTR(-ENODEV
);
1717 return ERR_PTR(-EINVAL
);
1720 static int ovs_vport_cmd_new(struct sk_buff
*skb
, struct genl_info
*info
)
1722 struct nlattr
**a
= info
->attrs
;
1723 struct ovs_header
*ovs_header
= info
->userhdr
;
1724 struct vport_parms parms
;
1725 struct sk_buff
*reply
;
1726 struct vport
*vport
;
1727 struct datapath
*dp
;
1731 if (!a
[OVS_VPORT_ATTR_NAME
] || !a
[OVS_VPORT_ATTR_TYPE
] ||
1732 !a
[OVS_VPORT_ATTR_UPCALL_PID
])
1735 port_no
= a
[OVS_VPORT_ATTR_PORT_NO
]
1736 ? nla_get_u32(a
[OVS_VPORT_ATTR_PORT_NO
]) : 0;
1737 if (port_no
>= DP_MAX_PORTS
)
1740 reply
= ovs_vport_cmd_alloc_info();
1745 dp
= get_dp(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
1748 goto exit_unlock_free
;
1751 vport
= ovs_vport_ovsl(dp
, port_no
);
1754 goto exit_unlock_free
;
1756 for (port_no
= 1; ; port_no
++) {
1757 if (port_no
>= DP_MAX_PORTS
) {
1759 goto exit_unlock_free
;
1761 vport
= ovs_vport_ovsl(dp
, port_no
);
1767 parms
.name
= nla_data(a
[OVS_VPORT_ATTR_NAME
]);
1768 parms
.type
= nla_get_u32(a
[OVS_VPORT_ATTR_TYPE
]);
1769 parms
.options
= a
[OVS_VPORT_ATTR_OPTIONS
];
1771 parms
.port_no
= port_no
;
1772 parms
.upcall_portids
= a
[OVS_VPORT_ATTR_UPCALL_PID
];
1774 vport
= new_vport(&parms
);
1775 err
= PTR_ERR(vport
);
1777 goto exit_unlock_free
;
1779 err
= ovs_vport_cmd_fill_info(vport
, reply
, info
->snd_portid
,
1780 info
->snd_seq
, 0, OVS_VPORT_CMD_NEW
);
1784 ovs_notify(&dp_vport_genl_family
, reply
, info
);
1793 static int ovs_vport_cmd_set(struct sk_buff
*skb
, struct genl_info
*info
)
1795 struct nlattr
**a
= info
->attrs
;
1796 struct sk_buff
*reply
;
1797 struct vport
*vport
;
1800 reply
= ovs_vport_cmd_alloc_info();
1805 vport
= lookup_vport(sock_net(skb
->sk
), info
->userhdr
, a
);
1806 err
= PTR_ERR(vport
);
1808 goto exit_unlock_free
;
1810 if (a
[OVS_VPORT_ATTR_TYPE
] &&
1811 nla_get_u32(a
[OVS_VPORT_ATTR_TYPE
]) != vport
->ops
->type
) {
1813 goto exit_unlock_free
;
1816 if (a
[OVS_VPORT_ATTR_OPTIONS
]) {
1817 err
= ovs_vport_set_options(vport
, a
[OVS_VPORT_ATTR_OPTIONS
]);
1819 goto exit_unlock_free
;
1823 if (a
[OVS_VPORT_ATTR_UPCALL_PID
]) {
1824 struct nlattr
*ids
= a
[OVS_VPORT_ATTR_UPCALL_PID
];
1826 err
= ovs_vport_set_upcall_portids(vport
, ids
);
1828 goto exit_unlock_free
;
1831 err
= ovs_vport_cmd_fill_info(vport
, reply
, info
->snd_portid
,
1832 info
->snd_seq
, 0, OVS_VPORT_CMD_NEW
);
1836 ovs_notify(&dp_vport_genl_family
, reply
, info
);
1845 static int ovs_vport_cmd_del(struct sk_buff
*skb
, struct genl_info
*info
)
1847 struct nlattr
**a
= info
->attrs
;
1848 struct sk_buff
*reply
;
1849 struct vport
*vport
;
1852 reply
= ovs_vport_cmd_alloc_info();
1857 vport
= lookup_vport(sock_net(skb
->sk
), info
->userhdr
, a
);
1858 err
= PTR_ERR(vport
);
1860 goto exit_unlock_free
;
1862 if (vport
->port_no
== OVSP_LOCAL
) {
1864 goto exit_unlock_free
;
1867 err
= ovs_vport_cmd_fill_info(vport
, reply
, info
->snd_portid
,
1868 info
->snd_seq
, 0, OVS_VPORT_CMD_DEL
);
1870 ovs_dp_detach_port(vport
);
1873 ovs_notify(&dp_vport_genl_family
, reply
, info
);
1882 static int ovs_vport_cmd_get(struct sk_buff
*skb
, struct genl_info
*info
)
1884 struct nlattr
**a
= info
->attrs
;
1885 struct ovs_header
*ovs_header
= info
->userhdr
;
1886 struct sk_buff
*reply
;
1887 struct vport
*vport
;
1890 reply
= ovs_vport_cmd_alloc_info();
1895 vport
= lookup_vport(sock_net(skb
->sk
), ovs_header
, a
);
1896 err
= PTR_ERR(vport
);
1898 goto exit_unlock_free
;
1899 err
= ovs_vport_cmd_fill_info(vport
, reply
, info
->snd_portid
,
1900 info
->snd_seq
, 0, OVS_VPORT_CMD_NEW
);
1904 return genlmsg_reply(reply
, info
);
1912 static int ovs_vport_cmd_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1914 struct ovs_header
*ovs_header
= genlmsg_data(nlmsg_data(cb
->nlh
));
1915 struct datapath
*dp
;
1916 int bucket
= cb
->args
[0], skip
= cb
->args
[1];
1920 dp
= get_dp(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
1925 for (i
= bucket
; i
< DP_VPORT_HASH_BUCKETS
; i
++) {
1926 struct vport
*vport
;
1929 hlist_for_each_entry_rcu(vport
, &dp
->ports
[i
], dp_hash_node
) {
1931 ovs_vport_cmd_fill_info(vport
, skb
,
1932 NETLINK_CB(cb
->skb
).portid
,
1935 OVS_VPORT_CMD_NEW
) < 0)
1951 static const struct nla_policy vport_policy
[OVS_VPORT_ATTR_MAX
+ 1] = {
1952 [OVS_VPORT_ATTR_NAME
] = { .type
= NLA_NUL_STRING
, .len
= IFNAMSIZ
- 1 },
1953 [OVS_VPORT_ATTR_STATS
] = { .len
= sizeof(struct ovs_vport_stats
) },
1954 [OVS_VPORT_ATTR_PORT_NO
] = { .type
= NLA_U32
},
1955 [OVS_VPORT_ATTR_TYPE
] = { .type
= NLA_U32
},
1956 [OVS_VPORT_ATTR_UPCALL_PID
] = { .type
= NLA_U32
},
1957 [OVS_VPORT_ATTR_OPTIONS
] = { .type
= NLA_NESTED
},
1960 static const struct genl_ops dp_vport_genl_ops
[] = {
1961 { .cmd
= OVS_VPORT_CMD_NEW
,
1962 .flags
= GENL_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1963 .policy
= vport_policy
,
1964 .doit
= ovs_vport_cmd_new
1966 { .cmd
= OVS_VPORT_CMD_DEL
,
1967 .flags
= GENL_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1968 .policy
= vport_policy
,
1969 .doit
= ovs_vport_cmd_del
1971 { .cmd
= OVS_VPORT_CMD_GET
,
1972 .flags
= 0, /* OK for unprivileged users. */
1973 .policy
= vport_policy
,
1974 .doit
= ovs_vport_cmd_get
,
1975 .dumpit
= ovs_vport_cmd_dump
1977 { .cmd
= OVS_VPORT_CMD_SET
,
1978 .flags
= GENL_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1979 .policy
= vport_policy
,
1980 .doit
= ovs_vport_cmd_set
,
1984 struct genl_family dp_vport_genl_family
= {
1985 .id
= GENL_ID_GENERATE
,
1986 .hdrsize
= sizeof(struct ovs_header
),
1987 .name
= OVS_VPORT_FAMILY
,
1988 .version
= OVS_VPORT_VERSION
,
1989 .maxattr
= OVS_VPORT_ATTR_MAX
,
1991 .parallel_ops
= true,
1992 .ops
= dp_vport_genl_ops
,
1993 .n_ops
= ARRAY_SIZE(dp_vport_genl_ops
),
1994 .mcgrps
= &ovs_dp_vport_multicast_group
,
1998 static struct genl_family
* const dp_genl_families
[] = {
1999 &dp_datapath_genl_family
,
2000 &dp_vport_genl_family
,
2001 &dp_flow_genl_family
,
2002 &dp_packet_genl_family
,
2005 static void dp_unregister_genl(int n_families
)
2009 for (i
= 0; i
< n_families
; i
++)
2010 genl_unregister_family(dp_genl_families
[i
]);
2013 static int dp_register_genl(void)
2018 for (i
= 0; i
< ARRAY_SIZE(dp_genl_families
); i
++) {
2020 err
= genl_register_family(dp_genl_families
[i
]);
2028 dp_unregister_genl(i
);
2032 static int __net_init
ovs_init_net(struct net
*net
)
2034 struct ovs_net
*ovs_net
= net_generic(net
, ovs_net_id
);
2036 INIT_LIST_HEAD(&ovs_net
->dps
);
2037 INIT_WORK(&ovs_net
->dp_notify_work
, ovs_dp_notify_wq
);
2041 static void __net_exit
ovs_exit_net(struct net
*net
)
2043 struct datapath
*dp
, *dp_next
;
2044 struct ovs_net
*ovs_net
= net_generic(net
, ovs_net_id
);
2047 list_for_each_entry_safe(dp
, dp_next
, &ovs_net
->dps
, list_node
)
2051 cancel_work_sync(&ovs_net
->dp_notify_work
);
2054 static struct pernet_operations ovs_net_ops
= {
2055 .init
= ovs_init_net
,
2056 .exit
= ovs_exit_net
,
2058 .size
= sizeof(struct ovs_net
),
2061 static int __init
dp_init(void)
2065 BUILD_BUG_ON(sizeof(struct ovs_skb_cb
) > FIELD_SIZEOF(struct sk_buff
, cb
));
2067 pr_info("Open vSwitch switching datapath\n");
2069 err
= ovs_internal_dev_rtnl_link_register();
2073 err
= ovs_flow_init();
2075 goto error_unreg_rtnl_link
;
2077 err
= ovs_vport_init();
2079 goto error_flow_exit
;
2081 err
= register_pernet_device(&ovs_net_ops
);
2083 goto error_vport_exit
;
2085 err
= register_netdevice_notifier(&ovs_dp_device_notifier
);
2087 goto error_netns_exit
;
2089 err
= dp_register_genl();
2091 goto error_unreg_notifier
;
2095 error_unreg_notifier
:
2096 unregister_netdevice_notifier(&ovs_dp_device_notifier
);
2098 unregister_pernet_device(&ovs_net_ops
);
2103 error_unreg_rtnl_link
:
2104 ovs_internal_dev_rtnl_link_unregister();
2109 static void dp_cleanup(void)
2111 dp_unregister_genl(ARRAY_SIZE(dp_genl_families
));
2112 unregister_netdevice_notifier(&ovs_dp_device_notifier
);
2113 unregister_pernet_device(&ovs_net_ops
);
2117 ovs_internal_dev_rtnl_link_unregister();
2120 module_init(dp_init
);
2121 module_exit(dp_cleanup
);
2123 MODULE_DESCRIPTION("Open vSwitch switching datapath");
2124 MODULE_LICENSE("GPL");