2 * Copyright (c) 2007-2012 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/workqueue.h>
51 #include <net/genetlink.h>
52 #include <net/net_namespace.h>
53 #include <net/netns/generic.h>
57 #include "vport-internal_dev.h"
60 * struct ovs_net - Per net-namespace data for ovs.
61 * @dps: List of datapaths to enable dumping them all out.
62 * Protected by genl_mutex.
68 static int ovs_net_id __read_mostly
;
70 #define REHASH_FLOW_INTERVAL (10 * 60 * HZ)
71 static void rehash_flow_table(struct work_struct
*work
);
72 static DECLARE_DELAYED_WORK(rehash_flow_wq
, rehash_flow_table
);
77 * Writes to device state (add/remove datapath, port, set operations on vports,
78 * etc.) are protected by RTNL.
80 * Writes to other state (flow table modifications, set miscellaneous datapath
81 * parameters, etc.) are protected by genl_mutex. The RTNL lock nests inside
84 * Reads are protected by RCU.
86 * There are a few special cases (mostly stats) that have their own
87 * synchronization but they nest under all of above and don't interact with
91 static struct vport
*new_vport(const struct vport_parms
*);
92 static int queue_gso_packets(struct net
*, int dp_ifindex
, struct sk_buff
*,
93 const struct dp_upcall_info
*);
94 static int queue_userspace_packet(struct net
*, int dp_ifindex
,
96 const struct dp_upcall_info
*);
98 /* Must be called with rcu_read_lock, genl_mutex, or RTNL lock. */
99 static struct datapath
*get_dp(struct net
*net
, int dp_ifindex
)
101 struct datapath
*dp
= NULL
;
102 struct net_device
*dev
;
105 dev
= dev_get_by_index_rcu(net
, dp_ifindex
);
107 struct vport
*vport
= ovs_internal_dev_get_vport(dev
);
116 /* Must be called with rcu_read_lock or RTNL lock. */
117 const char *ovs_dp_name(const struct datapath
*dp
)
119 struct vport
*vport
= ovs_vport_rtnl_rcu(dp
, OVSP_LOCAL
);
120 return vport
->ops
->get_name(vport
);
123 static int get_dpifindex(struct datapath
*dp
)
130 local
= ovs_vport_rcu(dp
, OVSP_LOCAL
);
132 ifindex
= local
->ops
->get_ifindex(local
);
141 static void destroy_dp_rcu(struct rcu_head
*rcu
)
143 struct datapath
*dp
= container_of(rcu
, struct datapath
, rcu
);
145 ovs_flow_tbl_destroy((__force
struct flow_table
*)dp
->table
);
146 free_percpu(dp
->stats_percpu
);
147 release_net(ovs_dp_get_net(dp
));
152 static struct hlist_head
*vport_hash_bucket(const struct datapath
*dp
,
155 return &dp
->ports
[port_no
& (DP_VPORT_HASH_BUCKETS
- 1)];
158 struct vport
*ovs_lookup_vport(const struct datapath
*dp
, u16 port_no
)
161 struct hlist_head
*head
;
163 head
= vport_hash_bucket(dp
, port_no
);
164 hlist_for_each_entry_rcu(vport
, head
, dp_hash_node
) {
165 if (vport
->port_no
== port_no
)
171 /* Called with RTNL lock and genl_lock. */
172 static struct vport
*new_vport(const struct vport_parms
*parms
)
176 vport
= ovs_vport_add(parms
);
177 if (!IS_ERR(vport
)) {
178 struct datapath
*dp
= parms
->dp
;
179 struct hlist_head
*head
= vport_hash_bucket(dp
, vport
->port_no
);
181 hlist_add_head_rcu(&vport
->dp_hash_node
, head
);
187 /* Called with RTNL lock. */
188 void ovs_dp_detach_port(struct vport
*p
)
192 /* First drop references to device. */
193 hlist_del_rcu(&p
->dp_hash_node
);
195 /* Then destroy it. */
199 /* Must be called with rcu_read_lock. */
200 void ovs_dp_process_received_packet(struct vport
*p
, struct sk_buff
*skb
)
202 struct datapath
*dp
= p
->dp
;
203 struct sw_flow
*flow
;
204 struct dp_stats_percpu
*stats
;
205 struct sw_flow_key key
;
210 stats
= this_cpu_ptr(dp
->stats_percpu
);
212 /* Extract flow from 'skb' into 'key'. */
213 error
= ovs_flow_extract(skb
, p
->port_no
, &key
, &key_len
);
214 if (unlikely(error
)) {
220 flow
= ovs_flow_tbl_lookup(rcu_dereference(dp
->table
), &key
, key_len
);
221 if (unlikely(!flow
)) {
222 struct dp_upcall_info upcall
;
224 upcall
.cmd
= OVS_PACKET_CMD_MISS
;
226 upcall
.userdata
= NULL
;
227 upcall
.portid
= p
->upcall_portid
;
228 ovs_dp_upcall(dp
, skb
, &upcall
);
230 stats_counter
= &stats
->n_missed
;
234 OVS_CB(skb
)->flow
= flow
;
236 stats_counter
= &stats
->n_hit
;
237 ovs_flow_used(OVS_CB(skb
)->flow
, skb
);
238 ovs_execute_actions(dp
, skb
);
241 /* Update datapath statistics. */
242 u64_stats_update_begin(&stats
->sync
);
244 u64_stats_update_end(&stats
->sync
);
247 static struct genl_family dp_packet_genl_family
= {
248 .id
= GENL_ID_GENERATE
,
249 .hdrsize
= sizeof(struct ovs_header
),
250 .name
= OVS_PACKET_FAMILY
,
251 .version
= OVS_PACKET_VERSION
,
252 .maxattr
= OVS_PACKET_ATTR_MAX
,
256 int ovs_dp_upcall(struct datapath
*dp
, struct sk_buff
*skb
,
257 const struct dp_upcall_info
*upcall_info
)
259 struct dp_stats_percpu
*stats
;
263 if (upcall_info
->portid
== 0) {
268 dp_ifindex
= get_dpifindex(dp
);
274 if (!skb_is_gso(skb
))
275 err
= queue_userspace_packet(ovs_dp_get_net(dp
), dp_ifindex
, skb
, upcall_info
);
277 err
= queue_gso_packets(ovs_dp_get_net(dp
), dp_ifindex
, skb
, upcall_info
);
284 stats
= this_cpu_ptr(dp
->stats_percpu
);
286 u64_stats_update_begin(&stats
->sync
);
288 u64_stats_update_end(&stats
->sync
);
293 static int queue_gso_packets(struct net
*net
, int dp_ifindex
,
295 const struct dp_upcall_info
*upcall_info
)
297 unsigned short gso_type
= skb_shinfo(skb
)->gso_type
;
298 struct dp_upcall_info later_info
;
299 struct sw_flow_key later_key
;
300 struct sk_buff
*segs
, *nskb
;
303 segs
= __skb_gso_segment(skb
, NETIF_F_SG
| NETIF_F_HW_CSUM
, false);
305 return PTR_ERR(segs
);
307 /* Queue all of the segments. */
310 err
= queue_userspace_packet(net
, dp_ifindex
, skb
, upcall_info
);
314 if (skb
== segs
&& gso_type
& SKB_GSO_UDP
) {
315 /* The initial flow key extracted by ovs_flow_extract()
316 * in this case is for a first fragment, so we need to
317 * properly mark later fragments.
319 later_key
= *upcall_info
->key
;
320 later_key
.ip
.frag
= OVS_FRAG_TYPE_LATER
;
322 later_info
= *upcall_info
;
323 later_info
.key
= &later_key
;
324 upcall_info
= &later_info
;
326 } while ((skb
= skb
->next
));
328 /* Free all of the segments. */
336 } while ((skb
= nskb
));
340 static int queue_userspace_packet(struct net
*net
, int dp_ifindex
,
342 const struct dp_upcall_info
*upcall_info
)
344 struct ovs_header
*upcall
;
345 struct sk_buff
*nskb
= NULL
;
346 struct sk_buff
*user_skb
; /* to be queued to userspace */
351 if (vlan_tx_tag_present(skb
)) {
352 nskb
= skb_clone(skb
, GFP_ATOMIC
);
356 nskb
= __vlan_put_tag(nskb
, vlan_tx_tag_get(nskb
));
364 if (nla_attr_size(skb
->len
) > USHRT_MAX
) {
369 len
= sizeof(struct ovs_header
);
370 len
+= nla_total_size(skb
->len
);
371 len
+= nla_total_size(FLOW_BUFSIZE
);
372 if (upcall_info
->userdata
)
373 len
+= NLA_ALIGN(upcall_info
->userdata
->nla_len
);
375 user_skb
= genlmsg_new(len
, GFP_ATOMIC
);
381 upcall
= genlmsg_put(user_skb
, 0, 0, &dp_packet_genl_family
,
382 0, upcall_info
->cmd
);
383 upcall
->dp_ifindex
= dp_ifindex
;
385 nla
= nla_nest_start(user_skb
, OVS_PACKET_ATTR_KEY
);
386 ovs_flow_to_nlattrs(upcall_info
->key
, user_skb
);
387 nla_nest_end(user_skb
, nla
);
389 if (upcall_info
->userdata
)
390 __nla_put(user_skb
, OVS_PACKET_ATTR_USERDATA
,
391 nla_len(upcall_info
->userdata
),
392 nla_data(upcall_info
->userdata
));
394 nla
= __nla_reserve(user_skb
, OVS_PACKET_ATTR_PACKET
, skb
->len
);
396 skb_copy_and_csum_dev(skb
, nla_data(nla
));
398 genlmsg_end(user_skb
, upcall
);
399 err
= genlmsg_unicast(net
, user_skb
, upcall_info
->portid
);
406 /* Called with genl_mutex. */
407 static int flush_flows(struct datapath
*dp
)
409 struct flow_table
*old_table
;
410 struct flow_table
*new_table
;
412 old_table
= genl_dereference(dp
->table
);
413 new_table
= ovs_flow_tbl_alloc(TBL_MIN_BUCKETS
);
417 rcu_assign_pointer(dp
->table
, new_table
);
419 ovs_flow_tbl_deferred_destroy(old_table
);
423 static int validate_actions(const struct nlattr
*attr
,
424 const struct sw_flow_key
*key
, int depth
);
426 static int validate_sample(const struct nlattr
*attr
,
427 const struct sw_flow_key
*key
, int depth
)
429 const struct nlattr
*attrs
[OVS_SAMPLE_ATTR_MAX
+ 1];
430 const struct nlattr
*probability
, *actions
;
431 const struct nlattr
*a
;
434 memset(attrs
, 0, sizeof(attrs
));
435 nla_for_each_nested(a
, attr
, rem
) {
436 int type
= nla_type(a
);
437 if (!type
|| type
> OVS_SAMPLE_ATTR_MAX
|| attrs
[type
])
444 probability
= attrs
[OVS_SAMPLE_ATTR_PROBABILITY
];
445 if (!probability
|| nla_len(probability
) != sizeof(u32
))
448 actions
= attrs
[OVS_SAMPLE_ATTR_ACTIONS
];
449 if (!actions
|| (nla_len(actions
) && nla_len(actions
) < NLA_HDRLEN
))
451 return validate_actions(actions
, key
, depth
+ 1);
454 static int validate_tp_port(const struct sw_flow_key
*flow_key
)
456 if (flow_key
->eth
.type
== htons(ETH_P_IP
)) {
457 if (flow_key
->ipv4
.tp
.src
|| flow_key
->ipv4
.tp
.dst
)
459 } else if (flow_key
->eth
.type
== htons(ETH_P_IPV6
)) {
460 if (flow_key
->ipv6
.tp
.src
|| flow_key
->ipv6
.tp
.dst
)
467 static int validate_set(const struct nlattr
*a
,
468 const struct sw_flow_key
*flow_key
)
470 const struct nlattr
*ovs_key
= nla_data(a
);
471 int key_type
= nla_type(ovs_key
);
473 /* There can be only one key in a action */
474 if (nla_total_size(nla_len(ovs_key
)) != nla_len(a
))
477 if (key_type
> OVS_KEY_ATTR_MAX
||
478 nla_len(ovs_key
) != ovs_key_lens
[key_type
])
482 const struct ovs_key_ipv4
*ipv4_key
;
483 const struct ovs_key_ipv6
*ipv6_key
;
485 case OVS_KEY_ATTR_PRIORITY
:
486 case OVS_KEY_ATTR_SKB_MARK
:
487 case OVS_KEY_ATTR_ETHERNET
:
490 case OVS_KEY_ATTR_IPV4
:
491 if (flow_key
->eth
.type
!= htons(ETH_P_IP
))
494 if (!flow_key
->ip
.proto
)
497 ipv4_key
= nla_data(ovs_key
);
498 if (ipv4_key
->ipv4_proto
!= flow_key
->ip
.proto
)
501 if (ipv4_key
->ipv4_frag
!= flow_key
->ip
.frag
)
506 case OVS_KEY_ATTR_IPV6
:
507 if (flow_key
->eth
.type
!= htons(ETH_P_IPV6
))
510 if (!flow_key
->ip
.proto
)
513 ipv6_key
= nla_data(ovs_key
);
514 if (ipv6_key
->ipv6_proto
!= flow_key
->ip
.proto
)
517 if (ipv6_key
->ipv6_frag
!= flow_key
->ip
.frag
)
520 if (ntohl(ipv6_key
->ipv6_label
) & 0xFFF00000)
525 case OVS_KEY_ATTR_TCP
:
526 if (flow_key
->ip
.proto
!= IPPROTO_TCP
)
529 return validate_tp_port(flow_key
);
531 case OVS_KEY_ATTR_UDP
:
532 if (flow_key
->ip
.proto
!= IPPROTO_UDP
)
535 return validate_tp_port(flow_key
);
544 static int validate_userspace(const struct nlattr
*attr
)
546 static const struct nla_policy userspace_policy
[OVS_USERSPACE_ATTR_MAX
+ 1] = {
547 [OVS_USERSPACE_ATTR_PID
] = {.type
= NLA_U32
},
548 [OVS_USERSPACE_ATTR_USERDATA
] = {.type
= NLA_UNSPEC
},
550 struct nlattr
*a
[OVS_USERSPACE_ATTR_MAX
+ 1];
553 error
= nla_parse_nested(a
, OVS_USERSPACE_ATTR_MAX
,
554 attr
, userspace_policy
);
558 if (!a
[OVS_USERSPACE_ATTR_PID
] ||
559 !nla_get_u32(a
[OVS_USERSPACE_ATTR_PID
]))
565 static int validate_actions(const struct nlattr
*attr
,
566 const struct sw_flow_key
*key
, int depth
)
568 const struct nlattr
*a
;
571 if (depth
>= SAMPLE_ACTION_DEPTH
)
574 nla_for_each_nested(a
, attr
, rem
) {
575 /* Expected argument lengths, (u32)-1 for variable length. */
576 static const u32 action_lens
[OVS_ACTION_ATTR_MAX
+ 1] = {
577 [OVS_ACTION_ATTR_OUTPUT
] = sizeof(u32
),
578 [OVS_ACTION_ATTR_USERSPACE
] = (u32
)-1,
579 [OVS_ACTION_ATTR_PUSH_VLAN
] = sizeof(struct ovs_action_push_vlan
),
580 [OVS_ACTION_ATTR_POP_VLAN
] = 0,
581 [OVS_ACTION_ATTR_SET
] = (u32
)-1,
582 [OVS_ACTION_ATTR_SAMPLE
] = (u32
)-1
584 const struct ovs_action_push_vlan
*vlan
;
585 int type
= nla_type(a
);
587 if (type
> OVS_ACTION_ATTR_MAX
||
588 (action_lens
[type
] != nla_len(a
) &&
589 action_lens
[type
] != (u32
)-1))
593 case OVS_ACTION_ATTR_UNSPEC
:
596 case OVS_ACTION_ATTR_USERSPACE
:
597 err
= validate_userspace(a
);
602 case OVS_ACTION_ATTR_OUTPUT
:
603 if (nla_get_u32(a
) >= DP_MAX_PORTS
)
608 case OVS_ACTION_ATTR_POP_VLAN
:
611 case OVS_ACTION_ATTR_PUSH_VLAN
:
613 if (vlan
->vlan_tpid
!= htons(ETH_P_8021Q
))
615 if (!(vlan
->vlan_tci
& htons(VLAN_TAG_PRESENT
)))
619 case OVS_ACTION_ATTR_SET
:
620 err
= validate_set(a
, key
);
625 case OVS_ACTION_ATTR_SAMPLE
:
626 err
= validate_sample(a
, key
, depth
);
642 static void clear_stats(struct sw_flow
*flow
)
646 flow
->packet_count
= 0;
647 flow
->byte_count
= 0;
650 static int ovs_packet_cmd_execute(struct sk_buff
*skb
, struct genl_info
*info
)
652 struct ovs_header
*ovs_header
= info
->userhdr
;
653 struct nlattr
**a
= info
->attrs
;
654 struct sw_flow_actions
*acts
;
655 struct sk_buff
*packet
;
656 struct sw_flow
*flow
;
664 if (!a
[OVS_PACKET_ATTR_PACKET
] || !a
[OVS_PACKET_ATTR_KEY
] ||
665 !a
[OVS_PACKET_ATTR_ACTIONS
] ||
666 nla_len(a
[OVS_PACKET_ATTR_PACKET
]) < ETH_HLEN
)
669 len
= nla_len(a
[OVS_PACKET_ATTR_PACKET
]);
670 packet
= __dev_alloc_skb(NET_IP_ALIGN
+ len
, GFP_KERNEL
);
674 skb_reserve(packet
, NET_IP_ALIGN
);
676 memcpy(__skb_put(packet
, len
), nla_data(a
[OVS_PACKET_ATTR_PACKET
]), len
);
678 skb_reset_mac_header(packet
);
679 eth
= eth_hdr(packet
);
681 /* Normally, setting the skb 'protocol' field would be handled by a
682 * call to eth_type_trans(), but it assumes there's a sending
683 * device, which we may not have. */
684 if (ntohs(eth
->h_proto
) >= 1536)
685 packet
->protocol
= eth
->h_proto
;
687 packet
->protocol
= htons(ETH_P_802_2
);
689 /* Build an sw_flow for sending this packet. */
690 flow
= ovs_flow_alloc();
695 err
= ovs_flow_extract(packet
, -1, &flow
->key
, &key_len
);
699 err
= ovs_flow_metadata_from_nlattrs(&flow
->key
.phy
.priority
,
700 &flow
->key
.phy
.skb_mark
,
701 &flow
->key
.phy
.in_port
,
702 a
[OVS_PACKET_ATTR_KEY
]);
706 err
= validate_actions(a
[OVS_PACKET_ATTR_ACTIONS
], &flow
->key
, 0);
710 flow
->hash
= ovs_flow_hash(&flow
->key
, key_len
);
712 acts
= ovs_flow_actions_alloc(a
[OVS_PACKET_ATTR_ACTIONS
]);
716 rcu_assign_pointer(flow
->sf_acts
, acts
);
718 OVS_CB(packet
)->flow
= flow
;
719 packet
->priority
= flow
->key
.phy
.priority
;
720 packet
->mark
= flow
->key
.phy
.skb_mark
;
723 dp
= get_dp(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
729 err
= ovs_execute_actions(dp
, packet
);
746 static const struct nla_policy packet_policy
[OVS_PACKET_ATTR_MAX
+ 1] = {
747 [OVS_PACKET_ATTR_PACKET
] = { .type
= NLA_UNSPEC
},
748 [OVS_PACKET_ATTR_KEY
] = { .type
= NLA_NESTED
},
749 [OVS_PACKET_ATTR_ACTIONS
] = { .type
= NLA_NESTED
},
752 static struct genl_ops dp_packet_genl_ops
[] = {
753 { .cmd
= OVS_PACKET_CMD_EXECUTE
,
754 .flags
= GENL_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
755 .policy
= packet_policy
,
756 .doit
= ovs_packet_cmd_execute
760 static void get_dp_stats(struct datapath
*dp
, struct ovs_dp_stats
*stats
)
763 struct flow_table
*table
= genl_dereference(dp
->table
);
765 stats
->n_flows
= ovs_flow_tbl_count(table
);
767 stats
->n_hit
= stats
->n_missed
= stats
->n_lost
= 0;
768 for_each_possible_cpu(i
) {
769 const struct dp_stats_percpu
*percpu_stats
;
770 struct dp_stats_percpu local_stats
;
773 percpu_stats
= per_cpu_ptr(dp
->stats_percpu
, i
);
776 start
= u64_stats_fetch_begin_bh(&percpu_stats
->sync
);
777 local_stats
= *percpu_stats
;
778 } while (u64_stats_fetch_retry_bh(&percpu_stats
->sync
, start
));
780 stats
->n_hit
+= local_stats
.n_hit
;
781 stats
->n_missed
+= local_stats
.n_missed
;
782 stats
->n_lost
+= local_stats
.n_lost
;
786 static const struct nla_policy flow_policy
[OVS_FLOW_ATTR_MAX
+ 1] = {
787 [OVS_FLOW_ATTR_KEY
] = { .type
= NLA_NESTED
},
788 [OVS_FLOW_ATTR_ACTIONS
] = { .type
= NLA_NESTED
},
789 [OVS_FLOW_ATTR_CLEAR
] = { .type
= NLA_FLAG
},
792 static struct genl_family dp_flow_genl_family
= {
793 .id
= GENL_ID_GENERATE
,
794 .hdrsize
= sizeof(struct ovs_header
),
795 .name
= OVS_FLOW_FAMILY
,
796 .version
= OVS_FLOW_VERSION
,
797 .maxattr
= OVS_FLOW_ATTR_MAX
,
801 static struct genl_multicast_group ovs_dp_flow_multicast_group
= {
802 .name
= OVS_FLOW_MCGROUP
805 /* Called with genl_lock. */
806 static int ovs_flow_cmd_fill_info(struct sw_flow
*flow
, struct datapath
*dp
,
807 struct sk_buff
*skb
, u32 portid
,
808 u32 seq
, u32 flags
, u8 cmd
)
810 const int skb_orig_len
= skb
->len
;
811 const struct sw_flow_actions
*sf_acts
;
812 struct ovs_flow_stats stats
;
813 struct ovs_header
*ovs_header
;
819 sf_acts
= rcu_dereference_protected(flow
->sf_acts
,
820 lockdep_genl_is_held());
822 ovs_header
= genlmsg_put(skb
, portid
, seq
, &dp_flow_genl_family
, flags
, cmd
);
826 ovs_header
->dp_ifindex
= get_dpifindex(dp
);
828 nla
= nla_nest_start(skb
, OVS_FLOW_ATTR_KEY
);
830 goto nla_put_failure
;
831 err
= ovs_flow_to_nlattrs(&flow
->key
, skb
);
834 nla_nest_end(skb
, nla
);
836 spin_lock_bh(&flow
->lock
);
838 stats
.n_packets
= flow
->packet_count
;
839 stats
.n_bytes
= flow
->byte_count
;
840 tcp_flags
= flow
->tcp_flags
;
841 spin_unlock_bh(&flow
->lock
);
844 nla_put_u64(skb
, OVS_FLOW_ATTR_USED
, ovs_flow_used_time(used
)))
845 goto nla_put_failure
;
847 if (stats
.n_packets
&&
848 nla_put(skb
, OVS_FLOW_ATTR_STATS
,
849 sizeof(struct ovs_flow_stats
), &stats
))
850 goto nla_put_failure
;
853 nla_put_u8(skb
, OVS_FLOW_ATTR_TCP_FLAGS
, tcp_flags
))
854 goto nla_put_failure
;
856 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
857 * this is the first flow to be dumped into 'skb'. This is unusual for
858 * Netlink but individual action lists can be longer than
859 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
860 * The userspace caller can always fetch the actions separately if it
861 * really wants them. (Most userspace callers in fact don't care.)
863 * This can only fail for dump operations because the skb is always
864 * properly sized for single flows.
866 err
= nla_put(skb
, OVS_FLOW_ATTR_ACTIONS
, sf_acts
->actions_len
,
868 if (err
< 0 && skb_orig_len
)
871 return genlmsg_end(skb
, ovs_header
);
876 genlmsg_cancel(skb
, ovs_header
);
880 static struct sk_buff
*ovs_flow_cmd_alloc_info(struct sw_flow
*flow
)
882 const struct sw_flow_actions
*sf_acts
;
885 sf_acts
= rcu_dereference_protected(flow
->sf_acts
,
886 lockdep_genl_is_held());
888 /* OVS_FLOW_ATTR_KEY */
889 len
= nla_total_size(FLOW_BUFSIZE
);
890 /* OVS_FLOW_ATTR_ACTIONS */
891 len
+= nla_total_size(sf_acts
->actions_len
);
892 /* OVS_FLOW_ATTR_STATS */
893 len
+= nla_total_size(sizeof(struct ovs_flow_stats
));
894 /* OVS_FLOW_ATTR_TCP_FLAGS */
895 len
+= nla_total_size(1);
896 /* OVS_FLOW_ATTR_USED */
897 len
+= nla_total_size(8);
899 len
+= NLMSG_ALIGN(sizeof(struct ovs_header
));
901 return genlmsg_new(len
, GFP_KERNEL
);
904 static struct sk_buff
*ovs_flow_cmd_build_info(struct sw_flow
*flow
,
906 u32 portid
, u32 seq
, u8 cmd
)
911 skb
= ovs_flow_cmd_alloc_info(flow
);
913 return ERR_PTR(-ENOMEM
);
915 retval
= ovs_flow_cmd_fill_info(flow
, dp
, skb
, portid
, seq
, 0, cmd
);
920 static int ovs_flow_cmd_new_or_set(struct sk_buff
*skb
, struct genl_info
*info
)
922 struct nlattr
**a
= info
->attrs
;
923 struct ovs_header
*ovs_header
= info
->userhdr
;
924 struct sw_flow_key key
;
925 struct sw_flow
*flow
;
926 struct sk_buff
*reply
;
928 struct flow_table
*table
;
934 if (!a
[OVS_FLOW_ATTR_KEY
])
936 error
= ovs_flow_from_nlattrs(&key
, &key_len
, a
[OVS_FLOW_ATTR_KEY
]);
940 /* Validate actions. */
941 if (a
[OVS_FLOW_ATTR_ACTIONS
]) {
942 error
= validate_actions(a
[OVS_FLOW_ATTR_ACTIONS
], &key
, 0);
945 } else if (info
->genlhdr
->cmd
== OVS_FLOW_CMD_NEW
) {
950 dp
= get_dp(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
955 table
= genl_dereference(dp
->table
);
956 flow
= ovs_flow_tbl_lookup(table
, &key
, key_len
);
958 struct sw_flow_actions
*acts
;
960 /* Bail out if we're not allowed to create a new flow. */
962 if (info
->genlhdr
->cmd
== OVS_FLOW_CMD_SET
)
965 /* Expand table, if necessary, to make room. */
966 if (ovs_flow_tbl_need_to_expand(table
)) {
967 struct flow_table
*new_table
;
969 new_table
= ovs_flow_tbl_expand(table
);
970 if (!IS_ERR(new_table
)) {
971 rcu_assign_pointer(dp
->table
, new_table
);
972 ovs_flow_tbl_deferred_destroy(table
);
973 table
= genl_dereference(dp
->table
);
978 flow
= ovs_flow_alloc();
980 error
= PTR_ERR(flow
);
986 /* Obtain actions. */
987 acts
= ovs_flow_actions_alloc(a
[OVS_FLOW_ATTR_ACTIONS
]);
988 error
= PTR_ERR(acts
);
990 goto error_free_flow
;
991 rcu_assign_pointer(flow
->sf_acts
, acts
);
993 /* Put flow in bucket. */
994 flow
->hash
= ovs_flow_hash(&key
, key_len
);
995 ovs_flow_tbl_insert(table
, flow
);
997 reply
= ovs_flow_cmd_build_info(flow
, dp
, info
->snd_portid
,
1001 /* We found a matching flow. */
1002 struct sw_flow_actions
*old_acts
;
1003 struct nlattr
*acts_attrs
;
1005 /* Bail out if we're not allowed to modify an existing flow.
1006 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
1007 * because Generic Netlink treats the latter as a dump
1008 * request. We also accept NLM_F_EXCL in case that bug ever
1012 if (info
->genlhdr
->cmd
== OVS_FLOW_CMD_NEW
&&
1013 info
->nlhdr
->nlmsg_flags
& (NLM_F_CREATE
| NLM_F_EXCL
))
1016 /* Update actions. */
1017 old_acts
= rcu_dereference_protected(flow
->sf_acts
,
1018 lockdep_genl_is_held());
1019 acts_attrs
= a
[OVS_FLOW_ATTR_ACTIONS
];
1021 (old_acts
->actions_len
!= nla_len(acts_attrs
) ||
1022 memcmp(old_acts
->actions
, nla_data(acts_attrs
),
1023 old_acts
->actions_len
))) {
1024 struct sw_flow_actions
*new_acts
;
1026 new_acts
= ovs_flow_actions_alloc(acts_attrs
);
1027 error
= PTR_ERR(new_acts
);
1028 if (IS_ERR(new_acts
))
1031 rcu_assign_pointer(flow
->sf_acts
, new_acts
);
1032 ovs_flow_deferred_free_acts(old_acts
);
1035 reply
= ovs_flow_cmd_build_info(flow
, dp
, info
->snd_portid
,
1036 info
->snd_seq
, OVS_FLOW_CMD_NEW
);
1039 if (a
[OVS_FLOW_ATTR_CLEAR
]) {
1040 spin_lock_bh(&flow
->lock
);
1042 spin_unlock_bh(&flow
->lock
);
1047 genl_notify(reply
, genl_info_net(info
), info
->snd_portid
,
1048 ovs_dp_flow_multicast_group
.id
, info
->nlhdr
,
1051 netlink_set_err(sock_net(skb
->sk
)->genl_sock
, 0,
1052 ovs_dp_flow_multicast_group
.id
, PTR_ERR(reply
));
1056 ovs_flow_free(flow
);
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 flow_table
*table
;
1073 if (!a
[OVS_FLOW_ATTR_KEY
])
1075 err
= ovs_flow_from_nlattrs(&key
, &key_len
, a
[OVS_FLOW_ATTR_KEY
]);
1079 dp
= get_dp(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
1083 table
= genl_dereference(dp
->table
);
1084 flow
= ovs_flow_tbl_lookup(table
, &key
, key_len
);
1088 reply
= ovs_flow_cmd_build_info(flow
, dp
, info
->snd_portid
,
1089 info
->snd_seq
, OVS_FLOW_CMD_NEW
);
1091 return PTR_ERR(reply
);
1093 return genlmsg_reply(reply
, info
);
1096 static int ovs_flow_cmd_del(struct sk_buff
*skb
, struct genl_info
*info
)
1098 struct nlattr
**a
= info
->attrs
;
1099 struct ovs_header
*ovs_header
= info
->userhdr
;
1100 struct sw_flow_key key
;
1101 struct sk_buff
*reply
;
1102 struct sw_flow
*flow
;
1103 struct datapath
*dp
;
1104 struct flow_table
*table
;
1108 dp
= get_dp(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
1112 if (!a
[OVS_FLOW_ATTR_KEY
])
1113 return flush_flows(dp
);
1115 err
= ovs_flow_from_nlattrs(&key
, &key_len
, a
[OVS_FLOW_ATTR_KEY
]);
1119 table
= genl_dereference(dp
->table
);
1120 flow
= ovs_flow_tbl_lookup(table
, &key
, key_len
);
1124 reply
= ovs_flow_cmd_alloc_info(flow
);
1128 ovs_flow_tbl_remove(table
, flow
);
1130 err
= ovs_flow_cmd_fill_info(flow
, dp
, reply
, info
->snd_portid
,
1131 info
->snd_seq
, 0, OVS_FLOW_CMD_DEL
);
1134 ovs_flow_deferred_free(flow
);
1136 genl_notify(reply
, genl_info_net(info
), info
->snd_portid
,
1137 ovs_dp_flow_multicast_group
.id
, info
->nlhdr
, GFP_KERNEL
);
1141 static int ovs_flow_cmd_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1143 struct ovs_header
*ovs_header
= genlmsg_data(nlmsg_data(cb
->nlh
));
1144 struct datapath
*dp
;
1145 struct flow_table
*table
;
1147 dp
= get_dp(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
1151 table
= genl_dereference(dp
->table
);
1154 struct sw_flow
*flow
;
1157 bucket
= cb
->args
[0];
1159 flow
= ovs_flow_tbl_next(table
, &bucket
, &obj
);
1163 if (ovs_flow_cmd_fill_info(flow
, dp
, skb
,
1164 NETLINK_CB(cb
->skb
).portid
,
1165 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
,
1166 OVS_FLOW_CMD_NEW
) < 0)
1169 cb
->args
[0] = bucket
;
1175 static struct genl_ops dp_flow_genl_ops
[] = {
1176 { .cmd
= OVS_FLOW_CMD_NEW
,
1177 .flags
= GENL_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1178 .policy
= flow_policy
,
1179 .doit
= ovs_flow_cmd_new_or_set
1181 { .cmd
= OVS_FLOW_CMD_DEL
,
1182 .flags
= GENL_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1183 .policy
= flow_policy
,
1184 .doit
= ovs_flow_cmd_del
1186 { .cmd
= OVS_FLOW_CMD_GET
,
1187 .flags
= 0, /* OK for unprivileged users. */
1188 .policy
= flow_policy
,
1189 .doit
= ovs_flow_cmd_get
,
1190 .dumpit
= ovs_flow_cmd_dump
1192 { .cmd
= OVS_FLOW_CMD_SET
,
1193 .flags
= GENL_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1194 .policy
= flow_policy
,
1195 .doit
= ovs_flow_cmd_new_or_set
,
1199 static const struct nla_policy datapath_policy
[OVS_DP_ATTR_MAX
+ 1] = {
1200 [OVS_DP_ATTR_NAME
] = { .type
= NLA_NUL_STRING
, .len
= IFNAMSIZ
- 1 },
1201 [OVS_DP_ATTR_UPCALL_PID
] = { .type
= NLA_U32
},
1204 static struct genl_family dp_datapath_genl_family
= {
1205 .id
= GENL_ID_GENERATE
,
1206 .hdrsize
= sizeof(struct ovs_header
),
1207 .name
= OVS_DATAPATH_FAMILY
,
1208 .version
= OVS_DATAPATH_VERSION
,
1209 .maxattr
= OVS_DP_ATTR_MAX
,
1213 static struct genl_multicast_group ovs_dp_datapath_multicast_group
= {
1214 .name
= OVS_DATAPATH_MCGROUP
1217 static int ovs_dp_cmd_fill_info(struct datapath
*dp
, struct sk_buff
*skb
,
1218 u32 portid
, u32 seq
, u32 flags
, u8 cmd
)
1220 struct ovs_header
*ovs_header
;
1221 struct ovs_dp_stats dp_stats
;
1224 ovs_header
= genlmsg_put(skb
, portid
, seq
, &dp_datapath_genl_family
,
1229 ovs_header
->dp_ifindex
= get_dpifindex(dp
);
1232 err
= nla_put_string(skb
, OVS_DP_ATTR_NAME
, ovs_dp_name(dp
));
1235 goto nla_put_failure
;
1237 get_dp_stats(dp
, &dp_stats
);
1238 if (nla_put(skb
, OVS_DP_ATTR_STATS
, sizeof(struct ovs_dp_stats
), &dp_stats
))
1239 goto nla_put_failure
;
1241 return genlmsg_end(skb
, ovs_header
);
1244 genlmsg_cancel(skb
, ovs_header
);
1249 static struct sk_buff
*ovs_dp_cmd_build_info(struct datapath
*dp
, u32 portid
,
1252 struct sk_buff
*skb
;
1255 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
1257 return ERR_PTR(-ENOMEM
);
1259 retval
= ovs_dp_cmd_fill_info(dp
, skb
, portid
, seq
, 0, cmd
);
1262 return ERR_PTR(retval
);
1267 /* Called with genl_mutex and optionally with RTNL lock also. */
1268 static struct datapath
*lookup_datapath(struct net
*net
,
1269 struct ovs_header
*ovs_header
,
1270 struct nlattr
*a
[OVS_DP_ATTR_MAX
+ 1])
1272 struct datapath
*dp
;
1274 if (!a
[OVS_DP_ATTR_NAME
])
1275 dp
= get_dp(net
, ovs_header
->dp_ifindex
);
1277 struct vport
*vport
;
1280 vport
= ovs_vport_locate(net
, nla_data(a
[OVS_DP_ATTR_NAME
]));
1281 dp
= vport
&& vport
->port_no
== OVSP_LOCAL
? vport
->dp
: NULL
;
1284 return dp
? dp
: ERR_PTR(-ENODEV
);
1287 static int ovs_dp_cmd_new(struct sk_buff
*skb
, struct genl_info
*info
)
1289 struct nlattr
**a
= info
->attrs
;
1290 struct vport_parms parms
;
1291 struct sk_buff
*reply
;
1292 struct datapath
*dp
;
1293 struct vport
*vport
;
1294 struct ovs_net
*ovs_net
;
1298 if (!a
[OVS_DP_ATTR_NAME
] || !a
[OVS_DP_ATTR_UPCALL_PID
])
1304 dp
= kzalloc(sizeof(*dp
), GFP_KERNEL
);
1306 goto err_unlock_rtnl
;
1308 ovs_dp_set_net(dp
, hold_net(sock_net(skb
->sk
)));
1310 /* Allocate table. */
1312 rcu_assign_pointer(dp
->table
, ovs_flow_tbl_alloc(TBL_MIN_BUCKETS
));
1316 dp
->stats_percpu
= alloc_percpu(struct dp_stats_percpu
);
1317 if (!dp
->stats_percpu
) {
1319 goto err_destroy_table
;
1322 dp
->ports
= kmalloc(DP_VPORT_HASH_BUCKETS
* sizeof(struct hlist_head
),
1326 goto err_destroy_percpu
;
1329 for (i
= 0; i
< DP_VPORT_HASH_BUCKETS
; i
++)
1330 INIT_HLIST_HEAD(&dp
->ports
[i
]);
1332 /* Set up our datapath device. */
1333 parms
.name
= nla_data(a
[OVS_DP_ATTR_NAME
]);
1334 parms
.type
= OVS_VPORT_TYPE_INTERNAL
;
1335 parms
.options
= NULL
;
1337 parms
.port_no
= OVSP_LOCAL
;
1338 parms
.upcall_portid
= nla_get_u32(a
[OVS_DP_ATTR_UPCALL_PID
]);
1340 vport
= new_vport(&parms
);
1341 if (IS_ERR(vport
)) {
1342 err
= PTR_ERR(vport
);
1346 goto err_destroy_ports_array
;
1349 reply
= ovs_dp_cmd_build_info(dp
, info
->snd_portid
,
1350 info
->snd_seq
, OVS_DP_CMD_NEW
);
1351 err
= PTR_ERR(reply
);
1353 goto err_destroy_local_port
;
1355 ovs_net
= net_generic(ovs_dp_get_net(dp
), ovs_net_id
);
1356 list_add_tail(&dp
->list_node
, &ovs_net
->dps
);
1359 genl_notify(reply
, genl_info_net(info
), info
->snd_portid
,
1360 ovs_dp_datapath_multicast_group
.id
, info
->nlhdr
,
1364 err_destroy_local_port
:
1365 ovs_dp_detach_port(ovs_vport_rtnl(dp
, OVSP_LOCAL
));
1366 err_destroy_ports_array
:
1369 free_percpu(dp
->stats_percpu
);
1371 ovs_flow_tbl_destroy(genl_dereference(dp
->table
));
1373 release_net(ovs_dp_get_net(dp
));
1381 /* Called with genl_mutex. */
1382 static void __dp_destroy(struct datapath
*dp
)
1388 for (i
= 0; i
< DP_VPORT_HASH_BUCKETS
; i
++) {
1389 struct vport
*vport
;
1390 struct hlist_node
*n
;
1392 hlist_for_each_entry_safe(vport
, n
, &dp
->ports
[i
], dp_hash_node
)
1393 if (vport
->port_no
!= OVSP_LOCAL
)
1394 ovs_dp_detach_port(vport
);
1397 list_del(&dp
->list_node
);
1398 ovs_dp_detach_port(ovs_vport_rtnl(dp
, OVSP_LOCAL
));
1400 /* rtnl_unlock() will wait until all the references to devices that
1401 * are pending unregistration have been dropped. We do it here to
1402 * ensure that any internal devices (which contain DP pointers) are
1403 * fully destroyed before freeing the datapath.
1407 call_rcu(&dp
->rcu
, destroy_dp_rcu
);
1410 static int ovs_dp_cmd_del(struct sk_buff
*skb
, struct genl_info
*info
)
1412 struct sk_buff
*reply
;
1413 struct datapath
*dp
;
1416 dp
= lookup_datapath(sock_net(skb
->sk
), info
->userhdr
, info
->attrs
);
1421 reply
= ovs_dp_cmd_build_info(dp
, info
->snd_portid
,
1422 info
->snd_seq
, OVS_DP_CMD_DEL
);
1423 err
= PTR_ERR(reply
);
1429 genl_notify(reply
, genl_info_net(info
), info
->snd_portid
,
1430 ovs_dp_datapath_multicast_group
.id
, info
->nlhdr
,
1436 static int ovs_dp_cmd_set(struct sk_buff
*skb
, struct genl_info
*info
)
1438 struct sk_buff
*reply
;
1439 struct datapath
*dp
;
1442 dp
= lookup_datapath(sock_net(skb
->sk
), info
->userhdr
, info
->attrs
);
1446 reply
= ovs_dp_cmd_build_info(dp
, info
->snd_portid
,
1447 info
->snd_seq
, OVS_DP_CMD_NEW
);
1448 if (IS_ERR(reply
)) {
1449 err
= PTR_ERR(reply
);
1450 netlink_set_err(sock_net(skb
->sk
)->genl_sock
, 0,
1451 ovs_dp_datapath_multicast_group
.id
, err
);
1455 genl_notify(reply
, genl_info_net(info
), info
->snd_portid
,
1456 ovs_dp_datapath_multicast_group
.id
, info
->nlhdr
,
1462 static int ovs_dp_cmd_get(struct sk_buff
*skb
, struct genl_info
*info
)
1464 struct sk_buff
*reply
;
1465 struct datapath
*dp
;
1467 dp
= lookup_datapath(sock_net(skb
->sk
), info
->userhdr
, info
->attrs
);
1471 reply
= ovs_dp_cmd_build_info(dp
, info
->snd_portid
,
1472 info
->snd_seq
, OVS_DP_CMD_NEW
);
1474 return PTR_ERR(reply
);
1476 return genlmsg_reply(reply
, info
);
1479 static int ovs_dp_cmd_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1481 struct ovs_net
*ovs_net
= net_generic(sock_net(skb
->sk
), ovs_net_id
);
1482 struct datapath
*dp
;
1483 int skip
= cb
->args
[0];
1486 list_for_each_entry(dp
, &ovs_net
->dps
, list_node
) {
1488 ovs_dp_cmd_fill_info(dp
, skb
, NETLINK_CB(cb
->skb
).portid
,
1489 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
,
1490 OVS_DP_CMD_NEW
) < 0)
1500 static struct genl_ops dp_datapath_genl_ops
[] = {
1501 { .cmd
= OVS_DP_CMD_NEW
,
1502 .flags
= GENL_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1503 .policy
= datapath_policy
,
1504 .doit
= ovs_dp_cmd_new
1506 { .cmd
= OVS_DP_CMD_DEL
,
1507 .flags
= GENL_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1508 .policy
= datapath_policy
,
1509 .doit
= ovs_dp_cmd_del
1511 { .cmd
= OVS_DP_CMD_GET
,
1512 .flags
= 0, /* OK for unprivileged users. */
1513 .policy
= datapath_policy
,
1514 .doit
= ovs_dp_cmd_get
,
1515 .dumpit
= ovs_dp_cmd_dump
1517 { .cmd
= OVS_DP_CMD_SET
,
1518 .flags
= GENL_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1519 .policy
= datapath_policy
,
1520 .doit
= ovs_dp_cmd_set
,
1524 static const struct nla_policy vport_policy
[OVS_VPORT_ATTR_MAX
+ 1] = {
1525 [OVS_VPORT_ATTR_NAME
] = { .type
= NLA_NUL_STRING
, .len
= IFNAMSIZ
- 1 },
1526 [OVS_VPORT_ATTR_STATS
] = { .len
= sizeof(struct ovs_vport_stats
) },
1527 [OVS_VPORT_ATTR_PORT_NO
] = { .type
= NLA_U32
},
1528 [OVS_VPORT_ATTR_TYPE
] = { .type
= NLA_U32
},
1529 [OVS_VPORT_ATTR_UPCALL_PID
] = { .type
= NLA_U32
},
1530 [OVS_VPORT_ATTR_OPTIONS
] = { .type
= NLA_NESTED
},
1533 static struct genl_family dp_vport_genl_family
= {
1534 .id
= GENL_ID_GENERATE
,
1535 .hdrsize
= sizeof(struct ovs_header
),
1536 .name
= OVS_VPORT_FAMILY
,
1537 .version
= OVS_VPORT_VERSION
,
1538 .maxattr
= OVS_VPORT_ATTR_MAX
,
1542 struct genl_multicast_group ovs_dp_vport_multicast_group
= {
1543 .name
= OVS_VPORT_MCGROUP
1546 /* Called with RTNL lock or RCU read lock. */
1547 static int ovs_vport_cmd_fill_info(struct vport
*vport
, struct sk_buff
*skb
,
1548 u32 portid
, u32 seq
, u32 flags
, u8 cmd
)
1550 struct ovs_header
*ovs_header
;
1551 struct ovs_vport_stats vport_stats
;
1554 ovs_header
= genlmsg_put(skb
, portid
, seq
, &dp_vport_genl_family
,
1559 ovs_header
->dp_ifindex
= get_dpifindex(vport
->dp
);
1561 if (nla_put_u32(skb
, OVS_VPORT_ATTR_PORT_NO
, vport
->port_no
) ||
1562 nla_put_u32(skb
, OVS_VPORT_ATTR_TYPE
, vport
->ops
->type
) ||
1563 nla_put_string(skb
, OVS_VPORT_ATTR_NAME
, vport
->ops
->get_name(vport
)) ||
1564 nla_put_u32(skb
, OVS_VPORT_ATTR_UPCALL_PID
, vport
->upcall_portid
))
1565 goto nla_put_failure
;
1567 ovs_vport_get_stats(vport
, &vport_stats
);
1568 if (nla_put(skb
, OVS_VPORT_ATTR_STATS
, sizeof(struct ovs_vport_stats
),
1570 goto nla_put_failure
;
1572 err
= ovs_vport_get_options(vport
, skb
);
1573 if (err
== -EMSGSIZE
)
1576 return genlmsg_end(skb
, ovs_header
);
1581 genlmsg_cancel(skb
, ovs_header
);
1585 /* Called with RTNL lock or RCU read lock. */
1586 struct sk_buff
*ovs_vport_cmd_build_info(struct vport
*vport
, u32 portid
,
1589 struct sk_buff
*skb
;
1592 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_ATOMIC
);
1594 return ERR_PTR(-ENOMEM
);
1596 retval
= ovs_vport_cmd_fill_info(vport
, skb
, portid
, seq
, 0, cmd
);
1599 return ERR_PTR(retval
);
1604 /* Called with RTNL lock or RCU read lock. */
1605 static struct vport
*lookup_vport(struct net
*net
,
1606 struct ovs_header
*ovs_header
,
1607 struct nlattr
*a
[OVS_VPORT_ATTR_MAX
+ 1])
1609 struct datapath
*dp
;
1610 struct vport
*vport
;
1612 if (a
[OVS_VPORT_ATTR_NAME
]) {
1613 vport
= ovs_vport_locate(net
, nla_data(a
[OVS_VPORT_ATTR_NAME
]));
1615 return ERR_PTR(-ENODEV
);
1616 if (ovs_header
->dp_ifindex
&&
1617 ovs_header
->dp_ifindex
!= get_dpifindex(vport
->dp
))
1618 return ERR_PTR(-ENODEV
);
1620 } else if (a
[OVS_VPORT_ATTR_PORT_NO
]) {
1621 u32 port_no
= nla_get_u32(a
[OVS_VPORT_ATTR_PORT_NO
]);
1623 if (port_no
>= DP_MAX_PORTS
)
1624 return ERR_PTR(-EFBIG
);
1626 dp
= get_dp(net
, ovs_header
->dp_ifindex
);
1628 return ERR_PTR(-ENODEV
);
1630 vport
= ovs_vport_rtnl_rcu(dp
, port_no
);
1632 return ERR_PTR(-ENODEV
);
1635 return ERR_PTR(-EINVAL
);
1638 static int ovs_vport_cmd_new(struct sk_buff
*skb
, struct genl_info
*info
)
1640 struct nlattr
**a
= info
->attrs
;
1641 struct ovs_header
*ovs_header
= info
->userhdr
;
1642 struct vport_parms parms
;
1643 struct sk_buff
*reply
;
1644 struct vport
*vport
;
1645 struct datapath
*dp
;
1650 if (!a
[OVS_VPORT_ATTR_NAME
] || !a
[OVS_VPORT_ATTR_TYPE
] ||
1651 !a
[OVS_VPORT_ATTR_UPCALL_PID
])
1655 dp
= get_dp(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
1660 if (a
[OVS_VPORT_ATTR_PORT_NO
]) {
1661 port_no
= nla_get_u32(a
[OVS_VPORT_ATTR_PORT_NO
]);
1664 if (port_no
>= DP_MAX_PORTS
)
1667 vport
= ovs_vport_rtnl_rcu(dp
, port_no
);
1672 for (port_no
= 1; ; port_no
++) {
1673 if (port_no
>= DP_MAX_PORTS
) {
1677 vport
= ovs_vport_rtnl(dp
, port_no
);
1683 parms
.name
= nla_data(a
[OVS_VPORT_ATTR_NAME
]);
1684 parms
.type
= nla_get_u32(a
[OVS_VPORT_ATTR_TYPE
]);
1685 parms
.options
= a
[OVS_VPORT_ATTR_OPTIONS
];
1687 parms
.port_no
= port_no
;
1688 parms
.upcall_portid
= nla_get_u32(a
[OVS_VPORT_ATTR_UPCALL_PID
]);
1690 vport
= new_vport(&parms
);
1691 err
= PTR_ERR(vport
);
1696 reply
= ovs_vport_cmd_build_info(vport
, info
->snd_portid
, info
->snd_seq
,
1698 if (IS_ERR(reply
)) {
1699 err
= PTR_ERR(reply
);
1700 ovs_dp_detach_port(vport
);
1703 genl_notify(reply
, genl_info_net(info
), info
->snd_portid
,
1704 ovs_dp_vport_multicast_group
.id
, info
->nlhdr
, GFP_KERNEL
);
1712 static int ovs_vport_cmd_set(struct sk_buff
*skb
, struct genl_info
*info
)
1714 struct nlattr
**a
= info
->attrs
;
1715 struct sk_buff
*reply
;
1716 struct vport
*vport
;
1720 vport
= lookup_vport(sock_net(skb
->sk
), info
->userhdr
, a
);
1721 err
= PTR_ERR(vport
);
1726 if (a
[OVS_VPORT_ATTR_TYPE
] &&
1727 nla_get_u32(a
[OVS_VPORT_ATTR_TYPE
]) != vport
->ops
->type
)
1730 if (!err
&& a
[OVS_VPORT_ATTR_OPTIONS
])
1731 err
= ovs_vport_set_options(vport
, a
[OVS_VPORT_ATTR_OPTIONS
]);
1734 if (a
[OVS_VPORT_ATTR_UPCALL_PID
])
1735 vport
->upcall_portid
= nla_get_u32(a
[OVS_VPORT_ATTR_UPCALL_PID
]);
1737 reply
= ovs_vport_cmd_build_info(vport
, info
->snd_portid
, info
->snd_seq
,
1739 if (IS_ERR(reply
)) {
1740 netlink_set_err(sock_net(skb
->sk
)->genl_sock
, 0,
1741 ovs_dp_vport_multicast_group
.id
, PTR_ERR(reply
));
1745 genl_notify(reply
, genl_info_net(info
), info
->snd_portid
,
1746 ovs_dp_vport_multicast_group
.id
, info
->nlhdr
, GFP_KERNEL
);
1753 static int ovs_vport_cmd_del(struct sk_buff
*skb
, struct genl_info
*info
)
1755 struct nlattr
**a
= info
->attrs
;
1756 struct sk_buff
*reply
;
1757 struct vport
*vport
;
1761 vport
= lookup_vport(sock_net(skb
->sk
), info
->userhdr
, a
);
1762 err
= PTR_ERR(vport
);
1766 if (vport
->port_no
== OVSP_LOCAL
) {
1771 reply
= ovs_vport_cmd_build_info(vport
, info
->snd_portid
, info
->snd_seq
,
1773 err
= PTR_ERR(reply
);
1778 ovs_dp_detach_port(vport
);
1780 genl_notify(reply
, genl_info_net(info
), info
->snd_portid
,
1781 ovs_dp_vport_multicast_group
.id
, info
->nlhdr
, GFP_KERNEL
);
1788 static int ovs_vport_cmd_get(struct sk_buff
*skb
, struct genl_info
*info
)
1790 struct nlattr
**a
= info
->attrs
;
1791 struct ovs_header
*ovs_header
= info
->userhdr
;
1792 struct sk_buff
*reply
;
1793 struct vport
*vport
;
1797 vport
= lookup_vport(sock_net(skb
->sk
), ovs_header
, a
);
1798 err
= PTR_ERR(vport
);
1802 reply
= ovs_vport_cmd_build_info(vport
, info
->snd_portid
, info
->snd_seq
,
1804 err
= PTR_ERR(reply
);
1810 return genlmsg_reply(reply
, info
);
1817 static int ovs_vport_cmd_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1819 struct ovs_header
*ovs_header
= genlmsg_data(nlmsg_data(cb
->nlh
));
1820 struct datapath
*dp
;
1821 int bucket
= cb
->args
[0], skip
= cb
->args
[1];
1824 dp
= get_dp(sock_net(skb
->sk
), ovs_header
->dp_ifindex
);
1829 for (i
= bucket
; i
< DP_VPORT_HASH_BUCKETS
; i
++) {
1830 struct vport
*vport
;
1833 hlist_for_each_entry_rcu(vport
, &dp
->ports
[i
], dp_hash_node
) {
1835 ovs_vport_cmd_fill_info(vport
, skb
,
1836 NETLINK_CB(cb
->skb
).portid
,
1839 OVS_VPORT_CMD_NEW
) < 0)
1855 static struct genl_ops dp_vport_genl_ops
[] = {
1856 { .cmd
= OVS_VPORT_CMD_NEW
,
1857 .flags
= GENL_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1858 .policy
= vport_policy
,
1859 .doit
= ovs_vport_cmd_new
1861 { .cmd
= OVS_VPORT_CMD_DEL
,
1862 .flags
= GENL_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1863 .policy
= vport_policy
,
1864 .doit
= ovs_vport_cmd_del
1866 { .cmd
= OVS_VPORT_CMD_GET
,
1867 .flags
= 0, /* OK for unprivileged users. */
1868 .policy
= vport_policy
,
1869 .doit
= ovs_vport_cmd_get
,
1870 .dumpit
= ovs_vport_cmd_dump
1872 { .cmd
= OVS_VPORT_CMD_SET
,
1873 .flags
= GENL_ADMIN_PERM
, /* Requires CAP_NET_ADMIN privilege. */
1874 .policy
= vport_policy
,
1875 .doit
= ovs_vport_cmd_set
,
1879 struct genl_family_and_ops
{
1880 struct genl_family
*family
;
1881 struct genl_ops
*ops
;
1883 struct genl_multicast_group
*group
;
1886 static const struct genl_family_and_ops dp_genl_families
[] = {
1887 { &dp_datapath_genl_family
,
1888 dp_datapath_genl_ops
, ARRAY_SIZE(dp_datapath_genl_ops
),
1889 &ovs_dp_datapath_multicast_group
},
1890 { &dp_vport_genl_family
,
1891 dp_vport_genl_ops
, ARRAY_SIZE(dp_vport_genl_ops
),
1892 &ovs_dp_vport_multicast_group
},
1893 { &dp_flow_genl_family
,
1894 dp_flow_genl_ops
, ARRAY_SIZE(dp_flow_genl_ops
),
1895 &ovs_dp_flow_multicast_group
},
1896 { &dp_packet_genl_family
,
1897 dp_packet_genl_ops
, ARRAY_SIZE(dp_packet_genl_ops
),
1901 static void dp_unregister_genl(int n_families
)
1905 for (i
= 0; i
< n_families
; i
++)
1906 genl_unregister_family(dp_genl_families
[i
].family
);
1909 static int dp_register_genl(void)
1916 for (i
= 0; i
< ARRAY_SIZE(dp_genl_families
); i
++) {
1917 const struct genl_family_and_ops
*f
= &dp_genl_families
[i
];
1919 err
= genl_register_family_with_ops(f
->family
, f
->ops
,
1926 err
= genl_register_mc_group(f
->family
, f
->group
);
1935 dp_unregister_genl(n_registered
);
1939 static void rehash_flow_table(struct work_struct
*work
)
1941 struct datapath
*dp
;
1947 struct ovs_net
*ovs_net
= net_generic(net
, ovs_net_id
);
1949 list_for_each_entry(dp
, &ovs_net
->dps
, list_node
) {
1950 struct flow_table
*old_table
= genl_dereference(dp
->table
);
1951 struct flow_table
*new_table
;
1953 new_table
= ovs_flow_tbl_rehash(old_table
);
1954 if (!IS_ERR(new_table
)) {
1955 rcu_assign_pointer(dp
->table
, new_table
);
1956 ovs_flow_tbl_deferred_destroy(old_table
);
1963 schedule_delayed_work(&rehash_flow_wq
, REHASH_FLOW_INTERVAL
);
1966 static int __net_init
ovs_init_net(struct net
*net
)
1968 struct ovs_net
*ovs_net
= net_generic(net
, ovs_net_id
);
1970 INIT_LIST_HEAD(&ovs_net
->dps
);
1974 static void __net_exit
ovs_exit_net(struct net
*net
)
1976 struct ovs_net
*ovs_net
= net_generic(net
, ovs_net_id
);
1977 struct datapath
*dp
, *dp_next
;
1980 list_for_each_entry_safe(dp
, dp_next
, &ovs_net
->dps
, list_node
)
1985 static struct pernet_operations ovs_net_ops
= {
1986 .init
= ovs_init_net
,
1987 .exit
= ovs_exit_net
,
1989 .size
= sizeof(struct ovs_net
),
1992 static int __init
dp_init(void)
1996 BUILD_BUG_ON(sizeof(struct ovs_skb_cb
) > FIELD_SIZEOF(struct sk_buff
, cb
));
1998 pr_info("Open vSwitch switching datapath\n");
2000 err
= ovs_flow_init();
2004 err
= ovs_vport_init();
2006 goto error_flow_exit
;
2008 err
= register_pernet_device(&ovs_net_ops
);
2010 goto error_vport_exit
;
2012 err
= register_netdevice_notifier(&ovs_dp_device_notifier
);
2014 goto error_netns_exit
;
2016 err
= dp_register_genl();
2018 goto error_unreg_notifier
;
2020 schedule_delayed_work(&rehash_flow_wq
, REHASH_FLOW_INTERVAL
);
2024 error_unreg_notifier
:
2025 unregister_netdevice_notifier(&ovs_dp_device_notifier
);
2027 unregister_pernet_device(&ovs_net_ops
);
2036 static void dp_cleanup(void)
2038 cancel_delayed_work_sync(&rehash_flow_wq
);
2039 dp_unregister_genl(ARRAY_SIZE(dp_genl_families
));
2040 unregister_netdevice_notifier(&ovs_dp_device_notifier
);
2041 unregister_pernet_device(&ovs_net_ops
);
2047 module_init(dp_init
);
2048 module_exit(dp_cleanup
);
2050 MODULE_DESCRIPTION("Open vSwitch switching datapath");
2051 MODULE_LICENSE("GPL");