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_family
*family
, struct genl_info
*info
,
84 return info
->nlhdr
->nlmsg_flags
& NLM_F_ECHO
||
85 genl_has_listeners(family
, genl_info_net(info
)->genl_sock
,
89 static void ovs_notify(struct genl_family
*family
,
90 struct sk_buff
*skb
, struct genl_info
*info
)
92 genl_notify(family
, skb
, genl_info_net(info
), info
->snd_portid
,
93 0, info
->nlhdr
, GFP_KERNEL
);
99 * All writes e.g. Writes to device state (add/remove datapath, port, set
100 * operations on vports, etc.), Writes to other state (flow table
101 * modifications, set miscellaneous datapath parameters, etc.) are protected
104 * Reads are protected by RCU.
106 * There are a few special cases (mostly stats) that have their own
107 * synchronization but they nest under all of above and don't interact with
110 * The RTNL lock nests inside ovs_mutex.
113 static DEFINE_MUTEX(ovs_mutex
);
117 mutex_lock(&ovs_mutex
);
120 void ovs_unlock(void)
122 mutex_unlock(&ovs_mutex
);
125 #ifdef CONFIG_LOCKDEP
126 int lockdep_ovsl_is_held(void)
129 return lockdep_is_held(&ovs_mutex
);
135 static struct vport
*new_vport(const struct vport_parms
*);
136 static int queue_gso_packets(struct datapath
*dp
, struct sk_buff
*,
137 const struct dp_upcall_info
*);
138 static int queue_userspace_packet(struct datapath
*dp
, struct sk_buff
*,
139 const struct dp_upcall_info
*);
141 /* Must be called with rcu_read_lock or ovs_mutex. */
142 static struct datapath
*get_dp(struct net
*net
, int dp_ifindex
)
144 struct datapath
*dp
= NULL
;
145 struct net_device
*dev
;
148 dev
= dev_get_by_index_rcu(net
, dp_ifindex
);
150 struct vport
*vport
= ovs_internal_dev_get_vport(dev
);
159 /* Must be called with rcu_read_lock or ovs_mutex. */
160 const char *ovs_dp_name(const struct datapath
*dp
)
162 struct vport
*vport
= ovs_vport_ovsl_rcu(dp
, OVSP_LOCAL
);
163 return vport
->ops
->get_name(vport
);
166 static int get_dpifindex(struct datapath
*dp
)
173 local
= ovs_vport_rcu(dp
, OVSP_LOCAL
);
175 ifindex
= netdev_vport_priv(local
)->dev
->ifindex
;
184 static void destroy_dp_rcu(struct rcu_head
*rcu
)
186 struct datapath
*dp
= container_of(rcu
, struct datapath
, rcu
);
188 free_percpu(dp
->stats_percpu
);
189 release_net(ovs_dp_get_net(dp
));
194 static struct hlist_head
*vport_hash_bucket(const struct datapath
*dp
,
197 return &dp
->ports
[port_no
& (DP_VPORT_HASH_BUCKETS
- 1)];
200 /* Called with ovs_mutex or RCU read lock. */
201 struct vport
*ovs_lookup_vport(const struct datapath
*dp
, u16 port_no
)
204 struct hlist_head
*head
;
206 head
= vport_hash_bucket(dp
, port_no
);
207 hlist_for_each_entry_rcu(vport
, head
, dp_hash_node
) {
208 if (vport
->port_no
== port_no
)
214 /* Called with ovs_mutex. */
215 static struct vport
*new_vport(const struct vport_parms
*parms
)
219 vport
= ovs_vport_add(parms
);
220 if (!IS_ERR(vport
)) {
221 struct datapath
*dp
= parms
->dp
;
222 struct hlist_head
*head
= vport_hash_bucket(dp
, vport
->port_no
);
224 hlist_add_head_rcu(&vport
->dp_hash_node
, head
);
229 void ovs_dp_detach_port(struct vport
*p
)
233 /* First drop references to device. */
234 hlist_del_rcu(&p
->dp_hash_node
);
236 /* Then destroy it. */
240 /* Must be called with rcu_read_lock. */
241 void ovs_dp_process_packet(struct sk_buff
*skb
, struct sw_flow_key
*key
)
243 const struct vport
*p
= OVS_CB(skb
)->input_vport
;
244 struct datapath
*dp
= p
->dp
;
245 struct sw_flow
*flow
;
246 struct dp_stats_percpu
*stats
;
250 stats
= this_cpu_ptr(dp
->stats_percpu
);
253 flow
= ovs_flow_tbl_lookup_stats(&dp
->table
, key
, &n_mask_hit
);
254 if (unlikely(!flow
)) {
255 struct dp_upcall_info upcall
;
258 upcall
.cmd
= OVS_PACKET_CMD_MISS
;
260 upcall
.userdata
= NULL
;
261 upcall
.portid
= ovs_vport_find_upcall_portid(p
, skb
);
262 error
= ovs_dp_upcall(dp
, skb
, &upcall
);
267 stats_counter
= &stats
->n_missed
;
271 OVS_CB(skb
)->flow
= flow
;
273 ovs_flow_stats_update(OVS_CB(skb
)->flow
, key
->tp
.flags
, skb
);
274 ovs_execute_actions(dp
, skb
, key
);
275 stats_counter
= &stats
->n_hit
;
278 /* Update datapath statistics. */
279 u64_stats_update_begin(&stats
->syncp
);
281 stats
->n_mask_hit
+= n_mask_hit
;
282 u64_stats_update_end(&stats
->syncp
);
285 int ovs_dp_upcall(struct datapath
*dp
, struct sk_buff
*skb
,
286 const struct dp_upcall_info
*upcall_info
)
288 struct dp_stats_percpu
*stats
;
291 if (upcall_info
->portid
== 0) {
296 if (!skb_is_gso(skb
))
297 err
= queue_userspace_packet(dp
, skb
, upcall_info
);
299 err
= queue_gso_packets(dp
, skb
, upcall_info
);
306 stats
= this_cpu_ptr(dp
->stats_percpu
);
308 u64_stats_update_begin(&stats
->syncp
);
310 u64_stats_update_end(&stats
->syncp
);
315 static int queue_gso_packets(struct datapath
*dp
, struct sk_buff
*skb
,
316 const struct dp_upcall_info
*upcall_info
)
318 unsigned short gso_type
= skb_shinfo(skb
)->gso_type
;
319 struct dp_upcall_info later_info
;
320 struct sw_flow_key later_key
;
321 struct sk_buff
*segs
, *nskb
;
324 segs
= __skb_gso_segment(skb
, NETIF_F_SG
, false);
326 return PTR_ERR(segs
);
328 /* Queue all of the segments. */
331 err
= queue_userspace_packet(dp
, skb
, upcall_info
);
335 if (skb
== segs
&& gso_type
& SKB_GSO_UDP
) {
336 /* The initial flow key extracted by ovs_flow_extract()
337 * in this case is for a first fragment, so we need to
338 * properly mark later fragments.
340 later_key
= *upcall_info
->key
;
341 later_key
.ip
.frag
= OVS_FRAG_TYPE_LATER
;
343 later_info
= *upcall_info
;
344 later_info
.key
= &later_key
;
345 upcall_info
= &later_info
;
347 } while ((skb
= skb
->next
));
349 /* Free all of the segments. */
357 } while ((skb
= nskb
));
361 static size_t key_attr_size(void)
363 return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */
364 + nla_total_size(0) /* OVS_KEY_ATTR_TUNNEL */
365 + nla_total_size(8) /* OVS_TUNNEL_KEY_ATTR_ID */
366 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_SRC */
367 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_DST */
368 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TOS */
369 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TTL */
370 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT */
371 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_CSUM */
372 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_OAM */
373 + nla_total_size(256) /* OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS */
374 + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */
375 + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
376 + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
377 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
378 + nla_total_size(4) /* OVS_KEY_ATTR_8021Q */
379 + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
380 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
381 + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
382 + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
383 + nla_total_size(28); /* OVS_KEY_ATTR_ND */
386 static size_t upcall_msg_size(const struct nlattr
*userdata
,
389 size_t size
= NLMSG_ALIGN(sizeof(struct ovs_header
))
390 + nla_total_size(hdrlen
) /* OVS_PACKET_ATTR_PACKET */
391 + nla_total_size(key_attr_size()); /* OVS_PACKET_ATTR_KEY */
393 /* OVS_PACKET_ATTR_USERDATA */
395 size
+= NLA_ALIGN(userdata
->nla_len
);
400 static int queue_userspace_packet(struct datapath
*dp
, struct sk_buff
*skb
,
401 const struct dp_upcall_info
*upcall_info
)
403 struct ovs_header
*upcall
;
404 struct sk_buff
*nskb
= NULL
;
405 struct sk_buff
*user_skb
= NULL
; /* to be queued to userspace */
407 struct genl_info info
= {
408 .dst_sk
= ovs_dp_get_net(dp
)->genl_sock
,
409 .snd_portid
= upcall_info
->portid
,
415 dp_ifindex
= get_dpifindex(dp
);
419 if (vlan_tx_tag_present(skb
)) {
420 nskb
= skb_clone(skb
, GFP_ATOMIC
);
424 nskb
= __vlan_put_tag(nskb
, nskb
->vlan_proto
, vlan_tx_tag_get(nskb
));
432 if (nla_attr_size(skb
->len
) > USHRT_MAX
) {
437 /* Complete checksum if needed */
438 if (skb
->ip_summed
== CHECKSUM_PARTIAL
&&
439 (err
= skb_checksum_help(skb
)))
442 /* Older versions of OVS user space enforce alignment of the last
443 * Netlink attribute to NLA_ALIGNTO which would require extensive
444 * padding logic. Only perform zerocopy if padding is not required.
446 if (dp
->user_features
& OVS_DP_F_UNALIGNED
)
447 hlen
= skb_zerocopy_headlen(skb
);
451 len
= upcall_msg_size(upcall_info
->userdata
, hlen
);
452 user_skb
= genlmsg_new_unicast(len
, &info
, GFP_ATOMIC
);
458 upcall
= genlmsg_put(user_skb
, 0, 0, &dp_packet_genl_family
,
459 0, upcall_info
->cmd
);
460 upcall
->dp_ifindex
= dp_ifindex
;
462 nla
= nla_nest_start(user_skb
, OVS_PACKET_ATTR_KEY
);
463 err
= ovs_nla_put_flow(upcall_info
->key
, upcall_info
->key
, user_skb
);
465 nla_nest_end(user_skb
, nla
);
467 if (upcall_info
->userdata
)
468 __nla_put(user_skb
, OVS_PACKET_ATTR_USERDATA
,
469 nla_len(upcall_info
->userdata
),
470 nla_data(upcall_info
->userdata
));
472 /* Only reserve room for attribute header, packet data is added
473 * in skb_zerocopy() */
474 if (!(nla
= nla_reserve(user_skb
, OVS_PACKET_ATTR_PACKET
, 0))) {
478 nla
->nla_len
= nla_attr_size(skb
->len
);
480 err
= skb_zerocopy(user_skb
, skb
, skb
->len
, hlen
);
484 /* Pad OVS_PACKET_ATTR_PACKET if linear copy was performed */
485 if (!(dp
->user_features
& OVS_DP_F_UNALIGNED
)) {
486 size_t plen
= NLA_ALIGN(user_skb
->len
) - user_skb
->len
;
489 memset(skb_put(user_skb
, plen
), 0, plen
);
492 ((struct nlmsghdr
*) user_skb
->data
)->nlmsg_len
= user_skb
->len
;
494 err
= genlmsg_unicast(ovs_dp_get_net(dp
), user_skb
, upcall_info
->portid
);
504 static int ovs_packet_cmd_execute(struct sk_buff
*skb
, struct genl_info
*info
)
506 struct ovs_header
*ovs_header
= info
->userhdr
;
507 struct nlattr
**a
= info
->attrs
;
508 struct sw_flow_actions
*acts
;
509 struct sk_buff
*packet
;
510 struct sw_flow
*flow
;
513 struct vport
*input_vport
;
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_key_extract_userspace(a
[OVS_PACKET_ATTR_KEY
], packet
,
553 acts
= ovs_nla_alloc_flow_actions(nla_len(a
[OVS_PACKET_ATTR_ACTIONS
]));
558 err
= ovs_nla_copy_actions(a
[OVS_PACKET_ATTR_ACTIONS
],
559 &flow
->key
, 0, &acts
);
563 rcu_assign_pointer(flow
->sf_acts
, acts
);
565 OVS_CB(packet
)->egress_tun_info
= NULL
;
566 OVS_CB(packet
)->flow
= flow
;
567 packet
->priority
= flow
->key
.phy
.priority
;
568 packet
->mark
= flow
->key
.phy
.skb_mark
;
571 dp
= get_dp(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
576 input_vport
= ovs_vport_rcu(dp
, flow
->key
.phy
.in_port
);
578 input_vport
= ovs_vport_rcu(dp
, OVSP_LOCAL
);
583 OVS_CB(packet
)->input_vport
= input_vport
;
586 err
= ovs_execute_actions(dp
, packet
, &flow
->key
);
590 ovs_flow_free(flow
, false);
596 ovs_flow_free(flow
, false);
603 static const struct nla_policy packet_policy
[OVS_PACKET_ATTR_MAX
+ 1] = {
604 [OVS_PACKET_ATTR_PACKET
] = { .len
= ETH_HLEN
},
605 [OVS_PACKET_ATTR_KEY
] = { .type
= NLA_NESTED
},
606 [OVS_PACKET_ATTR_ACTIONS
] = { .type
= NLA_NESTED
},
609 static const struct genl_ops dp_packet_genl_ops
[] = {
610 { .cmd
= OVS_PACKET_CMD_EXECUTE
,
611 .flags
= GENL_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
612 .policy
= packet_policy
,
613 .doit
= ovs_packet_cmd_execute
617 static struct genl_family dp_packet_genl_family
= {
618 .id
= GENL_ID_GENERATE
,
619 .hdrsize
= sizeof(struct ovs_header
),
620 .name
= OVS_PACKET_FAMILY
,
621 .version
= OVS_PACKET_VERSION
,
622 .maxattr
= OVS_PACKET_ATTR_MAX
,
624 .parallel_ops
= true,
625 .ops
= dp_packet_genl_ops
,
626 .n_ops
= ARRAY_SIZE(dp_packet_genl_ops
),
629 static void get_dp_stats(struct datapath
*dp
, struct ovs_dp_stats
*stats
,
630 struct ovs_dp_megaflow_stats
*mega_stats
)
634 memset(mega_stats
, 0, sizeof(*mega_stats
));
636 stats
->n_flows
= ovs_flow_tbl_count(&dp
->table
);
637 mega_stats
->n_masks
= ovs_flow_tbl_num_masks(&dp
->table
);
639 stats
->n_hit
= stats
->n_missed
= stats
->n_lost
= 0;
641 for_each_possible_cpu(i
) {
642 const struct dp_stats_percpu
*percpu_stats
;
643 struct dp_stats_percpu local_stats
;
646 percpu_stats
= per_cpu_ptr(dp
->stats_percpu
, i
);
649 start
= u64_stats_fetch_begin_irq(&percpu_stats
->syncp
);
650 local_stats
= *percpu_stats
;
651 } while (u64_stats_fetch_retry_irq(&percpu_stats
->syncp
, start
));
653 stats
->n_hit
+= local_stats
.n_hit
;
654 stats
->n_missed
+= local_stats
.n_missed
;
655 stats
->n_lost
+= local_stats
.n_lost
;
656 mega_stats
->n_mask_hit
+= local_stats
.n_mask_hit
;
660 static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions
*acts
)
662 return NLMSG_ALIGN(sizeof(struct ovs_header
))
663 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_KEY */
664 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_MASK */
665 + nla_total_size(sizeof(struct ovs_flow_stats
)) /* OVS_FLOW_ATTR_STATS */
666 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
667 + nla_total_size(8) /* OVS_FLOW_ATTR_USED */
668 + nla_total_size(acts
->actions_len
); /* OVS_FLOW_ATTR_ACTIONS */
671 /* Called with ovs_mutex or RCU read lock. */
672 static int ovs_flow_cmd_fill_info(const struct sw_flow
*flow
, int dp_ifindex
,
673 struct sk_buff
*skb
, u32 portid
,
674 u32 seq
, u32 flags
, u8 cmd
)
676 const int skb_orig_len
= skb
->len
;
677 struct nlattr
*start
;
678 struct ovs_flow_stats stats
;
681 struct ovs_header
*ovs_header
;
685 ovs_header
= genlmsg_put(skb
, portid
, seq
, &dp_flow_genl_family
, flags
, cmd
);
689 ovs_header
->dp_ifindex
= dp_ifindex
;
692 nla
= nla_nest_start(skb
, OVS_FLOW_ATTR_KEY
);
694 goto nla_put_failure
;
696 err
= ovs_nla_put_flow(&flow
->unmasked_key
, &flow
->unmasked_key
, skb
);
699 nla_nest_end(skb
, nla
);
701 nla
= nla_nest_start(skb
, OVS_FLOW_ATTR_MASK
);
703 goto nla_put_failure
;
705 err
= ovs_nla_put_flow(&flow
->key
, &flow
->mask
->key
, skb
);
709 nla_nest_end(skb
, nla
);
711 ovs_flow_stats_get(flow
, &stats
, &used
, &tcp_flags
);
714 nla_put_u64(skb
, OVS_FLOW_ATTR_USED
, ovs_flow_used_time(used
)))
715 goto nla_put_failure
;
717 if (stats
.n_packets
&&
718 nla_put(skb
, OVS_FLOW_ATTR_STATS
, sizeof(struct ovs_flow_stats
), &stats
))
719 goto nla_put_failure
;
721 if ((u8
)ntohs(tcp_flags
) &&
722 nla_put_u8(skb
, OVS_FLOW_ATTR_TCP_FLAGS
, (u8
)ntohs(tcp_flags
)))
723 goto nla_put_failure
;
725 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
726 * this is the first flow to be dumped into 'skb'. This is unusual for
727 * Netlink but individual action lists can be longer than
728 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
729 * The userspace caller can always fetch the actions separately if it
730 * really wants them. (Most userspace callers in fact don't care.)
732 * This can only fail for dump operations because the skb is always
733 * properly sized for single flows.
735 start
= nla_nest_start(skb
, OVS_FLOW_ATTR_ACTIONS
);
737 const struct sw_flow_actions
*sf_acts
;
739 sf_acts
= rcu_dereference_ovsl(flow
->sf_acts
);
740 err
= ovs_nla_put_actions(sf_acts
->actions
,
741 sf_acts
->actions_len
, skb
);
744 nla_nest_end(skb
, start
);
749 nla_nest_cancel(skb
, start
);
751 } else if (skb_orig_len
)
752 goto nla_put_failure
;
754 return genlmsg_end(skb
, ovs_header
);
759 genlmsg_cancel(skb
, ovs_header
);
763 /* May not be called with RCU read lock. */
764 static struct sk_buff
*ovs_flow_cmd_alloc_info(const struct sw_flow_actions
*acts
,
765 struct genl_info
*info
,
770 if (!always
&& !ovs_must_notify(&dp_flow_genl_family
, info
, 0))
773 skb
= genlmsg_new_unicast(ovs_flow_cmd_msg_size(acts
), info
, GFP_KERNEL
);
775 return ERR_PTR(-ENOMEM
);
780 /* Called with ovs_mutex. */
781 static struct sk_buff
*ovs_flow_cmd_build_info(const struct sw_flow
*flow
,
783 struct genl_info
*info
, u8 cmd
,
789 skb
= ovs_flow_cmd_alloc_info(ovsl_dereference(flow
->sf_acts
), info
,
791 if (IS_ERR_OR_NULL(skb
))
794 retval
= ovs_flow_cmd_fill_info(flow
, dp_ifindex
, skb
,
795 info
->snd_portid
, info
->snd_seq
, 0,
801 static int ovs_flow_cmd_new(struct sk_buff
*skb
, struct genl_info
*info
)
803 struct nlattr
**a
= info
->attrs
;
804 struct ovs_header
*ovs_header
= info
->userhdr
;
805 struct sw_flow
*flow
, *new_flow
;
806 struct sw_flow_mask mask
;
807 struct sk_buff
*reply
;
809 struct sw_flow_actions
*acts
;
810 struct sw_flow_match match
;
813 /* Must have key and actions. */
815 if (!a
[OVS_FLOW_ATTR_KEY
])
817 if (!a
[OVS_FLOW_ATTR_ACTIONS
])
820 /* Most of the time we need to allocate a new flow, do it before
823 new_flow
= ovs_flow_alloc();
824 if (IS_ERR(new_flow
)) {
825 error
= PTR_ERR(new_flow
);
830 ovs_match_init(&match
, &new_flow
->unmasked_key
, &mask
);
831 error
= ovs_nla_get_match(&match
,
832 a
[OVS_FLOW_ATTR_KEY
], a
[OVS_FLOW_ATTR_MASK
]);
836 ovs_flow_mask_key(&new_flow
->key
, &new_flow
->unmasked_key
, &mask
);
838 /* Validate actions. */
839 acts
= ovs_nla_alloc_flow_actions(nla_len(a
[OVS_FLOW_ATTR_ACTIONS
]));
840 error
= PTR_ERR(acts
);
844 error
= ovs_nla_copy_actions(a
[OVS_FLOW_ATTR_ACTIONS
], &new_flow
->key
,
847 OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
851 reply
= ovs_flow_cmd_alloc_info(acts
, info
, false);
853 error
= PTR_ERR(reply
);
858 dp
= get_dp(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
863 /* Check if this is a duplicate flow */
864 flow
= ovs_flow_tbl_lookup(&dp
->table
, &new_flow
->unmasked_key
);
866 rcu_assign_pointer(new_flow
->sf_acts
, acts
);
868 /* Put flow in bucket. */
869 error
= ovs_flow_tbl_insert(&dp
->table
, new_flow
, &mask
);
870 if (unlikely(error
)) {
875 if (unlikely(reply
)) {
876 error
= ovs_flow_cmd_fill_info(new_flow
,
877 ovs_header
->dp_ifindex
,
878 reply
, info
->snd_portid
,
885 struct sw_flow_actions
*old_acts
;
887 /* Bail out if we're not allowed to modify an existing flow.
888 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
889 * because Generic Netlink treats the latter as a dump
890 * request. We also accept NLM_F_EXCL in case that bug ever
893 if (unlikely(info
->nlhdr
->nlmsg_flags
& (NLM_F_CREATE
898 /* The unmasked key has to be the same for flow updates. */
899 if (unlikely(!ovs_flow_cmp_unmasked_key(flow
, &match
))) {
900 flow
= ovs_flow_tbl_lookup_exact(&dp
->table
, &match
);
906 /* Update actions. */
907 old_acts
= ovsl_dereference(flow
->sf_acts
);
908 rcu_assign_pointer(flow
->sf_acts
, acts
);
910 if (unlikely(reply
)) {
911 error
= ovs_flow_cmd_fill_info(flow
,
912 ovs_header
->dp_ifindex
,
913 reply
, info
->snd_portid
,
920 ovs_nla_free_flow_actions(old_acts
);
921 ovs_flow_free(new_flow
, false);
925 ovs_notify(&dp_flow_genl_family
, reply
, info
);
934 ovs_flow_free(new_flow
, false);
939 static struct sw_flow_actions
*get_flow_actions(const struct nlattr
*a
,
940 const struct sw_flow_key
*key
,
941 const struct sw_flow_mask
*mask
)
943 struct sw_flow_actions
*acts
;
944 struct sw_flow_key masked_key
;
947 acts
= ovs_nla_alloc_flow_actions(nla_len(a
));
951 ovs_flow_mask_key(&masked_key
, key
, mask
);
952 error
= ovs_nla_copy_actions(a
, &masked_key
, 0, &acts
);
954 OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
956 return ERR_PTR(error
);
962 static int ovs_flow_cmd_set(struct sk_buff
*skb
, struct genl_info
*info
)
964 struct nlattr
**a
= info
->attrs
;
965 struct ovs_header
*ovs_header
= info
->userhdr
;
966 struct sw_flow_key key
;
967 struct sw_flow
*flow
;
968 struct sw_flow_mask mask
;
969 struct sk_buff
*reply
= NULL
;
971 struct sw_flow_actions
*old_acts
= NULL
, *acts
= NULL
;
972 struct sw_flow_match match
;
977 if (!a
[OVS_FLOW_ATTR_KEY
])
980 ovs_match_init(&match
, &key
, &mask
);
981 error
= ovs_nla_get_match(&match
,
982 a
[OVS_FLOW_ATTR_KEY
], a
[OVS_FLOW_ATTR_MASK
]);
986 /* Validate actions. */
987 if (a
[OVS_FLOW_ATTR_ACTIONS
]) {
988 acts
= get_flow_actions(a
[OVS_FLOW_ATTR_ACTIONS
], &key
, &mask
);
990 error
= PTR_ERR(acts
);
995 /* Can allocate before locking if have acts. */
997 reply
= ovs_flow_cmd_alloc_info(acts
, info
, false);
999 error
= PTR_ERR(reply
);
1000 goto err_kfree_acts
;
1005 dp
= get_dp(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
1006 if (unlikely(!dp
)) {
1008 goto err_unlock_ovs
;
1010 /* Check that the flow exists. */
1011 flow
= ovs_flow_tbl_lookup_exact(&dp
->table
, &match
);
1012 if (unlikely(!flow
)) {
1014 goto err_unlock_ovs
;
1017 /* Update actions, if present. */
1019 old_acts
= ovsl_dereference(flow
->sf_acts
);
1020 rcu_assign_pointer(flow
->sf_acts
, acts
);
1022 if (unlikely(reply
)) {
1023 error
= ovs_flow_cmd_fill_info(flow
,
1024 ovs_header
->dp_ifindex
,
1025 reply
, info
->snd_portid
,
1031 /* Could not alloc without acts before locking. */
1032 reply
= ovs_flow_cmd_build_info(flow
, ovs_header
->dp_ifindex
,
1033 info
, OVS_FLOW_CMD_NEW
, false);
1034 if (unlikely(IS_ERR(reply
))) {
1035 error
= PTR_ERR(reply
);
1036 goto err_unlock_ovs
;
1041 if (a
[OVS_FLOW_ATTR_CLEAR
])
1042 ovs_flow_stats_clear(flow
);
1046 ovs_notify(&dp_flow_genl_family
, reply
, info
);
1048 ovs_nla_free_flow_actions(old_acts
);
1061 static int ovs_flow_cmd_get(struct sk_buff
*skb
, struct genl_info
*info
)
1063 struct nlattr
**a
= info
->attrs
;
1064 struct ovs_header
*ovs_header
= info
->userhdr
;
1065 struct sw_flow_key key
;
1066 struct sk_buff
*reply
;
1067 struct sw_flow
*flow
;
1068 struct datapath
*dp
;
1069 struct sw_flow_match match
;
1072 if (!a
[OVS_FLOW_ATTR_KEY
]) {
1073 OVS_NLERR("Flow get message rejected, Key attribute missing.\n");
1077 ovs_match_init(&match
, &key
, NULL
);
1078 err
= ovs_nla_get_match(&match
, a
[OVS_FLOW_ATTR_KEY
], NULL
);
1083 dp
= get_dp(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
1089 flow
= ovs_flow_tbl_lookup_exact(&dp
->table
, &match
);
1095 reply
= ovs_flow_cmd_build_info(flow
, ovs_header
->dp_ifindex
, info
,
1096 OVS_FLOW_CMD_NEW
, true);
1097 if (IS_ERR(reply
)) {
1098 err
= PTR_ERR(reply
);
1103 return genlmsg_reply(reply
, info
);
1109 static int ovs_flow_cmd_del(struct sk_buff
*skb
, struct genl_info
*info
)
1111 struct nlattr
**a
= info
->attrs
;
1112 struct ovs_header
*ovs_header
= info
->userhdr
;
1113 struct sw_flow_key key
;
1114 struct sk_buff
*reply
;
1115 struct sw_flow
*flow
;
1116 struct datapath
*dp
;
1117 struct sw_flow_match match
;
1120 if (likely(a
[OVS_FLOW_ATTR_KEY
])) {
1121 ovs_match_init(&match
, &key
, NULL
);
1122 err
= ovs_nla_get_match(&match
, a
[OVS_FLOW_ATTR_KEY
], NULL
);
1128 dp
= get_dp(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
1129 if (unlikely(!dp
)) {
1134 if (unlikely(!a
[OVS_FLOW_ATTR_KEY
])) {
1135 err
= ovs_flow_tbl_flush(&dp
->table
);
1139 flow
= ovs_flow_tbl_lookup_exact(&dp
->table
, &match
);
1140 if (unlikely(!flow
)) {
1145 ovs_flow_tbl_remove(&dp
->table
, flow
);
1148 reply
= ovs_flow_cmd_alloc_info((const struct sw_flow_actions __force
*) flow
->sf_acts
,
1150 if (likely(reply
)) {
1151 if (likely(!IS_ERR(reply
))) {
1152 rcu_read_lock(); /*To keep RCU checker happy. */
1153 err
= ovs_flow_cmd_fill_info(flow
, ovs_header
->dp_ifindex
,
1154 reply
, info
->snd_portid
,
1160 ovs_notify(&dp_flow_genl_family
, reply
, info
);
1162 netlink_set_err(sock_net(skb
->sk
)->genl_sock
, 0, 0, PTR_ERR(reply
));
1166 ovs_flow_free(flow
, true);
1173 static int ovs_flow_cmd_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1175 struct ovs_header
*ovs_header
= genlmsg_data(nlmsg_data(cb
->nlh
));
1176 struct table_instance
*ti
;
1177 struct datapath
*dp
;
1180 dp
= get_dp(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
1186 ti
= rcu_dereference(dp
->table
.ti
);
1188 struct sw_flow
*flow
;
1191 bucket
= cb
->args
[0];
1193 flow
= ovs_flow_tbl_dump_next(ti
, &bucket
, &obj
);
1197 if (ovs_flow_cmd_fill_info(flow
, ovs_header
->dp_ifindex
, skb
,
1198 NETLINK_CB(cb
->skb
).portid
,
1199 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
,
1200 OVS_FLOW_CMD_NEW
) < 0)
1203 cb
->args
[0] = bucket
;
1210 static const struct nla_policy flow_policy
[OVS_FLOW_ATTR_MAX
+ 1] = {
1211 [OVS_FLOW_ATTR_KEY
] = { .type
= NLA_NESTED
},
1212 [OVS_FLOW_ATTR_ACTIONS
] = { .type
= NLA_NESTED
},
1213 [OVS_FLOW_ATTR_CLEAR
] = { .type
= NLA_FLAG
},
1216 static const struct genl_ops dp_flow_genl_ops
[] = {
1217 { .cmd
= OVS_FLOW_CMD_NEW
,
1218 .flags
= GENL_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1219 .policy
= flow_policy
,
1220 .doit
= ovs_flow_cmd_new
1222 { .cmd
= OVS_FLOW_CMD_DEL
,
1223 .flags
= GENL_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1224 .policy
= flow_policy
,
1225 .doit
= ovs_flow_cmd_del
1227 { .cmd
= OVS_FLOW_CMD_GET
,
1228 .flags
= 0, /* OK for unprivileged users. */
1229 .policy
= flow_policy
,
1230 .doit
= ovs_flow_cmd_get
,
1231 .dumpit
= ovs_flow_cmd_dump
1233 { .cmd
= OVS_FLOW_CMD_SET
,
1234 .flags
= GENL_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1235 .policy
= flow_policy
,
1236 .doit
= ovs_flow_cmd_set
,
1240 static struct genl_family dp_flow_genl_family
= {
1241 .id
= GENL_ID_GENERATE
,
1242 .hdrsize
= sizeof(struct ovs_header
),
1243 .name
= OVS_FLOW_FAMILY
,
1244 .version
= OVS_FLOW_VERSION
,
1245 .maxattr
= OVS_FLOW_ATTR_MAX
,
1247 .parallel_ops
= true,
1248 .ops
= dp_flow_genl_ops
,
1249 .n_ops
= ARRAY_SIZE(dp_flow_genl_ops
),
1250 .mcgrps
= &ovs_dp_flow_multicast_group
,
1254 static size_t ovs_dp_cmd_msg_size(void)
1256 size_t msgsize
= NLMSG_ALIGN(sizeof(struct ovs_header
));
1258 msgsize
+= nla_total_size(IFNAMSIZ
);
1259 msgsize
+= nla_total_size(sizeof(struct ovs_dp_stats
));
1260 msgsize
+= nla_total_size(sizeof(struct ovs_dp_megaflow_stats
));
1261 msgsize
+= nla_total_size(sizeof(u32
)); /* OVS_DP_ATTR_USER_FEATURES */
1266 /* Called with ovs_mutex or RCU read lock. */
1267 static int ovs_dp_cmd_fill_info(struct datapath
*dp
, struct sk_buff
*skb
,
1268 u32 portid
, u32 seq
, u32 flags
, u8 cmd
)
1270 struct ovs_header
*ovs_header
;
1271 struct ovs_dp_stats dp_stats
;
1272 struct ovs_dp_megaflow_stats dp_megaflow_stats
;
1275 ovs_header
= genlmsg_put(skb
, portid
, seq
, &dp_datapath_genl_family
,
1280 ovs_header
->dp_ifindex
= get_dpifindex(dp
);
1282 err
= nla_put_string(skb
, OVS_DP_ATTR_NAME
, ovs_dp_name(dp
));
1284 goto nla_put_failure
;
1286 get_dp_stats(dp
, &dp_stats
, &dp_megaflow_stats
);
1287 if (nla_put(skb
, OVS_DP_ATTR_STATS
, sizeof(struct ovs_dp_stats
),
1289 goto nla_put_failure
;
1291 if (nla_put(skb
, OVS_DP_ATTR_MEGAFLOW_STATS
,
1292 sizeof(struct ovs_dp_megaflow_stats
),
1293 &dp_megaflow_stats
))
1294 goto nla_put_failure
;
1296 if (nla_put_u32(skb
, OVS_DP_ATTR_USER_FEATURES
, dp
->user_features
))
1297 goto nla_put_failure
;
1299 return genlmsg_end(skb
, ovs_header
);
1302 genlmsg_cancel(skb
, ovs_header
);
1307 static struct sk_buff
*ovs_dp_cmd_alloc_info(struct genl_info
*info
)
1309 return genlmsg_new_unicast(ovs_dp_cmd_msg_size(), info
, GFP_KERNEL
);
1312 /* Called with rcu_read_lock or ovs_mutex. */
1313 static struct datapath
*lookup_datapath(struct net
*net
,
1314 struct ovs_header
*ovs_header
,
1315 struct nlattr
*a
[OVS_DP_ATTR_MAX
+ 1])
1317 struct datapath
*dp
;
1319 if (!a
[OVS_DP_ATTR_NAME
])
1320 dp
= get_dp(net
, ovs_header
->dp_ifindex
);
1322 struct vport
*vport
;
1324 vport
= ovs_vport_locate(net
, nla_data(a
[OVS_DP_ATTR_NAME
]));
1325 dp
= vport
&& vport
->port_no
== OVSP_LOCAL
? vport
->dp
: NULL
;
1327 return dp
? dp
: ERR_PTR(-ENODEV
);
1330 static void ovs_dp_reset_user_features(struct sk_buff
*skb
, struct genl_info
*info
)
1332 struct datapath
*dp
;
1334 dp
= lookup_datapath(sock_net(skb
->sk
), info
->userhdr
, info
->attrs
);
1338 WARN(dp
->user_features
, "Dropping previously announced user features\n");
1339 dp
->user_features
= 0;
1342 static void ovs_dp_change(struct datapath
*dp
, struct nlattr
**a
)
1344 if (a
[OVS_DP_ATTR_USER_FEATURES
])
1345 dp
->user_features
= nla_get_u32(a
[OVS_DP_ATTR_USER_FEATURES
]);
1348 static int ovs_dp_cmd_new(struct sk_buff
*skb
, struct genl_info
*info
)
1350 struct nlattr
**a
= info
->attrs
;
1351 struct vport_parms parms
;
1352 struct sk_buff
*reply
;
1353 struct datapath
*dp
;
1354 struct vport
*vport
;
1355 struct ovs_net
*ovs_net
;
1359 if (!a
[OVS_DP_ATTR_NAME
] || !a
[OVS_DP_ATTR_UPCALL_PID
])
1362 reply
= ovs_dp_cmd_alloc_info(info
);
1367 dp
= kzalloc(sizeof(*dp
), GFP_KERNEL
);
1369 goto err_free_reply
;
1371 ovs_dp_set_net(dp
, hold_net(sock_net(skb
->sk
)));
1373 /* Allocate table. */
1374 err
= ovs_flow_tbl_init(&dp
->table
);
1378 dp
->stats_percpu
= netdev_alloc_pcpu_stats(struct dp_stats_percpu
);
1379 if (!dp
->stats_percpu
) {
1381 goto err_destroy_table
;
1384 dp
->ports
= kmalloc(DP_VPORT_HASH_BUCKETS
* sizeof(struct hlist_head
),
1388 goto err_destroy_percpu
;
1391 for (i
= 0; i
< DP_VPORT_HASH_BUCKETS
; i
++)
1392 INIT_HLIST_HEAD(&dp
->ports
[i
]);
1394 /* Set up our datapath device. */
1395 parms
.name
= nla_data(a
[OVS_DP_ATTR_NAME
]);
1396 parms
.type
= OVS_VPORT_TYPE_INTERNAL
;
1397 parms
.options
= NULL
;
1399 parms
.port_no
= OVSP_LOCAL
;
1400 parms
.upcall_portids
= a
[OVS_DP_ATTR_UPCALL_PID
];
1402 ovs_dp_change(dp
, a
);
1404 /* So far only local changes have been made, now need the lock. */
1407 vport
= new_vport(&parms
);
1408 if (IS_ERR(vport
)) {
1409 err
= PTR_ERR(vport
);
1413 if (err
== -EEXIST
) {
1414 /* An outdated user space instance that does not understand
1415 * the concept of user_features has attempted to create a new
1416 * datapath and is likely to reuse it. Drop all user features.
1418 if (info
->genlhdr
->version
< OVS_DP_VER_FEATURES
)
1419 ovs_dp_reset_user_features(skb
, info
);
1422 goto err_destroy_ports_array
;
1425 err
= ovs_dp_cmd_fill_info(dp
, reply
, info
->snd_portid
,
1426 info
->snd_seq
, 0, OVS_DP_CMD_NEW
);
1429 ovs_net
= net_generic(ovs_dp_get_net(dp
), ovs_net_id
);
1430 list_add_tail_rcu(&dp
->list_node
, &ovs_net
->dps
);
1434 ovs_notify(&dp_datapath_genl_family
, reply
, info
);
1437 err_destroy_ports_array
:
1441 free_percpu(dp
->stats_percpu
);
1443 ovs_flow_tbl_destroy(&dp
->table
, false);
1445 release_net(ovs_dp_get_net(dp
));
1453 /* Called with ovs_mutex. */
1454 static void __dp_destroy(struct datapath
*dp
)
1458 for (i
= 0; i
< DP_VPORT_HASH_BUCKETS
; i
++) {
1459 struct vport
*vport
;
1460 struct hlist_node
*n
;
1462 hlist_for_each_entry_safe(vport
, n
, &dp
->ports
[i
], dp_hash_node
)
1463 if (vport
->port_no
!= OVSP_LOCAL
)
1464 ovs_dp_detach_port(vport
);
1467 list_del_rcu(&dp
->list_node
);
1469 /* OVSP_LOCAL is datapath internal port. We need to make sure that
1470 * all ports in datapath are destroyed first before freeing datapath.
1472 ovs_dp_detach_port(ovs_vport_ovsl(dp
, OVSP_LOCAL
));
1474 /* RCU destroy the flow table */
1475 ovs_flow_tbl_destroy(&dp
->table
, true);
1477 call_rcu(&dp
->rcu
, destroy_dp_rcu
);
1480 static int ovs_dp_cmd_del(struct sk_buff
*skb
, struct genl_info
*info
)
1482 struct sk_buff
*reply
;
1483 struct datapath
*dp
;
1486 reply
= ovs_dp_cmd_alloc_info(info
);
1491 dp
= lookup_datapath(sock_net(skb
->sk
), info
->userhdr
, info
->attrs
);
1494 goto err_unlock_free
;
1496 err
= ovs_dp_cmd_fill_info(dp
, reply
, info
->snd_portid
,
1497 info
->snd_seq
, 0, OVS_DP_CMD_DEL
);
1503 ovs_notify(&dp_datapath_genl_family
, reply
, info
);
1513 static int ovs_dp_cmd_set(struct sk_buff
*skb
, struct genl_info
*info
)
1515 struct sk_buff
*reply
;
1516 struct datapath
*dp
;
1519 reply
= ovs_dp_cmd_alloc_info(info
);
1524 dp
= lookup_datapath(sock_net(skb
->sk
), info
->userhdr
, info
->attrs
);
1527 goto err_unlock_free
;
1529 ovs_dp_change(dp
, info
->attrs
);
1531 err
= ovs_dp_cmd_fill_info(dp
, reply
, info
->snd_portid
,
1532 info
->snd_seq
, 0, OVS_DP_CMD_NEW
);
1536 ovs_notify(&dp_datapath_genl_family
, reply
, info
);
1546 static int ovs_dp_cmd_get(struct sk_buff
*skb
, struct genl_info
*info
)
1548 struct sk_buff
*reply
;
1549 struct datapath
*dp
;
1552 reply
= ovs_dp_cmd_alloc_info(info
);
1557 dp
= lookup_datapath(sock_net(skb
->sk
), info
->userhdr
, info
->attrs
);
1560 goto err_unlock_free
;
1562 err
= ovs_dp_cmd_fill_info(dp
, reply
, info
->snd_portid
,
1563 info
->snd_seq
, 0, OVS_DP_CMD_NEW
);
1567 return genlmsg_reply(reply
, info
);
1575 static int ovs_dp_cmd_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1577 struct ovs_net
*ovs_net
= net_generic(sock_net(skb
->sk
), ovs_net_id
);
1578 struct datapath
*dp
;
1579 int skip
= cb
->args
[0];
1583 list_for_each_entry_rcu(dp
, &ovs_net
->dps
, list_node
) {
1585 ovs_dp_cmd_fill_info(dp
, skb
, NETLINK_CB(cb
->skb
).portid
,
1586 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
,
1587 OVS_DP_CMD_NEW
) < 0)
1598 static const struct nla_policy datapath_policy
[OVS_DP_ATTR_MAX
+ 1] = {
1599 [OVS_DP_ATTR_NAME
] = { .type
= NLA_NUL_STRING
, .len
= IFNAMSIZ
- 1 },
1600 [OVS_DP_ATTR_UPCALL_PID
] = { .type
= NLA_U32
},
1601 [OVS_DP_ATTR_USER_FEATURES
] = { .type
= NLA_U32
},
1604 static const struct genl_ops dp_datapath_genl_ops
[] = {
1605 { .cmd
= OVS_DP_CMD_NEW
,
1606 .flags
= GENL_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1607 .policy
= datapath_policy
,
1608 .doit
= ovs_dp_cmd_new
1610 { .cmd
= OVS_DP_CMD_DEL
,
1611 .flags
= GENL_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1612 .policy
= datapath_policy
,
1613 .doit
= ovs_dp_cmd_del
1615 { .cmd
= OVS_DP_CMD_GET
,
1616 .flags
= 0, /* OK for unprivileged users. */
1617 .policy
= datapath_policy
,
1618 .doit
= ovs_dp_cmd_get
,
1619 .dumpit
= ovs_dp_cmd_dump
1621 { .cmd
= OVS_DP_CMD_SET
,
1622 .flags
= GENL_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1623 .policy
= datapath_policy
,
1624 .doit
= ovs_dp_cmd_set
,
1628 static struct genl_family dp_datapath_genl_family
= {
1629 .id
= GENL_ID_GENERATE
,
1630 .hdrsize
= sizeof(struct ovs_header
),
1631 .name
= OVS_DATAPATH_FAMILY
,
1632 .version
= OVS_DATAPATH_VERSION
,
1633 .maxattr
= OVS_DP_ATTR_MAX
,
1635 .parallel_ops
= true,
1636 .ops
= dp_datapath_genl_ops
,
1637 .n_ops
= ARRAY_SIZE(dp_datapath_genl_ops
),
1638 .mcgrps
= &ovs_dp_datapath_multicast_group
,
1642 /* Called with ovs_mutex or RCU read lock. */
1643 static int ovs_vport_cmd_fill_info(struct vport
*vport
, struct sk_buff
*skb
,
1644 u32 portid
, u32 seq
, u32 flags
, u8 cmd
)
1646 struct ovs_header
*ovs_header
;
1647 struct ovs_vport_stats vport_stats
;
1650 ovs_header
= genlmsg_put(skb
, portid
, seq
, &dp_vport_genl_family
,
1655 ovs_header
->dp_ifindex
= get_dpifindex(vport
->dp
);
1657 if (nla_put_u32(skb
, OVS_VPORT_ATTR_PORT_NO
, vport
->port_no
) ||
1658 nla_put_u32(skb
, OVS_VPORT_ATTR_TYPE
, vport
->ops
->type
) ||
1659 nla_put_string(skb
, OVS_VPORT_ATTR_NAME
,
1660 vport
->ops
->get_name(vport
)))
1661 goto nla_put_failure
;
1663 ovs_vport_get_stats(vport
, &vport_stats
);
1664 if (nla_put(skb
, OVS_VPORT_ATTR_STATS
, sizeof(struct ovs_vport_stats
),
1666 goto nla_put_failure
;
1668 if (ovs_vport_get_upcall_portids(vport
, skb
))
1669 goto nla_put_failure
;
1671 err
= ovs_vport_get_options(vport
, skb
);
1672 if (err
== -EMSGSIZE
)
1675 return genlmsg_end(skb
, ovs_header
);
1680 genlmsg_cancel(skb
, ovs_header
);
1684 static struct sk_buff
*ovs_vport_cmd_alloc_info(void)
1686 return nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
1689 /* Called with ovs_mutex, only via ovs_dp_notify_wq(). */
1690 struct sk_buff
*ovs_vport_cmd_build_info(struct vport
*vport
, u32 portid
,
1693 struct sk_buff
*skb
;
1696 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_ATOMIC
);
1698 return ERR_PTR(-ENOMEM
);
1700 retval
= ovs_vport_cmd_fill_info(vport
, skb
, portid
, seq
, 0, cmd
);
1706 /* Called with ovs_mutex or RCU read lock. */
1707 static struct vport
*lookup_vport(struct net
*net
,
1708 struct ovs_header
*ovs_header
,
1709 struct nlattr
*a
[OVS_VPORT_ATTR_MAX
+ 1])
1711 struct datapath
*dp
;
1712 struct vport
*vport
;
1714 if (a
[OVS_VPORT_ATTR_NAME
]) {
1715 vport
= ovs_vport_locate(net
, nla_data(a
[OVS_VPORT_ATTR_NAME
]));
1717 return ERR_PTR(-ENODEV
);
1718 if (ovs_header
->dp_ifindex
&&
1719 ovs_header
->dp_ifindex
!= get_dpifindex(vport
->dp
))
1720 return ERR_PTR(-ENODEV
);
1722 } else if (a
[OVS_VPORT_ATTR_PORT_NO
]) {
1723 u32 port_no
= nla_get_u32(a
[OVS_VPORT_ATTR_PORT_NO
]);
1725 if (port_no
>= DP_MAX_PORTS
)
1726 return ERR_PTR(-EFBIG
);
1728 dp
= get_dp(net
, ovs_header
->dp_ifindex
);
1730 return ERR_PTR(-ENODEV
);
1732 vport
= ovs_vport_ovsl_rcu(dp
, port_no
);
1734 return ERR_PTR(-ENODEV
);
1737 return ERR_PTR(-EINVAL
);
1740 static int ovs_vport_cmd_new(struct sk_buff
*skb
, struct genl_info
*info
)
1742 struct nlattr
**a
= info
->attrs
;
1743 struct ovs_header
*ovs_header
= info
->userhdr
;
1744 struct vport_parms parms
;
1745 struct sk_buff
*reply
;
1746 struct vport
*vport
;
1747 struct datapath
*dp
;
1751 if (!a
[OVS_VPORT_ATTR_NAME
] || !a
[OVS_VPORT_ATTR_TYPE
] ||
1752 !a
[OVS_VPORT_ATTR_UPCALL_PID
])
1755 port_no
= a
[OVS_VPORT_ATTR_PORT_NO
]
1756 ? nla_get_u32(a
[OVS_VPORT_ATTR_PORT_NO
]) : 0;
1757 if (port_no
>= DP_MAX_PORTS
)
1760 reply
= ovs_vport_cmd_alloc_info();
1765 dp
= get_dp(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
1768 goto exit_unlock_free
;
1771 vport
= ovs_vport_ovsl(dp
, port_no
);
1774 goto exit_unlock_free
;
1776 for (port_no
= 1; ; port_no
++) {
1777 if (port_no
>= DP_MAX_PORTS
) {
1779 goto exit_unlock_free
;
1781 vport
= ovs_vport_ovsl(dp
, port_no
);
1787 parms
.name
= nla_data(a
[OVS_VPORT_ATTR_NAME
]);
1788 parms
.type
= nla_get_u32(a
[OVS_VPORT_ATTR_TYPE
]);
1789 parms
.options
= a
[OVS_VPORT_ATTR_OPTIONS
];
1791 parms
.port_no
= port_no
;
1792 parms
.upcall_portids
= a
[OVS_VPORT_ATTR_UPCALL_PID
];
1794 vport
= new_vport(&parms
);
1795 err
= PTR_ERR(vport
);
1797 goto exit_unlock_free
;
1799 err
= ovs_vport_cmd_fill_info(vport
, reply
, info
->snd_portid
,
1800 info
->snd_seq
, 0, OVS_VPORT_CMD_NEW
);
1804 ovs_notify(&dp_vport_genl_family
, reply
, info
);
1813 static int ovs_vport_cmd_set(struct sk_buff
*skb
, struct genl_info
*info
)
1815 struct nlattr
**a
= info
->attrs
;
1816 struct sk_buff
*reply
;
1817 struct vport
*vport
;
1820 reply
= ovs_vport_cmd_alloc_info();
1825 vport
= lookup_vport(sock_net(skb
->sk
), info
->userhdr
, a
);
1826 err
= PTR_ERR(vport
);
1828 goto exit_unlock_free
;
1830 if (a
[OVS_VPORT_ATTR_TYPE
] &&
1831 nla_get_u32(a
[OVS_VPORT_ATTR_TYPE
]) != vport
->ops
->type
) {
1833 goto exit_unlock_free
;
1836 if (a
[OVS_VPORT_ATTR_OPTIONS
]) {
1837 err
= ovs_vport_set_options(vport
, a
[OVS_VPORT_ATTR_OPTIONS
]);
1839 goto exit_unlock_free
;
1843 if (a
[OVS_VPORT_ATTR_UPCALL_PID
]) {
1844 struct nlattr
*ids
= a
[OVS_VPORT_ATTR_UPCALL_PID
];
1846 err
= ovs_vport_set_upcall_portids(vport
, ids
);
1848 goto exit_unlock_free
;
1851 err
= ovs_vport_cmd_fill_info(vport
, reply
, info
->snd_portid
,
1852 info
->snd_seq
, 0, OVS_VPORT_CMD_NEW
);
1856 ovs_notify(&dp_vport_genl_family
, reply
, info
);
1865 static int ovs_vport_cmd_del(struct sk_buff
*skb
, struct genl_info
*info
)
1867 struct nlattr
**a
= info
->attrs
;
1868 struct sk_buff
*reply
;
1869 struct vport
*vport
;
1872 reply
= ovs_vport_cmd_alloc_info();
1877 vport
= lookup_vport(sock_net(skb
->sk
), info
->userhdr
, a
);
1878 err
= PTR_ERR(vport
);
1880 goto exit_unlock_free
;
1882 if (vport
->port_no
== OVSP_LOCAL
) {
1884 goto exit_unlock_free
;
1887 err
= ovs_vport_cmd_fill_info(vport
, reply
, info
->snd_portid
,
1888 info
->snd_seq
, 0, OVS_VPORT_CMD_DEL
);
1890 ovs_dp_detach_port(vport
);
1893 ovs_notify(&dp_vport_genl_family
, reply
, info
);
1902 static int ovs_vport_cmd_get(struct sk_buff
*skb
, struct genl_info
*info
)
1904 struct nlattr
**a
= info
->attrs
;
1905 struct ovs_header
*ovs_header
= info
->userhdr
;
1906 struct sk_buff
*reply
;
1907 struct vport
*vport
;
1910 reply
= ovs_vport_cmd_alloc_info();
1915 vport
= lookup_vport(sock_net(skb
->sk
), ovs_header
, a
);
1916 err
= PTR_ERR(vport
);
1918 goto exit_unlock_free
;
1919 err
= ovs_vport_cmd_fill_info(vport
, reply
, info
->snd_portid
,
1920 info
->snd_seq
, 0, OVS_VPORT_CMD_NEW
);
1924 return genlmsg_reply(reply
, info
);
1932 static int ovs_vport_cmd_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1934 struct ovs_header
*ovs_header
= genlmsg_data(nlmsg_data(cb
->nlh
));
1935 struct datapath
*dp
;
1936 int bucket
= cb
->args
[0], skip
= cb
->args
[1];
1940 dp
= get_dp(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
1945 for (i
= bucket
; i
< DP_VPORT_HASH_BUCKETS
; i
++) {
1946 struct vport
*vport
;
1949 hlist_for_each_entry_rcu(vport
, &dp
->ports
[i
], dp_hash_node
) {
1951 ovs_vport_cmd_fill_info(vport
, skb
,
1952 NETLINK_CB(cb
->skb
).portid
,
1955 OVS_VPORT_CMD_NEW
) < 0)
1971 static const struct nla_policy vport_policy
[OVS_VPORT_ATTR_MAX
+ 1] = {
1972 [OVS_VPORT_ATTR_NAME
] = { .type
= NLA_NUL_STRING
, .len
= IFNAMSIZ
- 1 },
1973 [OVS_VPORT_ATTR_STATS
] = { .len
= sizeof(struct ovs_vport_stats
) },
1974 [OVS_VPORT_ATTR_PORT_NO
] = { .type
= NLA_U32
},
1975 [OVS_VPORT_ATTR_TYPE
] = { .type
= NLA_U32
},
1976 [OVS_VPORT_ATTR_UPCALL_PID
] = { .type
= NLA_U32
},
1977 [OVS_VPORT_ATTR_OPTIONS
] = { .type
= NLA_NESTED
},
1980 static const struct genl_ops dp_vport_genl_ops
[] = {
1981 { .cmd
= OVS_VPORT_CMD_NEW
,
1982 .flags
= GENL_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1983 .policy
= vport_policy
,
1984 .doit
= ovs_vport_cmd_new
1986 { .cmd
= OVS_VPORT_CMD_DEL
,
1987 .flags
= GENL_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1988 .policy
= vport_policy
,
1989 .doit
= ovs_vport_cmd_del
1991 { .cmd
= OVS_VPORT_CMD_GET
,
1992 .flags
= 0, /* OK for unprivileged users. */
1993 .policy
= vport_policy
,
1994 .doit
= ovs_vport_cmd_get
,
1995 .dumpit
= ovs_vport_cmd_dump
1997 { .cmd
= OVS_VPORT_CMD_SET
,
1998 .flags
= GENL_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1999 .policy
= vport_policy
,
2000 .doit
= ovs_vport_cmd_set
,
2004 struct genl_family dp_vport_genl_family
= {
2005 .id
= GENL_ID_GENERATE
,
2006 .hdrsize
= sizeof(struct ovs_header
),
2007 .name
= OVS_VPORT_FAMILY
,
2008 .version
= OVS_VPORT_VERSION
,
2009 .maxattr
= OVS_VPORT_ATTR_MAX
,
2011 .parallel_ops
= true,
2012 .ops
= dp_vport_genl_ops
,
2013 .n_ops
= ARRAY_SIZE(dp_vport_genl_ops
),
2014 .mcgrps
= &ovs_dp_vport_multicast_group
,
2018 static struct genl_family
* const dp_genl_families
[] = {
2019 &dp_datapath_genl_family
,
2020 &dp_vport_genl_family
,
2021 &dp_flow_genl_family
,
2022 &dp_packet_genl_family
,
2025 static void dp_unregister_genl(int n_families
)
2029 for (i
= 0; i
< n_families
; i
++)
2030 genl_unregister_family(dp_genl_families
[i
]);
2033 static int dp_register_genl(void)
2038 for (i
= 0; i
< ARRAY_SIZE(dp_genl_families
); i
++) {
2040 err
= genl_register_family(dp_genl_families
[i
]);
2048 dp_unregister_genl(i
);
2052 static int __net_init
ovs_init_net(struct net
*net
)
2054 struct ovs_net
*ovs_net
= net_generic(net
, ovs_net_id
);
2056 INIT_LIST_HEAD(&ovs_net
->dps
);
2057 INIT_WORK(&ovs_net
->dp_notify_work
, ovs_dp_notify_wq
);
2061 static void __net_exit
ovs_exit_net(struct net
*net
)
2063 struct datapath
*dp
, *dp_next
;
2064 struct ovs_net
*ovs_net
= net_generic(net
, ovs_net_id
);
2067 list_for_each_entry_safe(dp
, dp_next
, &ovs_net
->dps
, list_node
)
2071 cancel_work_sync(&ovs_net
->dp_notify_work
);
2074 static struct pernet_operations ovs_net_ops
= {
2075 .init
= ovs_init_net
,
2076 .exit
= ovs_exit_net
,
2078 .size
= sizeof(struct ovs_net
),
2081 static int __init
dp_init(void)
2085 BUILD_BUG_ON(sizeof(struct ovs_skb_cb
) > FIELD_SIZEOF(struct sk_buff
, cb
));
2087 pr_info("Open vSwitch switching datapath\n");
2089 err
= action_fifos_init();
2093 err
= ovs_internal_dev_rtnl_link_register();
2095 goto error_action_fifos_exit
;
2097 err
= ovs_flow_init();
2099 goto error_unreg_rtnl_link
;
2101 err
= ovs_vport_init();
2103 goto error_flow_exit
;
2105 err
= register_pernet_device(&ovs_net_ops
);
2107 goto error_vport_exit
;
2109 err
= register_netdevice_notifier(&ovs_dp_device_notifier
);
2111 goto error_netns_exit
;
2113 err
= dp_register_genl();
2115 goto error_unreg_notifier
;
2119 error_unreg_notifier
:
2120 unregister_netdevice_notifier(&ovs_dp_device_notifier
);
2122 unregister_pernet_device(&ovs_net_ops
);
2127 error_unreg_rtnl_link
:
2128 ovs_internal_dev_rtnl_link_unregister();
2129 error_action_fifos_exit
:
2130 action_fifos_exit();
2135 static void dp_cleanup(void)
2137 dp_unregister_genl(ARRAY_SIZE(dp_genl_families
));
2138 unregister_netdevice_notifier(&ovs_dp_device_notifier
);
2139 unregister_pernet_device(&ovs_net_ops
);
2143 ovs_internal_dev_rtnl_link_unregister();
2144 action_fifos_exit();
2147 module_init(dp_init
);
2148 module_exit(dp_cleanup
);
2150 MODULE_DESCRIPTION("Open vSwitch switching datapath");
2151 MODULE_LICENSE("GPL");