2 * Copyright (c) 2007-2013 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 <linux/genetlink.h>
51 #include <net/genetlink.h>
52 #include <net/genetlink.h>
53 #include <net/net_namespace.h>
54 #include <net/netns/generic.h>
58 #include "flow_table.h"
59 #include "flow_netlink.h"
60 #include "vport-internal_dev.h"
61 #include "vport-netdev.h"
63 int ovs_net_id __read_mostly
;
65 static struct genl_family dp_packet_genl_family
;
66 static struct genl_family dp_flow_genl_family
;
67 static struct genl_family dp_datapath_genl_family
;
69 static struct genl_multicast_group ovs_dp_flow_multicast_group
= {
70 .name
= OVS_FLOW_MCGROUP
73 static struct genl_multicast_group ovs_dp_datapath_multicast_group
= {
74 .name
= OVS_DATAPATH_MCGROUP
77 struct genl_multicast_group ovs_dp_vport_multicast_group
= {
78 .name
= OVS_VPORT_MCGROUP
81 /* Check if need to build a reply message.
82 * OVS userspace sets the NLM_F_ECHO flag if it needs the reply. */
83 static bool ovs_must_notify(struct genl_info
*info
,
84 const struct genl_multicast_group
*grp
)
86 return info
->nlhdr
->nlmsg_flags
& NLM_F_ECHO
||
87 netlink_has_listeners(genl_info_net(info
)->genl_sock
, 0);
90 static void ovs_notify(struct genl_family
*family
,
91 struct sk_buff
*skb
, struct genl_info
*info
)
93 genl_notify(family
, skb
, genl_info_net(info
), info
->snd_portid
,
94 0, info
->nlhdr
, GFP_KERNEL
);
100 * All writes e.g. Writes to device state (add/remove datapath, port, set
101 * operations on vports, etc.), Writes to other state (flow table
102 * modifications, set miscellaneous datapath parameters, etc.) are protected
105 * Reads are protected by RCU.
107 * There are a few special cases (mostly stats) that have their own
108 * synchronization but they nest under all of above and don't interact with
111 * The RTNL lock nests inside ovs_mutex.
114 static DEFINE_MUTEX(ovs_mutex
);
118 mutex_lock(&ovs_mutex
);
121 void ovs_unlock(void)
123 mutex_unlock(&ovs_mutex
);
126 #ifdef CONFIG_LOCKDEP
127 int lockdep_ovsl_is_held(void)
130 return lockdep_is_held(&ovs_mutex
);
136 static struct vport
*new_vport(const struct vport_parms
*);
137 static int queue_gso_packets(struct datapath
*dp
, struct sk_buff
*,
138 const struct dp_upcall_info
*);
139 static int queue_userspace_packet(struct datapath
*dp
, struct sk_buff
*,
140 const struct dp_upcall_info
*);
142 /* Must be called with rcu_read_lock or ovs_mutex. */
143 static struct datapath
*get_dp(struct net
*net
, int dp_ifindex
)
145 struct datapath
*dp
= NULL
;
146 struct net_device
*dev
;
149 dev
= dev_get_by_index_rcu(net
, dp_ifindex
);
151 struct vport
*vport
= ovs_internal_dev_get_vport(dev
);
160 /* Must be called with rcu_read_lock or ovs_mutex. */
161 static const char *ovs_dp_name(const struct datapath
*dp
)
163 struct vport
*vport
= ovs_vport_ovsl_rcu(dp
, OVSP_LOCAL
);
164 return vport
->ops
->get_name(vport
);
167 static int get_dpifindex(struct datapath
*dp
)
174 local
= ovs_vport_rcu(dp
, OVSP_LOCAL
);
176 ifindex
= netdev_vport_priv(local
)->dev
->ifindex
;
185 static void destroy_dp_rcu(struct rcu_head
*rcu
)
187 struct datapath
*dp
= container_of(rcu
, struct datapath
, rcu
);
189 free_percpu(dp
->stats_percpu
);
190 release_net(ovs_dp_get_net(dp
));
195 static struct hlist_head
*vport_hash_bucket(const struct datapath
*dp
,
198 return &dp
->ports
[port_no
& (DP_VPORT_HASH_BUCKETS
- 1)];
201 /* Called with ovs_mutex or RCU read lock. */
202 struct vport
*ovs_lookup_vport(const struct datapath
*dp
, u16 port_no
)
205 struct hlist_head
*head
;
207 head
= vport_hash_bucket(dp
, port_no
);
208 hlist_for_each_entry_rcu(vport
, head
, dp_hash_node
) {
209 if (vport
->port_no
== port_no
)
215 /* Called with ovs_mutex. */
216 static struct vport
*new_vport(const struct vport_parms
*parms
)
220 vport
= ovs_vport_add(parms
);
221 if (!IS_ERR(vport
)) {
222 struct datapath
*dp
= parms
->dp
;
223 struct hlist_head
*head
= vport_hash_bucket(dp
, vport
->port_no
);
225 hlist_add_head_rcu(&vport
->dp_hash_node
, head
);
230 void ovs_dp_detach_port(struct vport
*p
)
234 /* First drop references to device. */
235 hlist_del_rcu(&p
->dp_hash_node
);
237 /* Then destroy it. */
241 /* Must be called with rcu_read_lock. */
242 void ovs_dp_process_received_packet(struct vport
*p
, struct sk_buff
*skb
)
244 struct datapath
*dp
= p
->dp
;
245 struct sw_flow
*flow
;
246 struct dp_stats_percpu
*stats
;
247 struct sw_flow_key key
;
252 stats
= this_cpu_ptr(dp
->stats_percpu
);
254 /* Extract flow from 'skb' into 'key'. */
255 error
= ovs_flow_extract(skb
, p
->port_no
, &key
);
256 if (unlikely(error
)) {
262 flow
= ovs_flow_tbl_lookup_stats(&dp
->table
, &key
, &n_mask_hit
);
263 if (unlikely(!flow
)) {
264 struct dp_upcall_info upcall
;
266 upcall
.cmd
= OVS_PACKET_CMD_MISS
;
268 upcall
.userdata
= NULL
;
269 upcall
.portid
= p
->upcall_portid
;
270 ovs_dp_upcall(dp
, skb
, &upcall
);
272 stats_counter
= &stats
->n_missed
;
276 OVS_CB(skb
)->flow
= flow
;
277 OVS_CB(skb
)->pkt_key
= &key
;
279 ovs_flow_stats_update(OVS_CB(skb
)->flow
, skb
);
280 ovs_execute_actions(dp
, skb
);
281 stats_counter
= &stats
->n_hit
;
284 /* Update datapath statistics. */
285 u64_stats_update_begin(&stats
->syncp
);
287 stats
->n_mask_hit
+= n_mask_hit
;
288 u64_stats_update_end(&stats
->syncp
);
291 int ovs_dp_upcall(struct datapath
*dp
, struct sk_buff
*skb
,
292 const struct dp_upcall_info
*upcall_info
)
294 struct dp_stats_percpu
*stats
;
297 if (upcall_info
->portid
== 0) {
302 if (!skb_is_gso(skb
))
303 err
= queue_userspace_packet(dp
, skb
, upcall_info
);
305 err
= queue_gso_packets(dp
, skb
, upcall_info
);
312 stats
= this_cpu_ptr(dp
->stats_percpu
);
314 u64_stats_update_begin(&stats
->syncp
);
316 u64_stats_update_end(&stats
->syncp
);
321 static int queue_gso_packets(struct datapath
*dp
, struct sk_buff
*skb
,
322 const struct dp_upcall_info
*upcall_info
)
324 unsigned short gso_type
= skb_shinfo(skb
)->gso_type
;
325 struct dp_upcall_info later_info
;
326 struct sw_flow_key later_key
;
327 struct sk_buff
*segs
, *nskb
;
330 segs
= __skb_gso_segment(skb
, NETIF_F_SG
, false);
332 return PTR_ERR(segs
);
334 /* Queue all of the segments. */
337 err
= queue_userspace_packet(dp
, skb
, upcall_info
);
341 if (skb
== segs
&& gso_type
& SKB_GSO_UDP
) {
342 /* The initial flow key extracted by ovs_flow_extract()
343 * in this case is for a first fragment, so we need to
344 * properly mark later fragments.
346 later_key
= *upcall_info
->key
;
347 later_key
.ip
.frag
= OVS_FRAG_TYPE_LATER
;
349 later_info
= *upcall_info
;
350 later_info
.key
= &later_key
;
351 upcall_info
= &later_info
;
353 } while ((skb
= skb
->next
));
355 /* Free all of the segments. */
363 } while ((skb
= nskb
));
367 static size_t key_attr_size(void)
369 return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */
370 + nla_total_size(0) /* OVS_KEY_ATTR_TUNNEL */
371 + nla_total_size(8) /* OVS_TUNNEL_KEY_ATTR_ID */
372 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_SRC */
373 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_DST */
374 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TOS */
375 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TTL */
376 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT */
377 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_CSUM */
378 + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */
379 + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
380 + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
381 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
382 + nla_total_size(4) /* OVS_KEY_ATTR_8021Q */
383 + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
384 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
385 + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
386 + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
387 + nla_total_size(28); /* OVS_KEY_ATTR_ND */
390 static size_t upcall_msg_size(const struct nlattr
*userdata
,
393 size_t size
= NLMSG_ALIGN(sizeof(struct ovs_header
))
394 + nla_total_size(hdrlen
) /* OVS_PACKET_ATTR_PACKET */
395 + nla_total_size(key_attr_size()); /* OVS_PACKET_ATTR_KEY */
397 /* OVS_PACKET_ATTR_USERDATA */
399 size
+= NLA_ALIGN(userdata
->nla_len
);
404 static int queue_userspace_packet(struct datapath
*dp
, struct sk_buff
*skb
,
405 const struct dp_upcall_info
*upcall_info
)
407 struct ovs_header
*upcall
;
408 struct sk_buff
*nskb
= NULL
;
409 struct sk_buff
*user_skb
; /* to be queued to userspace */
411 struct genl_info info
= {
412 .dst_sk
= ovs_dp_get_net(dp
)->genl_sock
,
413 .snd_portid
= upcall_info
->portid
,
419 dp_ifindex
= get_dpifindex(dp
);
423 if (vlan_tx_tag_present(skb
)) {
424 nskb
= skb_clone(skb
, GFP_ATOMIC
);
428 nskb
= __vlan_put_tag(nskb
, nskb
->vlan_proto
, vlan_tx_tag_get(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
->userdata
, 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 nla
= nla_nest_start(user_skb
, OVS_PACKET_ATTR_KEY
);
467 ovs_nla_put_flow(upcall_info
->key
, upcall_info
->key
, user_skb
);
468 nla_nest_end(user_skb
, nla
);
470 if (upcall_info
->userdata
)
471 __nla_put(user_skb
, OVS_PACKET_ATTR_USERDATA
,
472 nla_len(upcall_info
->userdata
),
473 nla_data(upcall_info
->userdata
));
475 /* Only reserve room for attribute header, packet data is added
476 * in skb_zerocopy() */
477 if (!(nla
= nla_reserve(user_skb
, OVS_PACKET_ATTR_PACKET
, 0))) {
481 nla
->nla_len
= nla_attr_size(skb
->len
);
483 err
= skb_zerocopy(user_skb
, skb
, skb
->len
, hlen
);
487 /* Pad OVS_PACKET_ATTR_PACKET if linear copy was performed */
488 if (!(dp
->user_features
& OVS_DP_F_UNALIGNED
)) {
489 size_t plen
= NLA_ALIGN(user_skb
->len
) - user_skb
->len
;
492 memset(skb_put(user_skb
, plen
), 0, plen
);
495 ((struct nlmsghdr
*) user_skb
->data
)->nlmsg_len
= user_skb
->len
;
497 err
= genlmsg_unicast(ovs_dp_get_net(dp
), user_skb
, upcall_info
->portid
);
505 static int ovs_packet_cmd_execute(struct sk_buff
*skb
, struct genl_info
*info
)
507 struct ovs_header
*ovs_header
= info
->userhdr
;
508 struct nlattr
**a
= info
->attrs
;
509 struct sw_flow_actions
*acts
;
510 struct sk_buff
*packet
;
511 struct sw_flow
*flow
;
518 if (!a
[OVS_PACKET_ATTR_PACKET
] || !a
[OVS_PACKET_ATTR_KEY
] ||
519 !a
[OVS_PACKET_ATTR_ACTIONS
])
522 len
= nla_len(a
[OVS_PACKET_ATTR_PACKET
]);
523 packet
= __dev_alloc_skb(NET_IP_ALIGN
+ len
, GFP_KERNEL
);
527 skb_reserve(packet
, NET_IP_ALIGN
);
529 nla_memcpy(__skb_put(packet
, len
), a
[OVS_PACKET_ATTR_PACKET
], len
);
531 skb_reset_mac_header(packet
);
532 eth
= eth_hdr(packet
);
534 /* Normally, setting the skb 'protocol' field would be handled by a
535 * call to eth_type_trans(), but it assumes there's a sending
536 * device, which we may not have. */
537 if (ntohs(eth
->h_proto
) >= ETH_P_802_3_MIN
)
538 packet
->protocol
= eth
->h_proto
;
540 packet
->protocol
= htons(ETH_P_802_2
);
542 /* Build an sw_flow for sending this packet. */
543 flow
= ovs_flow_alloc();
548 err
= ovs_flow_extract(packet
, -1, &flow
->key
);
552 err
= ovs_nla_get_flow_metadata(flow
, a
[OVS_PACKET_ATTR_KEY
]);
555 acts
= ovs_nla_alloc_flow_actions(nla_len(a
[OVS_PACKET_ATTR_ACTIONS
]));
560 err
= ovs_nla_copy_actions(a
[OVS_PACKET_ATTR_ACTIONS
],
561 &flow
->key
, 0, &acts
);
562 rcu_assign_pointer(flow
->sf_acts
, acts
);
566 OVS_CB(packet
)->flow
= flow
;
567 OVS_CB(packet
)->pkt_key
= &flow
->key
;
568 packet
->priority
= flow
->key
.phy
.priority
;
569 packet
->mark
= flow
->key
.phy
.skb_mark
;
572 dp
= get_dp(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
578 err
= ovs_execute_actions(dp
, packet
);
582 ovs_flow_free(flow
, false);
588 ovs_flow_free(flow
, false);
595 static const struct nla_policy packet_policy
[OVS_PACKET_ATTR_MAX
+ 1] = {
596 [OVS_PACKET_ATTR_PACKET
] = { .len
= ETH_HLEN
},
597 [OVS_PACKET_ATTR_KEY
] = { .type
= NLA_NESTED
},
598 [OVS_PACKET_ATTR_ACTIONS
] = { .type
= NLA_NESTED
},
601 static const struct genl_ops dp_packet_genl_ops
[] = {
602 { .cmd
= OVS_PACKET_CMD_EXECUTE
,
603 .flags
= GENL_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
604 .policy
= packet_policy
,
605 .doit
= ovs_packet_cmd_execute
609 static struct genl_family dp_packet_genl_family
= {
610 .id
= GENL_ID_GENERATE
,
611 .hdrsize
= sizeof(struct ovs_header
),
612 .name
= OVS_PACKET_FAMILY
,
613 .version
= OVS_PACKET_VERSION
,
614 .maxattr
= OVS_PACKET_ATTR_MAX
,
616 .parallel_ops
= true,
617 .ops
= dp_packet_genl_ops
,
618 .n_ops
= ARRAY_SIZE(dp_packet_genl_ops
),
621 static void get_dp_stats(struct datapath
*dp
, struct ovs_dp_stats
*stats
,
622 struct ovs_dp_megaflow_stats
*mega_stats
)
626 memset(mega_stats
, 0, sizeof(*mega_stats
));
628 stats
->n_flows
= ovs_flow_tbl_count(&dp
->table
);
629 mega_stats
->n_masks
= ovs_flow_tbl_num_masks(&dp
->table
);
631 stats
->n_hit
= stats
->n_missed
= stats
->n_lost
= 0;
633 for_each_possible_cpu(i
) {
634 const struct dp_stats_percpu
*percpu_stats
;
635 struct dp_stats_percpu local_stats
;
638 percpu_stats
= per_cpu_ptr(dp
->stats_percpu
, i
);
641 start
= u64_stats_fetch_begin_irq(&percpu_stats
->syncp
);
642 local_stats
= *percpu_stats
;
643 } while (u64_stats_fetch_retry_irq(&percpu_stats
->syncp
, start
));
645 stats
->n_hit
+= local_stats
.n_hit
;
646 stats
->n_missed
+= local_stats
.n_missed
;
647 stats
->n_lost
+= local_stats
.n_lost
;
648 mega_stats
->n_mask_hit
+= local_stats
.n_mask_hit
;
652 static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions
*acts
)
654 return NLMSG_ALIGN(sizeof(struct ovs_header
))
655 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_KEY */
656 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_MASK */
657 + nla_total_size(sizeof(struct ovs_flow_stats
)) /* OVS_FLOW_ATTR_STATS */
658 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
659 + nla_total_size(8) /* OVS_FLOW_ATTR_USED */
660 + nla_total_size(acts
->actions_len
); /* OVS_FLOW_ATTR_ACTIONS */
663 /* Called with ovs_mutex or RCU read lock. */
664 static int ovs_flow_cmd_fill_info(const struct sw_flow
*flow
, int dp_ifindex
,
665 struct sk_buff
*skb
, u32 portid
,
666 u32 seq
, u32 flags
, u8 cmd
)
668 const int skb_orig_len
= skb
->len
;
669 struct nlattr
*start
;
670 struct ovs_flow_stats stats
;
673 struct ovs_header
*ovs_header
;
677 ovs_header
= genlmsg_put(skb
, portid
, seq
, &dp_flow_genl_family
, flags
, cmd
);
681 ovs_header
->dp_ifindex
= dp_ifindex
;
684 nla
= nla_nest_start(skb
, OVS_FLOW_ATTR_KEY
);
686 goto nla_put_failure
;
688 err
= ovs_nla_put_flow(&flow
->unmasked_key
, &flow
->unmasked_key
, skb
);
691 nla_nest_end(skb
, nla
);
693 nla
= nla_nest_start(skb
, OVS_FLOW_ATTR_MASK
);
695 goto nla_put_failure
;
697 err
= ovs_nla_put_flow(&flow
->key
, &flow
->mask
->key
, skb
);
701 nla_nest_end(skb
, nla
);
703 ovs_flow_stats_get(flow
, &stats
, &used
, &tcp_flags
);
706 nla_put_u64(skb
, OVS_FLOW_ATTR_USED
, ovs_flow_used_time(used
)))
707 goto nla_put_failure
;
709 if (stats
.n_packets
&&
710 nla_put(skb
, OVS_FLOW_ATTR_STATS
, sizeof(struct ovs_flow_stats
), &stats
))
711 goto nla_put_failure
;
713 if ((u8
)ntohs(tcp_flags
) &&
714 nla_put_u8(skb
, OVS_FLOW_ATTR_TCP_FLAGS
, (u8
)ntohs(tcp_flags
)))
715 goto nla_put_failure
;
717 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
718 * this is the first flow to be dumped into 'skb'. This is unusual for
719 * Netlink but individual action lists can be longer than
720 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
721 * The userspace caller can always fetch the actions separately if it
722 * really wants them. (Most userspace callers in fact don't care.)
724 * This can only fail for dump operations because the skb is always
725 * properly sized for single flows.
727 start
= nla_nest_start(skb
, OVS_FLOW_ATTR_ACTIONS
);
729 const struct sw_flow_actions
*sf_acts
;
731 sf_acts
= rcu_dereference_ovsl(flow
->sf_acts
);
732 err
= ovs_nla_put_actions(sf_acts
->actions
,
733 sf_acts
->actions_len
, skb
);
736 nla_nest_end(skb
, start
);
741 nla_nest_cancel(skb
, start
);
743 } else if (skb_orig_len
)
744 goto nla_put_failure
;
746 return genlmsg_end(skb
, ovs_header
);
751 genlmsg_cancel(skb
, ovs_header
);
755 /* May not be called with RCU read lock. */
756 static struct sk_buff
*ovs_flow_cmd_alloc_info(const struct sw_flow_actions
*acts
,
757 struct genl_info
*info
,
762 if (!always
&& !ovs_must_notify(info
, &ovs_dp_flow_multicast_group
))
765 skb
= genlmsg_new_unicast(ovs_flow_cmd_msg_size(acts
), info
, GFP_KERNEL
);
767 return ERR_PTR(-ENOMEM
);
772 /* Called with ovs_mutex. */
773 static struct sk_buff
*ovs_flow_cmd_build_info(const struct sw_flow
*flow
,
775 struct genl_info
*info
, u8 cmd
,
781 skb
= ovs_flow_cmd_alloc_info(ovsl_dereference(flow
->sf_acts
), info
,
783 if (!skb
|| IS_ERR(skb
))
786 retval
= ovs_flow_cmd_fill_info(flow
, dp_ifindex
, skb
,
787 info
->snd_portid
, info
->snd_seq
, 0,
793 static int ovs_flow_cmd_new(struct sk_buff
*skb
, struct genl_info
*info
)
795 struct nlattr
**a
= info
->attrs
;
796 struct ovs_header
*ovs_header
= info
->userhdr
;
797 struct sw_flow
*flow
, *new_flow
;
798 struct sw_flow_mask mask
;
799 struct sk_buff
*reply
;
801 struct sw_flow_actions
*acts
;
802 struct sw_flow_match match
;
805 /* Must have key and actions. */
807 if (!a
[OVS_FLOW_ATTR_KEY
])
809 if (!a
[OVS_FLOW_ATTR_ACTIONS
])
812 /* Most of the time we need to allocate a new flow, do it before
815 new_flow
= ovs_flow_alloc();
816 if (IS_ERR(new_flow
)) {
817 error
= PTR_ERR(new_flow
);
822 ovs_match_init(&match
, &new_flow
->unmasked_key
, &mask
);
823 error
= ovs_nla_get_match(&match
,
824 a
[OVS_FLOW_ATTR_KEY
], a
[OVS_FLOW_ATTR_MASK
]);
828 ovs_flow_mask_key(&new_flow
->key
, &new_flow
->unmasked_key
, &mask
);
830 /* Validate actions. */
831 acts
= ovs_nla_alloc_flow_actions(nla_len(a
[OVS_FLOW_ATTR_ACTIONS
]));
832 error
= PTR_ERR(acts
);
836 error
= ovs_nla_copy_actions(a
[OVS_FLOW_ATTR_ACTIONS
], &new_flow
->key
,
839 OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
843 reply
= ovs_flow_cmd_alloc_info(acts
, info
, false);
845 error
= PTR_ERR(reply
);
850 dp
= get_dp(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
855 /* Check if this is a duplicate flow */
856 flow
= ovs_flow_tbl_lookup(&dp
->table
, &new_flow
->unmasked_key
);
858 rcu_assign_pointer(new_flow
->sf_acts
, acts
);
860 /* Put flow in bucket. */
861 error
= ovs_flow_tbl_insert(&dp
->table
, new_flow
, &mask
);
862 if (unlikely(error
)) {
867 if (unlikely(reply
)) {
868 error
= ovs_flow_cmd_fill_info(new_flow
,
869 ovs_header
->dp_ifindex
,
870 reply
, info
->snd_portid
,
877 struct sw_flow_actions
*old_acts
;
879 /* Bail out if we're not allowed to modify an existing flow.
880 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
881 * because Generic Netlink treats the latter as a dump
882 * request. We also accept NLM_F_EXCL in case that bug ever
885 if (unlikely(info
->nlhdr
->nlmsg_flags
& (NLM_F_CREATE
890 /* The unmasked key has to be the same for flow updates. */
891 if (unlikely(!ovs_flow_cmp_unmasked_key(flow
, &match
))) {
895 /* Update actions. */
896 old_acts
= ovsl_dereference(flow
->sf_acts
);
897 rcu_assign_pointer(flow
->sf_acts
, acts
);
899 if (unlikely(reply
)) {
900 error
= ovs_flow_cmd_fill_info(flow
,
901 ovs_header
->dp_ifindex
,
902 reply
, info
->snd_portid
,
909 ovs_nla_free_flow_actions(old_acts
);
910 ovs_flow_free(new_flow
, false);
914 ovs_notify(&dp_flow_genl_family
, reply
, info
);
923 ovs_flow_free(new_flow
, false);
928 static int ovs_flow_cmd_set(struct sk_buff
*skb
, struct genl_info
*info
)
930 struct nlattr
**a
= info
->attrs
;
931 struct ovs_header
*ovs_header
= info
->userhdr
;
932 struct sw_flow_key key
, masked_key
;
933 struct sw_flow
*flow
;
934 struct sw_flow_mask mask
;
935 struct sk_buff
*reply
= NULL
;
937 struct sw_flow_actions
*old_acts
= NULL
, *acts
= NULL
;
938 struct sw_flow_match match
;
943 if (!a
[OVS_FLOW_ATTR_KEY
])
946 ovs_match_init(&match
, &key
, &mask
);
947 error
= ovs_nla_get_match(&match
,
948 a
[OVS_FLOW_ATTR_KEY
], a
[OVS_FLOW_ATTR_MASK
]);
952 /* Validate actions. */
953 if (a
[OVS_FLOW_ATTR_ACTIONS
]) {
954 acts
= ovs_nla_alloc_flow_actions(nla_len(a
[OVS_FLOW_ATTR_ACTIONS
]));
955 error
= PTR_ERR(acts
);
959 ovs_flow_mask_key(&masked_key
, &key
, &mask
);
960 error
= ovs_nla_copy_actions(a
[OVS_FLOW_ATTR_ACTIONS
],
961 &masked_key
, 0, &acts
);
963 OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
968 /* Can allocate before locking if have acts. */
970 reply
= ovs_flow_cmd_alloc_info(acts
, info
, false);
972 error
= PTR_ERR(reply
);
978 dp
= get_dp(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
983 /* Check that the flow exists. */
984 flow
= ovs_flow_tbl_lookup(&dp
->table
, &key
);
985 if (unlikely(!flow
)) {
989 /* The unmasked key has to be the same for flow updates. */
990 if (unlikely(!ovs_flow_cmp_unmasked_key(flow
, &match
))) {
994 /* Update actions, if present. */
996 old_acts
= ovsl_dereference(flow
->sf_acts
);
997 rcu_assign_pointer(flow
->sf_acts
, acts
);
999 if (unlikely(reply
)) {
1000 error
= ovs_flow_cmd_fill_info(flow
,
1001 ovs_header
->dp_ifindex
,
1002 reply
, info
->snd_portid
,
1008 /* Could not alloc without acts before locking. */
1009 reply
= ovs_flow_cmd_build_info(flow
, ovs_header
->dp_ifindex
,
1010 info
, OVS_FLOW_CMD_NEW
, false);
1011 if (unlikely(IS_ERR(reply
))) {
1012 error
= PTR_ERR(reply
);
1013 goto err_unlock_ovs
;
1018 if (a
[OVS_FLOW_ATTR_CLEAR
])
1019 ovs_flow_stats_clear(flow
);
1023 ovs_notify(&dp_flow_genl_family
, reply
, info
);
1025 ovs_nla_free_flow_actions(old_acts
);
1038 static int ovs_flow_cmd_get(struct sk_buff
*skb
, struct genl_info
*info
)
1040 struct nlattr
**a
= info
->attrs
;
1041 struct ovs_header
*ovs_header
= info
->userhdr
;
1042 struct sw_flow_key key
;
1043 struct sk_buff
*reply
;
1044 struct sw_flow
*flow
;
1045 struct datapath
*dp
;
1046 struct sw_flow_match match
;
1049 if (!a
[OVS_FLOW_ATTR_KEY
]) {
1050 OVS_NLERR("Flow get message rejected, Key attribute missing.\n");
1054 ovs_match_init(&match
, &key
, NULL
);
1055 err
= ovs_nla_get_match(&match
, a
[OVS_FLOW_ATTR_KEY
], NULL
);
1060 dp
= get_dp(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
1066 flow
= ovs_flow_tbl_lookup(&dp
->table
, &key
);
1067 if (!flow
|| !ovs_flow_cmp_unmasked_key(flow
, &match
)) {
1072 reply
= ovs_flow_cmd_build_info(flow
, ovs_header
->dp_ifindex
, info
,
1073 OVS_FLOW_CMD_NEW
, true);
1074 if (IS_ERR(reply
)) {
1075 err
= PTR_ERR(reply
);
1080 return genlmsg_reply(reply
, info
);
1086 static int ovs_flow_cmd_del(struct sk_buff
*skb
, struct genl_info
*info
)
1088 struct nlattr
**a
= info
->attrs
;
1089 struct ovs_header
*ovs_header
= info
->userhdr
;
1090 struct sw_flow_key key
;
1091 struct sk_buff
*reply
;
1092 struct sw_flow
*flow
;
1093 struct datapath
*dp
;
1094 struct sw_flow_match match
;
1097 if (likely(a
[OVS_FLOW_ATTR_KEY
])) {
1098 ovs_match_init(&match
, &key
, NULL
);
1099 err
= ovs_nla_get_match(&match
, a
[OVS_FLOW_ATTR_KEY
], NULL
);
1105 dp
= get_dp(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
1106 if (unlikely(!dp
)) {
1111 if (unlikely(!a
[OVS_FLOW_ATTR_KEY
])) {
1112 err
= ovs_flow_tbl_flush(&dp
->table
);
1116 flow
= ovs_flow_tbl_lookup(&dp
->table
, &key
);
1117 if (unlikely(!flow
|| !ovs_flow_cmp_unmasked_key(flow
, &match
))) {
1122 ovs_flow_tbl_remove(&dp
->table
, flow
);
1125 reply
= ovs_flow_cmd_alloc_info((const struct sw_flow_actions __force
*) flow
->sf_acts
,
1127 if (likely(reply
)) {
1128 if (likely(!IS_ERR(reply
))) {
1129 rcu_read_lock(); /*To keep RCU checker happy. */
1130 err
= ovs_flow_cmd_fill_info(flow
, ovs_header
->dp_ifindex
,
1131 reply
, info
->snd_portid
,
1137 ovs_notify(&dp_flow_genl_family
, reply
, info
);
1139 netlink_set_err(sock_net(skb
->sk
)->genl_sock
, 0, 0, PTR_ERR(reply
));
1143 ovs_flow_free(flow
, true);
1150 static int ovs_flow_cmd_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1152 struct ovs_header
*ovs_header
= genlmsg_data(nlmsg_data(cb
->nlh
));
1153 struct table_instance
*ti
;
1154 struct datapath
*dp
;
1157 dp
= get_dp(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
1163 ti
= rcu_dereference(dp
->table
.ti
);
1165 struct sw_flow
*flow
;
1168 bucket
= cb
->args
[0];
1170 flow
= ovs_flow_tbl_dump_next(ti
, &bucket
, &obj
);
1174 if (ovs_flow_cmd_fill_info(flow
, ovs_header
->dp_ifindex
, skb
,
1175 NETLINK_CB(cb
->skb
).portid
,
1176 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
,
1177 OVS_FLOW_CMD_NEW
) < 0)
1180 cb
->args
[0] = bucket
;
1187 static const struct nla_policy flow_policy
[OVS_FLOW_ATTR_MAX
+ 1] = {
1188 [OVS_FLOW_ATTR_KEY
] = { .type
= NLA_NESTED
},
1189 [OVS_FLOW_ATTR_ACTIONS
] = { .type
= NLA_NESTED
},
1190 [OVS_FLOW_ATTR_CLEAR
] = { .type
= NLA_FLAG
},
1193 static struct genl_ops dp_flow_genl_ops
[] = {
1194 { .cmd
= OVS_FLOW_CMD_NEW
,
1195 .flags
= GENL_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1196 .policy
= flow_policy
,
1197 .doit
= ovs_flow_cmd_new
1199 { .cmd
= OVS_FLOW_CMD_DEL
,
1200 .flags
= GENL_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1201 .policy
= flow_policy
,
1202 .doit
= ovs_flow_cmd_del
1204 { .cmd
= OVS_FLOW_CMD_GET
,
1205 .flags
= 0, /* OK for unprivileged users. */
1206 .policy
= flow_policy
,
1207 .doit
= ovs_flow_cmd_get
,
1208 .dumpit
= ovs_flow_cmd_dump
1210 { .cmd
= OVS_FLOW_CMD_SET
,
1211 .flags
= GENL_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1212 .policy
= flow_policy
,
1213 .doit
= ovs_flow_cmd_set
,
1217 static struct genl_family dp_flow_genl_family
= {
1218 .id
= GENL_ID_GENERATE
,
1219 .hdrsize
= sizeof(struct ovs_header
),
1220 .name
= OVS_FLOW_FAMILY
,
1221 .version
= OVS_FLOW_VERSION
,
1222 .maxattr
= OVS_FLOW_ATTR_MAX
,
1224 .parallel_ops
= true,
1225 .ops
= dp_flow_genl_ops
,
1226 .n_ops
= ARRAY_SIZE(dp_flow_genl_ops
),
1227 .mcgrps
= &ovs_dp_flow_multicast_group
,
1231 static size_t ovs_dp_cmd_msg_size(void)
1233 size_t msgsize
= NLMSG_ALIGN(sizeof(struct ovs_header
));
1235 msgsize
+= nla_total_size(IFNAMSIZ
);
1236 msgsize
+= nla_total_size(sizeof(struct ovs_dp_stats
));
1237 msgsize
+= nla_total_size(sizeof(struct ovs_dp_megaflow_stats
));
1238 msgsize
+= nla_total_size(sizeof(u32
)); /* OVS_DP_ATTR_USER_FEATURES */
1243 /* Called with ovs_mutex or RCU read lock. */
1244 static int ovs_dp_cmd_fill_info(struct datapath
*dp
, struct sk_buff
*skb
,
1245 u32 portid
, u32 seq
, u32 flags
, u8 cmd
)
1247 struct ovs_header
*ovs_header
;
1248 struct ovs_dp_stats dp_stats
;
1249 struct ovs_dp_megaflow_stats dp_megaflow_stats
;
1252 ovs_header
= genlmsg_put(skb
, portid
, seq
, &dp_datapath_genl_family
,
1257 ovs_header
->dp_ifindex
= get_dpifindex(dp
);
1259 err
= nla_put_string(skb
, OVS_DP_ATTR_NAME
, ovs_dp_name(dp
));
1261 goto nla_put_failure
;
1263 get_dp_stats(dp
, &dp_stats
, &dp_megaflow_stats
);
1264 if (nla_put(skb
, OVS_DP_ATTR_STATS
, sizeof(struct ovs_dp_stats
),
1266 goto nla_put_failure
;
1268 if (nla_put(skb
, OVS_DP_ATTR_MEGAFLOW_STATS
,
1269 sizeof(struct ovs_dp_megaflow_stats
),
1270 &dp_megaflow_stats
))
1271 goto nla_put_failure
;
1273 if (nla_put_u32(skb
, OVS_DP_ATTR_USER_FEATURES
, dp
->user_features
))
1274 goto nla_put_failure
;
1276 return genlmsg_end(skb
, ovs_header
);
1279 genlmsg_cancel(skb
, ovs_header
);
1284 static struct sk_buff
*ovs_dp_cmd_alloc_info(struct genl_info
*info
)
1286 return genlmsg_new_unicast(ovs_dp_cmd_msg_size(), info
, GFP_KERNEL
);
1289 /* Called with rcu_read_lock or ovs_mutex. */
1290 static struct datapath
*lookup_datapath(struct net
*net
,
1291 struct ovs_header
*ovs_header
,
1292 struct nlattr
*a
[OVS_DP_ATTR_MAX
+ 1])
1294 struct datapath
*dp
;
1296 if (!a
[OVS_DP_ATTR_NAME
])
1297 dp
= get_dp(net
, ovs_header
->dp_ifindex
);
1299 struct vport
*vport
;
1301 vport
= ovs_vport_locate(net
, nla_data(a
[OVS_DP_ATTR_NAME
]));
1302 dp
= vport
&& vport
->port_no
== OVSP_LOCAL
? vport
->dp
: NULL
;
1304 return dp
? dp
: ERR_PTR(-ENODEV
);
1307 static void ovs_dp_reset_user_features(struct sk_buff
*skb
, struct genl_info
*info
)
1309 struct datapath
*dp
;
1311 dp
= lookup_datapath(sock_net(skb
->sk
), info
->userhdr
, info
->attrs
);
1315 WARN(dp
->user_features
, "Dropping previously announced user features\n");
1316 dp
->user_features
= 0;
1319 static void ovs_dp_change(struct datapath
*dp
, struct nlattr
**a
)
1321 if (a
[OVS_DP_ATTR_USER_FEATURES
])
1322 dp
->user_features
= nla_get_u32(a
[OVS_DP_ATTR_USER_FEATURES
]);
1325 static int ovs_dp_cmd_new(struct sk_buff
*skb
, struct genl_info
*info
)
1327 struct nlattr
**a
= info
->attrs
;
1328 struct vport_parms parms
;
1329 struct sk_buff
*reply
;
1330 struct datapath
*dp
;
1331 struct vport
*vport
;
1332 struct ovs_net
*ovs_net
;
1336 if (!a
[OVS_DP_ATTR_NAME
] || !a
[OVS_DP_ATTR_UPCALL_PID
])
1339 reply
= ovs_dp_cmd_alloc_info(info
);
1344 dp
= kzalloc(sizeof(*dp
), GFP_KERNEL
);
1346 goto err_free_reply
;
1348 ovs_dp_set_net(dp
, hold_net(sock_net(skb
->sk
)));
1350 /* Allocate table. */
1351 err
= ovs_flow_tbl_init(&dp
->table
);
1355 dp
->stats_percpu
= netdev_alloc_pcpu_stats(struct dp_stats_percpu
);
1356 if (!dp
->stats_percpu
) {
1358 goto err_destroy_table
;
1361 dp
->ports
= kmalloc(DP_VPORT_HASH_BUCKETS
* sizeof(struct hlist_head
),
1365 goto err_destroy_percpu
;
1368 for (i
= 0; i
< DP_VPORT_HASH_BUCKETS
; i
++)
1369 INIT_HLIST_HEAD(&dp
->ports
[i
]);
1371 /* Set up our datapath device. */
1372 parms
.name
= nla_data(a
[OVS_DP_ATTR_NAME
]);
1373 parms
.type
= OVS_VPORT_TYPE_INTERNAL
;
1374 parms
.options
= NULL
;
1376 parms
.port_no
= OVSP_LOCAL
;
1377 parms
.upcall_portid
= nla_get_u32(a
[OVS_DP_ATTR_UPCALL_PID
]);
1379 ovs_dp_change(dp
, a
);
1381 /* So far only local changes have been made, now need the lock. */
1384 vport
= new_vport(&parms
);
1385 if (IS_ERR(vport
)) {
1386 err
= PTR_ERR(vport
);
1390 if (err
== -EEXIST
) {
1391 /* An outdated user space instance that does not understand
1392 * the concept of user_features has attempted to create a new
1393 * datapath and is likely to reuse it. Drop all user features.
1395 if (info
->genlhdr
->version
< OVS_DP_VER_FEATURES
)
1396 ovs_dp_reset_user_features(skb
, info
);
1399 goto err_destroy_ports_array
;
1402 err
= ovs_dp_cmd_fill_info(dp
, reply
, info
->snd_portid
,
1403 info
->snd_seq
, 0, OVS_DP_CMD_NEW
);
1406 ovs_net
= net_generic(ovs_dp_get_net(dp
), ovs_net_id
);
1407 list_add_tail_rcu(&dp
->list_node
, &ovs_net
->dps
);
1411 ovs_notify(&dp_datapath_genl_family
, reply
, info
);
1414 err_destroy_ports_array
:
1418 free_percpu(dp
->stats_percpu
);
1420 ovs_flow_tbl_destroy(&dp
->table
, false);
1422 release_net(ovs_dp_get_net(dp
));
1430 /* Called with ovs_mutex. */
1431 static void __dp_destroy(struct datapath
*dp
)
1435 for (i
= 0; i
< DP_VPORT_HASH_BUCKETS
; i
++) {
1436 struct vport
*vport
;
1437 struct hlist_node
*n
;
1439 hlist_for_each_entry_safe(vport
, n
, &dp
->ports
[i
], dp_hash_node
)
1440 if (vport
->port_no
!= OVSP_LOCAL
)
1441 ovs_dp_detach_port(vport
);
1444 list_del_rcu(&dp
->list_node
);
1446 /* OVSP_LOCAL is datapath internal port. We need to make sure that
1447 * all ports in datapath are destroyed first before freeing datapath.
1449 ovs_dp_detach_port(ovs_vport_ovsl(dp
, OVSP_LOCAL
));
1451 /* RCU destroy the flow table */
1452 ovs_flow_tbl_destroy(&dp
->table
, true);
1454 call_rcu(&dp
->rcu
, destroy_dp_rcu
);
1457 static int ovs_dp_cmd_del(struct sk_buff
*skb
, struct genl_info
*info
)
1459 struct sk_buff
*reply
;
1460 struct datapath
*dp
;
1463 reply
= ovs_dp_cmd_alloc_info(info
);
1468 dp
= lookup_datapath(sock_net(skb
->sk
), info
->userhdr
, info
->attrs
);
1471 goto err_unlock_free
;
1473 err
= ovs_dp_cmd_fill_info(dp
, reply
, info
->snd_portid
,
1474 info
->snd_seq
, 0, OVS_DP_CMD_DEL
);
1480 ovs_notify(&dp_datapath_genl_family
, reply
, info
);
1490 static int ovs_dp_cmd_set(struct sk_buff
*skb
, struct genl_info
*info
)
1492 struct sk_buff
*reply
;
1493 struct datapath
*dp
;
1496 reply
= ovs_dp_cmd_alloc_info(info
);
1501 dp
= lookup_datapath(sock_net(skb
->sk
), info
->userhdr
, info
->attrs
);
1504 goto err_unlock_free
;
1506 ovs_dp_change(dp
, info
->attrs
);
1508 err
= ovs_dp_cmd_fill_info(dp
, reply
, info
->snd_portid
,
1509 info
->snd_seq
, 0, OVS_DP_CMD_NEW
);
1513 ovs_notify(&dp_datapath_genl_family
, reply
, info
);
1523 static int ovs_dp_cmd_get(struct sk_buff
*skb
, struct genl_info
*info
)
1525 struct sk_buff
*reply
;
1526 struct datapath
*dp
;
1529 reply
= ovs_dp_cmd_alloc_info(info
);
1534 dp
= lookup_datapath(sock_net(skb
->sk
), info
->userhdr
, info
->attrs
);
1537 goto err_unlock_free
;
1539 err
= ovs_dp_cmd_fill_info(dp
, reply
, info
->snd_portid
,
1540 info
->snd_seq
, 0, OVS_DP_CMD_NEW
);
1544 return genlmsg_reply(reply
, info
);
1552 static int ovs_dp_cmd_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1554 struct ovs_net
*ovs_net
= net_generic(sock_net(skb
->sk
), ovs_net_id
);
1555 struct datapath
*dp
;
1556 int skip
= cb
->args
[0];
1560 list_for_each_entry_rcu(dp
, &ovs_net
->dps
, list_node
) {
1562 ovs_dp_cmd_fill_info(dp
, skb
, NETLINK_CB(cb
->skb
).portid
,
1563 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
,
1564 OVS_DP_CMD_NEW
) < 0)
1575 static const struct nla_policy datapath_policy
[OVS_DP_ATTR_MAX
+ 1] = {
1576 [OVS_DP_ATTR_NAME
] = { .type
= NLA_NUL_STRING
, .len
= IFNAMSIZ
- 1 },
1577 [OVS_DP_ATTR_UPCALL_PID
] = { .type
= NLA_U32
},
1578 [OVS_DP_ATTR_USER_FEATURES
] = { .type
= NLA_U32
},
1581 static struct genl_ops dp_datapath_genl_ops
[] = {
1582 { .cmd
= OVS_DP_CMD_NEW
,
1583 .flags
= GENL_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1584 .policy
= datapath_policy
,
1585 .doit
= ovs_dp_cmd_new
1587 { .cmd
= OVS_DP_CMD_DEL
,
1588 .flags
= GENL_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1589 .policy
= datapath_policy
,
1590 .doit
= ovs_dp_cmd_del
1592 { .cmd
= OVS_DP_CMD_GET
,
1593 .flags
= 0, /* OK for unprivileged users. */
1594 .policy
= datapath_policy
,
1595 .doit
= ovs_dp_cmd_get
,
1596 .dumpit
= ovs_dp_cmd_dump
1598 { .cmd
= OVS_DP_CMD_SET
,
1599 .flags
= GENL_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1600 .policy
= datapath_policy
,
1601 .doit
= ovs_dp_cmd_set
,
1605 static struct genl_family dp_datapath_genl_family
= {
1606 .id
= GENL_ID_GENERATE
,
1607 .hdrsize
= sizeof(struct ovs_header
),
1608 .name
= OVS_DATAPATH_FAMILY
,
1609 .version
= OVS_DATAPATH_VERSION
,
1610 .maxattr
= OVS_DP_ATTR_MAX
,
1612 .parallel_ops
= true,
1613 .ops
= dp_datapath_genl_ops
,
1614 .n_ops
= ARRAY_SIZE(dp_datapath_genl_ops
),
1615 .mcgrps
= &ovs_dp_datapath_multicast_group
,
1619 /* Called with ovs_mutex or RCU read lock. */
1620 static int ovs_vport_cmd_fill_info(struct vport
*vport
, struct sk_buff
*skb
,
1621 u32 portid
, u32 seq
, u32 flags
, u8 cmd
)
1623 struct ovs_header
*ovs_header
;
1624 struct ovs_vport_stats vport_stats
;
1627 ovs_header
= genlmsg_put(skb
, portid
, seq
, &dp_vport_genl_family
,
1632 ovs_header
->dp_ifindex
= get_dpifindex(vport
->dp
);
1634 if (nla_put_u32(skb
, OVS_VPORT_ATTR_PORT_NO
, vport
->port_no
) ||
1635 nla_put_u32(skb
, OVS_VPORT_ATTR_TYPE
, vport
->ops
->type
) ||
1636 nla_put_string(skb
, OVS_VPORT_ATTR_NAME
, vport
->ops
->get_name(vport
)) ||
1637 nla_put_u32(skb
, OVS_VPORT_ATTR_UPCALL_PID
, vport
->upcall_portid
))
1638 goto nla_put_failure
;
1640 ovs_vport_get_stats(vport
, &vport_stats
);
1641 if (nla_put(skb
, OVS_VPORT_ATTR_STATS
, sizeof(struct ovs_vport_stats
),
1643 goto nla_put_failure
;
1645 err
= ovs_vport_get_options(vport
, skb
);
1646 if (err
== -EMSGSIZE
)
1649 return genlmsg_end(skb
, ovs_header
);
1654 genlmsg_cancel(skb
, ovs_header
);
1658 static struct sk_buff
*ovs_vport_cmd_alloc_info(void)
1660 return nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
1663 /* Called with ovs_mutex, only via ovs_dp_notify_wq(). */
1664 struct sk_buff
*ovs_vport_cmd_build_info(struct vport
*vport
, u32 portid
,
1667 struct sk_buff
*skb
;
1670 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_ATOMIC
);
1672 return ERR_PTR(-ENOMEM
);
1674 retval
= ovs_vport_cmd_fill_info(vport
, skb
, portid
, seq
, 0, cmd
);
1680 /* Called with ovs_mutex or RCU read lock. */
1681 static struct vport
*lookup_vport(struct net
*net
,
1682 struct ovs_header
*ovs_header
,
1683 struct nlattr
*a
[OVS_VPORT_ATTR_MAX
+ 1])
1685 struct datapath
*dp
;
1686 struct vport
*vport
;
1688 if (a
[OVS_VPORT_ATTR_NAME
]) {
1689 vport
= ovs_vport_locate(net
, nla_data(a
[OVS_VPORT_ATTR_NAME
]));
1691 return ERR_PTR(-ENODEV
);
1692 if (ovs_header
->dp_ifindex
&&
1693 ovs_header
->dp_ifindex
!= get_dpifindex(vport
->dp
))
1694 return ERR_PTR(-ENODEV
);
1696 } else if (a
[OVS_VPORT_ATTR_PORT_NO
]) {
1697 u32 port_no
= nla_get_u32(a
[OVS_VPORT_ATTR_PORT_NO
]);
1699 if (port_no
>= DP_MAX_PORTS
)
1700 return ERR_PTR(-EFBIG
);
1702 dp
= get_dp(net
, ovs_header
->dp_ifindex
);
1704 return ERR_PTR(-ENODEV
);
1706 vport
= ovs_vport_ovsl_rcu(dp
, port_no
);
1708 return ERR_PTR(-ENODEV
);
1711 return ERR_PTR(-EINVAL
);
1714 static int ovs_vport_cmd_new(struct sk_buff
*skb
, struct genl_info
*info
)
1716 struct nlattr
**a
= info
->attrs
;
1717 struct ovs_header
*ovs_header
= info
->userhdr
;
1718 struct vport_parms parms
;
1719 struct sk_buff
*reply
;
1720 struct vport
*vport
;
1721 struct datapath
*dp
;
1725 if (!a
[OVS_VPORT_ATTR_NAME
] || !a
[OVS_VPORT_ATTR_TYPE
] ||
1726 !a
[OVS_VPORT_ATTR_UPCALL_PID
])
1729 port_no
= a
[OVS_VPORT_ATTR_PORT_NO
]
1730 ? nla_get_u32(a
[OVS_VPORT_ATTR_PORT_NO
]) : 0;
1731 if (port_no
>= DP_MAX_PORTS
)
1734 reply
= ovs_vport_cmd_alloc_info();
1739 dp
= get_dp(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
1742 goto exit_unlock_free
;
1745 vport
= ovs_vport_ovsl(dp
, port_no
);
1748 goto exit_unlock_free
;
1750 for (port_no
= 1; ; port_no
++) {
1751 if (port_no
>= DP_MAX_PORTS
) {
1753 goto exit_unlock_free
;
1755 vport
= ovs_vport_ovsl(dp
, port_no
);
1761 parms
.name
= nla_data(a
[OVS_VPORT_ATTR_NAME
]);
1762 parms
.type
= nla_get_u32(a
[OVS_VPORT_ATTR_TYPE
]);
1763 parms
.options
= a
[OVS_VPORT_ATTR_OPTIONS
];
1765 parms
.port_no
= port_no
;
1766 parms
.upcall_portid
= nla_get_u32(a
[OVS_VPORT_ATTR_UPCALL_PID
]);
1768 vport
= new_vport(&parms
);
1769 err
= PTR_ERR(vport
);
1771 goto exit_unlock_free
;
1773 err
= ovs_vport_cmd_fill_info(vport
, reply
, info
->snd_portid
,
1774 info
->snd_seq
, 0, OVS_VPORT_CMD_NEW
);
1778 ovs_notify(&dp_vport_genl_family
, reply
, info
);
1787 static int ovs_vport_cmd_set(struct sk_buff
*skb
, struct genl_info
*info
)
1789 struct nlattr
**a
= info
->attrs
;
1790 struct sk_buff
*reply
;
1791 struct vport
*vport
;
1794 reply
= ovs_vport_cmd_alloc_info();
1799 vport
= lookup_vport(sock_net(skb
->sk
), info
->userhdr
, a
);
1800 err
= PTR_ERR(vport
);
1802 goto exit_unlock_free
;
1804 if (a
[OVS_VPORT_ATTR_TYPE
] &&
1805 nla_get_u32(a
[OVS_VPORT_ATTR_TYPE
]) != vport
->ops
->type
) {
1807 goto exit_unlock_free
;
1810 if (a
[OVS_VPORT_ATTR_OPTIONS
]) {
1811 err
= ovs_vport_set_options(vport
, a
[OVS_VPORT_ATTR_OPTIONS
]);
1813 goto exit_unlock_free
;
1816 if (a
[OVS_VPORT_ATTR_UPCALL_PID
])
1817 vport
->upcall_portid
= nla_get_u32(a
[OVS_VPORT_ATTR_UPCALL_PID
]);
1819 err
= ovs_vport_cmd_fill_info(vport
, reply
, info
->snd_portid
,
1820 info
->snd_seq
, 0, OVS_VPORT_CMD_NEW
);
1824 ovs_notify(&dp_vport_genl_family
, reply
, info
);
1833 static int ovs_vport_cmd_del(struct sk_buff
*skb
, struct genl_info
*info
)
1835 struct nlattr
**a
= info
->attrs
;
1836 struct sk_buff
*reply
;
1837 struct vport
*vport
;
1840 reply
= ovs_vport_cmd_alloc_info();
1845 vport
= lookup_vport(sock_net(skb
->sk
), info
->userhdr
, a
);
1846 err
= PTR_ERR(vport
);
1848 goto exit_unlock_free
;
1850 if (vport
->port_no
== OVSP_LOCAL
) {
1852 goto exit_unlock_free
;
1855 err
= ovs_vport_cmd_fill_info(vport
, reply
, info
->snd_portid
,
1856 info
->snd_seq
, 0, OVS_VPORT_CMD_DEL
);
1858 ovs_dp_detach_port(vport
);
1861 ovs_notify(&dp_vport_genl_family
, reply
, info
);
1870 static int ovs_vport_cmd_get(struct sk_buff
*skb
, struct genl_info
*info
)
1872 struct nlattr
**a
= info
->attrs
;
1873 struct ovs_header
*ovs_header
= info
->userhdr
;
1874 struct sk_buff
*reply
;
1875 struct vport
*vport
;
1878 reply
= ovs_vport_cmd_alloc_info();
1883 vport
= lookup_vport(sock_net(skb
->sk
), ovs_header
, a
);
1884 err
= PTR_ERR(vport
);
1886 goto exit_unlock_free
;
1887 err
= ovs_vport_cmd_fill_info(vport
, reply
, info
->snd_portid
,
1888 info
->snd_seq
, 0, OVS_VPORT_CMD_NEW
);
1892 return genlmsg_reply(reply
, info
);
1900 static int ovs_vport_cmd_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1902 struct ovs_header
*ovs_header
= genlmsg_data(nlmsg_data(cb
->nlh
));
1903 struct datapath
*dp
;
1904 int bucket
= cb
->args
[0], skip
= cb
->args
[1];
1908 dp
= get_dp(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
1913 for (i
= bucket
; i
< DP_VPORT_HASH_BUCKETS
; i
++) {
1914 struct vport
*vport
;
1917 hlist_for_each_entry_rcu(vport
, &dp
->ports
[i
], dp_hash_node
) {
1919 ovs_vport_cmd_fill_info(vport
, skb
,
1920 NETLINK_CB(cb
->skb
).portid
,
1923 OVS_VPORT_CMD_NEW
) < 0)
1939 static const struct nla_policy vport_policy
[OVS_VPORT_ATTR_MAX
+ 1] = {
1940 [OVS_VPORT_ATTR_NAME
] = { .type
= NLA_NUL_STRING
, .len
= IFNAMSIZ
- 1 },
1941 [OVS_VPORT_ATTR_STATS
] = { .len
= sizeof(struct ovs_vport_stats
) },
1942 [OVS_VPORT_ATTR_PORT_NO
] = { .type
= NLA_U32
},
1943 [OVS_VPORT_ATTR_TYPE
] = { .type
= NLA_U32
},
1944 [OVS_VPORT_ATTR_UPCALL_PID
] = { .type
= NLA_U32
},
1945 [OVS_VPORT_ATTR_OPTIONS
] = { .type
= NLA_NESTED
},
1948 static struct genl_ops dp_vport_genl_ops
[] = {
1949 { .cmd
= OVS_VPORT_CMD_NEW
,
1950 .flags
= GENL_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1951 .policy
= vport_policy
,
1952 .doit
= ovs_vport_cmd_new
1954 { .cmd
= OVS_VPORT_CMD_DEL
,
1955 .flags
= GENL_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1956 .policy
= vport_policy
,
1957 .doit
= ovs_vport_cmd_del
1959 { .cmd
= OVS_VPORT_CMD_GET
,
1960 .flags
= 0, /* OK for unprivileged users. */
1961 .policy
= vport_policy
,
1962 .doit
= ovs_vport_cmd_get
,
1963 .dumpit
= ovs_vport_cmd_dump
1965 { .cmd
= OVS_VPORT_CMD_SET
,
1966 .flags
= GENL_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1967 .policy
= vport_policy
,
1968 .doit
= ovs_vport_cmd_set
,
1972 struct genl_family dp_vport_genl_family
= {
1973 .id
= GENL_ID_GENERATE
,
1974 .hdrsize
= sizeof(struct ovs_header
),
1975 .name
= OVS_VPORT_FAMILY
,
1976 .version
= OVS_VPORT_VERSION
,
1977 .maxattr
= OVS_VPORT_ATTR_MAX
,
1979 .parallel_ops
= true,
1980 .ops
= dp_vport_genl_ops
,
1981 .n_ops
= ARRAY_SIZE(dp_vport_genl_ops
),
1982 .mcgrps
= &ovs_dp_vport_multicast_group
,
1986 static struct genl_family
* const dp_genl_families
[] = {
1987 &dp_datapath_genl_family
,
1988 &dp_vport_genl_family
,
1989 &dp_flow_genl_family
,
1990 &dp_packet_genl_family
,
1993 static void dp_unregister_genl(int n_families
)
1997 for (i
= 0; i
< n_families
; i
++)
1998 genl_unregister_family(dp_genl_families
[i
]);
2001 static int dp_register_genl(void)
2006 for (i
= 0; i
< ARRAY_SIZE(dp_genl_families
); i
++) {
2008 err
= genl_register_family(dp_genl_families
[i
]);
2016 dp_unregister_genl(i
);
2020 static int __net_init
ovs_init_net(struct net
*net
)
2022 struct ovs_net
*ovs_net
= net_generic(net
, ovs_net_id
);
2024 INIT_LIST_HEAD(&ovs_net
->dps
);
2025 INIT_WORK(&ovs_net
->dp_notify_work
, ovs_dp_notify_wq
);
2029 static void __net_exit
ovs_exit_net(struct net
*net
)
2031 struct datapath
*dp
, *dp_next
;
2032 struct ovs_net
*ovs_net
= net_generic(net
, ovs_net_id
);
2035 list_for_each_entry_safe(dp
, dp_next
, &ovs_net
->dps
, list_node
)
2039 cancel_work_sync(&ovs_net
->dp_notify_work
);
2042 static struct pernet_operations ovs_net_ops
= {
2043 .init
= ovs_init_net
,
2044 .exit
= ovs_exit_net
,
2046 .size
= sizeof(struct ovs_net
),
2049 static int __init
dp_init(void)
2053 BUILD_BUG_ON(sizeof(struct ovs_skb_cb
) > FIELD_SIZEOF(struct sk_buff
, cb
));
2055 pr_info("Open vSwitch switching datapath\n");
2057 err
= ovs_flow_init();
2061 err
= ovs_vport_init();
2063 goto error_flow_exit
;
2065 err
= register_pernet_device(&ovs_net_ops
);
2067 goto error_vport_exit
;
2069 err
= register_netdevice_notifier(&ovs_dp_device_notifier
);
2071 goto error_netns_exit
;
2073 err
= dp_register_genl();
2075 goto error_unreg_notifier
;
2079 error_unreg_notifier
:
2080 unregister_netdevice_notifier(&ovs_dp_device_notifier
);
2082 unregister_pernet_device(&ovs_net_ops
);
2091 static void dp_cleanup(void)
2093 dp_unregister_genl(ARRAY_SIZE(dp_genl_families
));
2094 unregister_netdevice_notifier(&ovs_dp_device_notifier
);
2095 unregister_pernet_device(&ovs_net_ops
);
2101 module_init(dp_init
);
2102 module_exit(dp_cleanup
);
2104 MODULE_DESCRIPTION("Open vSwitch switching datapath");
2105 MODULE_LICENSE("GPL");