]>
Commit | Line | Data |
---|---|---|
ccb1352e | 1 | /* |
ad552007 | 2 | * Copyright (c) 2007-2014 Nicira, Inc. |
ccb1352e JG |
3 | * |
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. | |
7 | * | |
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. | |
12 | * | |
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 | |
16 | * 02110-1301, USA | |
17 | */ | |
18 | ||
19 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt | |
20 | ||
21 | #include <linux/init.h> | |
22 | #include <linux/module.h> | |
23 | #include <linux/if_arp.h> | |
24 | #include <linux/if_vlan.h> | |
25 | #include <linux/in.h> | |
26 | #include <linux/ip.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> | |
ccb1352e JG |
39 | #include <linux/ethtool.h> |
40 | #include <linux/wait.h> | |
ccb1352e JG |
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> | |
0c200ef9 PS |
50 | #include <linux/genetlink.h> |
51 | #include <net/genetlink.h> | |
ccb1352e | 52 | #include <net/genetlink.h> |
46df7b81 PS |
53 | #include <net/net_namespace.h> |
54 | #include <net/netns/generic.h> | |
ccb1352e JG |
55 | |
56 | #include "datapath.h" | |
57 | #include "flow.h" | |
e80857cc | 58 | #include "flow_table.h" |
e6445719 | 59 | #include "flow_netlink.h" |
ccb1352e | 60 | #include "vport-internal_dev.h" |
cff63a52 | 61 | #include "vport-netdev.h" |
ccb1352e | 62 | |
8e4e1713 PS |
63 | int ovs_net_id __read_mostly; |
64 | ||
0c200ef9 PS |
65 | static struct genl_family dp_packet_genl_family; |
66 | static struct genl_family dp_flow_genl_family; | |
67 | static struct genl_family dp_datapath_genl_family; | |
68 | ||
69 | static struct genl_multicast_group ovs_dp_flow_multicast_group = { | |
70 | .name = OVS_FLOW_MCGROUP | |
71 | }; | |
72 | ||
73 | static struct genl_multicast_group ovs_dp_datapath_multicast_group = { | |
74 | .name = OVS_DATAPATH_MCGROUP | |
75 | }; | |
76 | ||
77 | struct genl_multicast_group ovs_dp_vport_multicast_group = { | |
78 | .name = OVS_VPORT_MCGROUP | |
79 | }; | |
80 | ||
fb5d1e9e JR |
81 | /* Check if need to build a reply message. |
82 | * OVS userspace sets the NLM_F_ECHO flag if it needs the reply. */ | |
83 | static bool ovs_must_notify(struct genl_info *info, | |
84 | const struct genl_multicast_group *grp) | |
85 | { | |
86 | return info->nlhdr->nlmsg_flags & NLM_F_ECHO || | |
87 | netlink_has_listeners(genl_info_net(info)->genl_sock, 0); | |
88 | } | |
89 | ||
68eb5503 | 90 | static void ovs_notify(struct genl_family *family, |
2a94fe48 | 91 | struct sk_buff *skb, struct genl_info *info) |
ed661185 | 92 | { |
68eb5503 | 93 | genl_notify(family, skb, genl_info_net(info), info->snd_portid, |
2a94fe48 | 94 | 0, info->nlhdr, GFP_KERNEL); |
ed661185 TG |
95 | } |
96 | ||
ccb1352e JG |
97 | /** |
98 | * DOC: Locking: | |
99 | * | |
8e4e1713 PS |
100 | * All writes e.g. Writes to device state (add/remove datapath, port, set |
101 | * operations on vports, etc.), Writes to other state (flow table | |
102 | * modifications, set miscellaneous datapath parameters, etc.) are protected | |
103 | * by ovs_lock. | |
ccb1352e JG |
104 | * |
105 | * Reads are protected by RCU. | |
106 | * | |
107 | * There are a few special cases (mostly stats) that have their own | |
108 | * synchronization but they nest under all of above and don't interact with | |
109 | * each other. | |
8e4e1713 PS |
110 | * |
111 | * The RTNL lock nests inside ovs_mutex. | |
ccb1352e JG |
112 | */ |
113 | ||
8e4e1713 PS |
114 | static DEFINE_MUTEX(ovs_mutex); |
115 | ||
116 | void ovs_lock(void) | |
117 | { | |
118 | mutex_lock(&ovs_mutex); | |
119 | } | |
120 | ||
121 | void ovs_unlock(void) | |
122 | { | |
123 | mutex_unlock(&ovs_mutex); | |
124 | } | |
125 | ||
126 | #ifdef CONFIG_LOCKDEP | |
127 | int lockdep_ovsl_is_held(void) | |
128 | { | |
129 | if (debug_locks) | |
130 | return lockdep_is_held(&ovs_mutex); | |
131 | else | |
132 | return 1; | |
133 | } | |
134 | #endif | |
135 | ||
ccb1352e | 136 | static struct vport *new_vport(const struct vport_parms *); |
8055a89c | 137 | static int queue_gso_packets(struct datapath *dp, struct sk_buff *, |
ccb1352e | 138 | const struct dp_upcall_info *); |
8055a89c | 139 | static int queue_userspace_packet(struct datapath *dp, struct sk_buff *, |
ccb1352e JG |
140 | const struct dp_upcall_info *); |
141 | ||
8e4e1713 | 142 | /* Must be called with rcu_read_lock or ovs_mutex. */ |
46df7b81 | 143 | static struct datapath *get_dp(struct net *net, int dp_ifindex) |
ccb1352e JG |
144 | { |
145 | struct datapath *dp = NULL; | |
146 | struct net_device *dev; | |
147 | ||
148 | rcu_read_lock(); | |
46df7b81 | 149 | dev = dev_get_by_index_rcu(net, dp_ifindex); |
ccb1352e JG |
150 | if (dev) { |
151 | struct vport *vport = ovs_internal_dev_get_vport(dev); | |
152 | if (vport) | |
153 | dp = vport->dp; | |
154 | } | |
155 | rcu_read_unlock(); | |
156 | ||
157 | return dp; | |
158 | } | |
159 | ||
8e4e1713 | 160 | /* Must be called with rcu_read_lock or ovs_mutex. */ |
443cd88c | 161 | static const char *ovs_dp_name(const struct datapath *dp) |
ccb1352e | 162 | { |
8e4e1713 | 163 | struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL); |
ccb1352e JG |
164 | return vport->ops->get_name(vport); |
165 | } | |
166 | ||
167 | static int get_dpifindex(struct datapath *dp) | |
168 | { | |
169 | struct vport *local; | |
170 | int ifindex; | |
171 | ||
172 | rcu_read_lock(); | |
173 | ||
15eac2a7 | 174 | local = ovs_vport_rcu(dp, OVSP_LOCAL); |
ccb1352e | 175 | if (local) |
cff63a52 | 176 | ifindex = netdev_vport_priv(local)->dev->ifindex; |
ccb1352e JG |
177 | else |
178 | ifindex = 0; | |
179 | ||
180 | rcu_read_unlock(); | |
181 | ||
182 | return ifindex; | |
183 | } | |
184 | ||
185 | static void destroy_dp_rcu(struct rcu_head *rcu) | |
186 | { | |
187 | struct datapath *dp = container_of(rcu, struct datapath, rcu); | |
188 | ||
ccb1352e | 189 | free_percpu(dp->stats_percpu); |
46df7b81 | 190 | release_net(ovs_dp_get_net(dp)); |
15eac2a7 | 191 | kfree(dp->ports); |
ccb1352e JG |
192 | kfree(dp); |
193 | } | |
194 | ||
15eac2a7 PS |
195 | static struct hlist_head *vport_hash_bucket(const struct datapath *dp, |
196 | u16 port_no) | |
197 | { | |
198 | return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)]; | |
199 | } | |
200 | ||
bb6f9a70 | 201 | /* Called with ovs_mutex or RCU read lock. */ |
15eac2a7 PS |
202 | struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no) |
203 | { | |
204 | struct vport *vport; | |
15eac2a7 PS |
205 | struct hlist_head *head; |
206 | ||
207 | head = vport_hash_bucket(dp, port_no); | |
b67bfe0d | 208 | hlist_for_each_entry_rcu(vport, head, dp_hash_node) { |
15eac2a7 PS |
209 | if (vport->port_no == port_no) |
210 | return vport; | |
211 | } | |
212 | return NULL; | |
213 | } | |
214 | ||
8e4e1713 | 215 | /* Called with ovs_mutex. */ |
ccb1352e JG |
216 | static struct vport *new_vport(const struct vport_parms *parms) |
217 | { | |
218 | struct vport *vport; | |
219 | ||
220 | vport = ovs_vport_add(parms); | |
221 | if (!IS_ERR(vport)) { | |
222 | struct datapath *dp = parms->dp; | |
15eac2a7 | 223 | struct hlist_head *head = vport_hash_bucket(dp, vport->port_no); |
ccb1352e | 224 | |
15eac2a7 | 225 | hlist_add_head_rcu(&vport->dp_hash_node, head); |
ccb1352e | 226 | } |
ccb1352e JG |
227 | return vport; |
228 | } | |
229 | ||
ccb1352e JG |
230 | void ovs_dp_detach_port(struct vport *p) |
231 | { | |
8e4e1713 | 232 | ASSERT_OVSL(); |
ccb1352e JG |
233 | |
234 | /* First drop references to device. */ | |
15eac2a7 | 235 | hlist_del_rcu(&p->dp_hash_node); |
ccb1352e JG |
236 | |
237 | /* Then destroy it. */ | |
238 | ovs_vport_del(p); | |
239 | } | |
240 | ||
241 | /* Must be called with rcu_read_lock. */ | |
242 | void ovs_dp_process_received_packet(struct vport *p, struct sk_buff *skb) | |
243 | { | |
244 | struct datapath *dp = p->dp; | |
245 | struct sw_flow *flow; | |
246 | struct dp_stats_percpu *stats; | |
247 | struct sw_flow_key key; | |
248 | u64 *stats_counter; | |
1bd7116f | 249 | u32 n_mask_hit; |
ccb1352e | 250 | int error; |
ccb1352e | 251 | |
404f2f10 | 252 | stats = this_cpu_ptr(dp->stats_percpu); |
ccb1352e JG |
253 | |
254 | /* Extract flow from 'skb' into 'key'. */ | |
03f0d916 | 255 | error = ovs_flow_extract(skb, p->port_no, &key); |
ccb1352e JG |
256 | if (unlikely(error)) { |
257 | kfree_skb(skb); | |
258 | return; | |
259 | } | |
260 | ||
261 | /* Look up flow. */ | |
5bb50632 | 262 | flow = ovs_flow_tbl_lookup_stats(&dp->table, &key, &n_mask_hit); |
ccb1352e JG |
263 | if (unlikely(!flow)) { |
264 | struct dp_upcall_info upcall; | |
265 | ||
266 | upcall.cmd = OVS_PACKET_CMD_MISS; | |
267 | upcall.key = &key; | |
268 | upcall.userdata = NULL; | |
15e47304 | 269 | upcall.portid = p->upcall_portid; |
ccb1352e JG |
270 | ovs_dp_upcall(dp, skb, &upcall); |
271 | consume_skb(skb); | |
272 | stats_counter = &stats->n_missed; | |
273 | goto out; | |
274 | } | |
275 | ||
276 | OVS_CB(skb)->flow = flow; | |
03f0d916 | 277 | OVS_CB(skb)->pkt_key = &key; |
ccb1352e | 278 | |
ad552007 | 279 | ovs_flow_stats_update(OVS_CB(skb)->flow, key.tp.flags, skb); |
ccb1352e | 280 | ovs_execute_actions(dp, skb); |
e298e505 | 281 | stats_counter = &stats->n_hit; |
ccb1352e JG |
282 | |
283 | out: | |
284 | /* Update datapath statistics. */ | |
df9d9fdf | 285 | u64_stats_update_begin(&stats->syncp); |
ccb1352e | 286 | (*stats_counter)++; |
1bd7116f | 287 | stats->n_mask_hit += n_mask_hit; |
df9d9fdf | 288 | u64_stats_update_end(&stats->syncp); |
ccb1352e JG |
289 | } |
290 | ||
ccb1352e | 291 | int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb, |
46df7b81 | 292 | const struct dp_upcall_info *upcall_info) |
ccb1352e JG |
293 | { |
294 | struct dp_stats_percpu *stats; | |
ccb1352e JG |
295 | int err; |
296 | ||
15e47304 | 297 | if (upcall_info->portid == 0) { |
ccb1352e JG |
298 | err = -ENOTCONN; |
299 | goto err; | |
300 | } | |
301 | ||
ccb1352e | 302 | if (!skb_is_gso(skb)) |
8055a89c | 303 | err = queue_userspace_packet(dp, skb, upcall_info); |
ccb1352e | 304 | else |
8055a89c | 305 | err = queue_gso_packets(dp, skb, upcall_info); |
ccb1352e JG |
306 | if (err) |
307 | goto err; | |
308 | ||
309 | return 0; | |
310 | ||
311 | err: | |
404f2f10 | 312 | stats = this_cpu_ptr(dp->stats_percpu); |
ccb1352e | 313 | |
df9d9fdf | 314 | u64_stats_update_begin(&stats->syncp); |
ccb1352e | 315 | stats->n_lost++; |
df9d9fdf | 316 | u64_stats_update_end(&stats->syncp); |
ccb1352e JG |
317 | |
318 | return err; | |
319 | } | |
320 | ||
8055a89c | 321 | static int queue_gso_packets(struct datapath *dp, struct sk_buff *skb, |
ccb1352e JG |
322 | const struct dp_upcall_info *upcall_info) |
323 | { | |
a1b5d0dd | 324 | unsigned short gso_type = skb_shinfo(skb)->gso_type; |
ccb1352e JG |
325 | struct dp_upcall_info later_info; |
326 | struct sw_flow_key later_key; | |
327 | struct sk_buff *segs, *nskb; | |
328 | int err; | |
329 | ||
09c5e605 | 330 | segs = __skb_gso_segment(skb, NETIF_F_SG, false); |
92e5dfc3 PS |
331 | if (IS_ERR(segs)) |
332 | return PTR_ERR(segs); | |
ccb1352e JG |
333 | |
334 | /* Queue all of the segments. */ | |
335 | skb = segs; | |
336 | do { | |
8055a89c | 337 | err = queue_userspace_packet(dp, skb, upcall_info); |
ccb1352e JG |
338 | if (err) |
339 | break; | |
340 | ||
a1b5d0dd | 341 | if (skb == segs && gso_type & SKB_GSO_UDP) { |
ccb1352e JG |
342 | /* The initial flow key extracted by ovs_flow_extract() |
343 | * in this case is for a first fragment, so we need to | |
344 | * properly mark later fragments. | |
345 | */ | |
346 | later_key = *upcall_info->key; | |
347 | later_key.ip.frag = OVS_FRAG_TYPE_LATER; | |
348 | ||
349 | later_info = *upcall_info; | |
350 | later_info.key = &later_key; | |
351 | upcall_info = &later_info; | |
352 | } | |
353 | } while ((skb = skb->next)); | |
354 | ||
355 | /* Free all of the segments. */ | |
356 | skb = segs; | |
357 | do { | |
358 | nskb = skb->next; | |
359 | if (err) | |
360 | kfree_skb(skb); | |
361 | else | |
362 | consume_skb(skb); | |
363 | } while ((skb = nskb)); | |
364 | return err; | |
365 | } | |
366 | ||
c3ff8cfe TG |
367 | static size_t key_attr_size(void) |
368 | { | |
369 | return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */ | |
7d5437c7 PS |
370 | + nla_total_size(0) /* OVS_KEY_ATTR_TUNNEL */ |
371 | + nla_total_size(8) /* OVS_TUNNEL_KEY_ATTR_ID */ | |
372 | + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_SRC */ | |
373 | + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_DST */ | |
374 | + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TOS */ | |
375 | + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TTL */ | |
376 | + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT */ | |
377 | + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_CSUM */ | |
c3ff8cfe TG |
378 | + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */ |
379 | + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */ | |
380 | + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */ | |
381 | + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */ | |
382 | + nla_total_size(4) /* OVS_KEY_ATTR_8021Q */ | |
383 | + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */ | |
384 | + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */ | |
385 | + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */ | |
386 | + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */ | |
387 | + nla_total_size(28); /* OVS_KEY_ATTR_ND */ | |
388 | } | |
389 | ||
bda56f14 TG |
390 | static size_t upcall_msg_size(const struct nlattr *userdata, |
391 | unsigned int hdrlen) | |
c3ff8cfe TG |
392 | { |
393 | size_t size = NLMSG_ALIGN(sizeof(struct ovs_header)) | |
bda56f14 | 394 | + nla_total_size(hdrlen) /* OVS_PACKET_ATTR_PACKET */ |
c3ff8cfe TG |
395 | + nla_total_size(key_attr_size()); /* OVS_PACKET_ATTR_KEY */ |
396 | ||
397 | /* OVS_PACKET_ATTR_USERDATA */ | |
398 | if (userdata) | |
399 | size += NLA_ALIGN(userdata->nla_len); | |
400 | ||
401 | return size; | |
402 | } | |
403 | ||
8055a89c | 404 | static int queue_userspace_packet(struct datapath *dp, struct sk_buff *skb, |
ccb1352e JG |
405 | const struct dp_upcall_info *upcall_info) |
406 | { | |
407 | struct ovs_header *upcall; | |
408 | struct sk_buff *nskb = NULL; | |
409 | struct sk_buff *user_skb; /* to be queued to userspace */ | |
410 | struct nlattr *nla; | |
795449d8 | 411 | struct genl_info info = { |
8055a89c | 412 | .dst_sk = ovs_dp_get_net(dp)->genl_sock, |
795449d8 TG |
413 | .snd_portid = upcall_info->portid, |
414 | }; | |
415 | size_t len; | |
bda56f14 | 416 | unsigned int hlen; |
8055a89c TG |
417 | int err, dp_ifindex; |
418 | ||
419 | dp_ifindex = get_dpifindex(dp); | |
420 | if (!dp_ifindex) | |
421 | return -ENODEV; | |
ccb1352e JG |
422 | |
423 | if (vlan_tx_tag_present(skb)) { | |
424 | nskb = skb_clone(skb, GFP_ATOMIC); | |
425 | if (!nskb) | |
426 | return -ENOMEM; | |
427 | ||
86a9bad3 | 428 | nskb = __vlan_put_tag(nskb, nskb->vlan_proto, vlan_tx_tag_get(nskb)); |
8aa51d64 | 429 | if (!nskb) |
ccb1352e JG |
430 | return -ENOMEM; |
431 | ||
432 | nskb->vlan_tci = 0; | |
433 | skb = nskb; | |
434 | } | |
435 | ||
436 | if (nla_attr_size(skb->len) > USHRT_MAX) { | |
437 | err = -EFBIG; | |
438 | goto out; | |
439 | } | |
440 | ||
bda56f14 TG |
441 | /* Complete checksum if needed */ |
442 | if (skb->ip_summed == CHECKSUM_PARTIAL && | |
443 | (err = skb_checksum_help(skb))) | |
444 | goto out; | |
445 | ||
446 | /* Older versions of OVS user space enforce alignment of the last | |
447 | * Netlink attribute to NLA_ALIGNTO which would require extensive | |
448 | * padding logic. Only perform zerocopy if padding is not required. | |
449 | */ | |
450 | if (dp->user_features & OVS_DP_F_UNALIGNED) | |
451 | hlen = skb_zerocopy_headlen(skb); | |
452 | else | |
453 | hlen = skb->len; | |
454 | ||
455 | len = upcall_msg_size(upcall_info->userdata, hlen); | |
795449d8 | 456 | user_skb = genlmsg_new_unicast(len, &info, GFP_ATOMIC); |
ccb1352e JG |
457 | if (!user_skb) { |
458 | err = -ENOMEM; | |
459 | goto out; | |
460 | } | |
461 | ||
462 | upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family, | |
463 | 0, upcall_info->cmd); | |
464 | upcall->dp_ifindex = dp_ifindex; | |
465 | ||
466 | nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY); | |
e6445719 | 467 | ovs_nla_put_flow(upcall_info->key, upcall_info->key, user_skb); |
ccb1352e JG |
468 | nla_nest_end(user_skb, nla); |
469 | ||
470 | if (upcall_info->userdata) | |
4490108b BP |
471 | __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA, |
472 | nla_len(upcall_info->userdata), | |
473 | nla_data(upcall_info->userdata)); | |
ccb1352e | 474 | |
bda56f14 TG |
475 | /* Only reserve room for attribute header, packet data is added |
476 | * in skb_zerocopy() */ | |
477 | if (!(nla = nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, 0))) { | |
478 | err = -ENOBUFS; | |
479 | goto out; | |
480 | } | |
481 | nla->nla_len = nla_attr_size(skb->len); | |
ccb1352e | 482 | |
36d5fe6a ZK |
483 | err = skb_zerocopy(user_skb, skb, skb->len, hlen); |
484 | if (err) | |
485 | goto out; | |
ccb1352e | 486 | |
aea0bb4f TG |
487 | /* Pad OVS_PACKET_ATTR_PACKET if linear copy was performed */ |
488 | if (!(dp->user_features & OVS_DP_F_UNALIGNED)) { | |
489 | size_t plen = NLA_ALIGN(user_skb->len) - user_skb->len; | |
490 | ||
491 | if (plen > 0) | |
492 | memset(skb_put(user_skb, plen), 0, plen); | |
493 | } | |
494 | ||
bda56f14 | 495 | ((struct nlmsghdr *) user_skb->data)->nlmsg_len = user_skb->len; |
ccb1352e | 496 | |
bda56f14 | 497 | err = genlmsg_unicast(ovs_dp_get_net(dp), user_skb, upcall_info->portid); |
ccb1352e | 498 | out: |
36d5fe6a ZK |
499 | if (err) |
500 | skb_tx_error(skb); | |
ccb1352e JG |
501 | kfree_skb(nskb); |
502 | return err; | |
503 | } | |
504 | ||
ccb1352e JG |
505 | static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info) |
506 | { | |
507 | struct ovs_header *ovs_header = info->userhdr; | |
508 | struct nlattr **a = info->attrs; | |
509 | struct sw_flow_actions *acts; | |
510 | struct sk_buff *packet; | |
511 | struct sw_flow *flow; | |
512 | struct datapath *dp; | |
513 | struct ethhdr *eth; | |
514 | int len; | |
515 | int err; | |
ccb1352e JG |
516 | |
517 | err = -EINVAL; | |
518 | if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] || | |
dded45fc | 519 | !a[OVS_PACKET_ATTR_ACTIONS]) |
ccb1352e JG |
520 | goto err; |
521 | ||
522 | len = nla_len(a[OVS_PACKET_ATTR_PACKET]); | |
523 | packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL); | |
524 | err = -ENOMEM; | |
525 | if (!packet) | |
526 | goto err; | |
527 | skb_reserve(packet, NET_IP_ALIGN); | |
528 | ||
32686a9d | 529 | nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len); |
ccb1352e JG |
530 | |
531 | skb_reset_mac_header(packet); | |
532 | eth = eth_hdr(packet); | |
533 | ||
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. */ | |
e5c5d22e | 537 | if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN) |
ccb1352e JG |
538 | packet->protocol = eth->h_proto; |
539 | else | |
540 | packet->protocol = htons(ETH_P_802_2); | |
541 | ||
542 | /* Build an sw_flow for sending this packet. */ | |
23dabf88 | 543 | flow = ovs_flow_alloc(); |
ccb1352e JG |
544 | err = PTR_ERR(flow); |
545 | if (IS_ERR(flow)) | |
546 | goto err_kfree_skb; | |
547 | ||
03f0d916 | 548 | err = ovs_flow_extract(packet, -1, &flow->key); |
ccb1352e JG |
549 | if (err) |
550 | goto err_flow_free; | |
551 | ||
e6445719 | 552 | err = ovs_nla_get_flow_metadata(flow, a[OVS_PACKET_ATTR_KEY]); |
ccb1352e JG |
553 | if (err) |
554 | goto err_flow_free; | |
e6445719 | 555 | acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_PACKET_ATTR_ACTIONS])); |
ccb1352e JG |
556 | err = PTR_ERR(acts); |
557 | if (IS_ERR(acts)) | |
558 | goto err_flow_free; | |
74f84a57 | 559 | |
e6445719 PS |
560 | err = ovs_nla_copy_actions(a[OVS_PACKET_ATTR_ACTIONS], |
561 | &flow->key, 0, &acts); | |
ccb1352e | 562 | rcu_assign_pointer(flow->sf_acts, acts); |
74f84a57 PS |
563 | if (err) |
564 | goto err_flow_free; | |
ccb1352e JG |
565 | |
566 | OVS_CB(packet)->flow = flow; | |
03f0d916 | 567 | OVS_CB(packet)->pkt_key = &flow->key; |
ccb1352e | 568 | packet->priority = flow->key.phy.priority; |
39c7caeb | 569 | packet->mark = flow->key.phy.skb_mark; |
ccb1352e JG |
570 | |
571 | rcu_read_lock(); | |
46df7b81 | 572 | dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex); |
ccb1352e JG |
573 | err = -ENODEV; |
574 | if (!dp) | |
575 | goto err_unlock; | |
576 | ||
577 | local_bh_disable(); | |
578 | err = ovs_execute_actions(dp, packet); | |
579 | local_bh_enable(); | |
580 | rcu_read_unlock(); | |
581 | ||
03f0d916 | 582 | ovs_flow_free(flow, false); |
ccb1352e JG |
583 | return err; |
584 | ||
585 | err_unlock: | |
586 | rcu_read_unlock(); | |
587 | err_flow_free: | |
03f0d916 | 588 | ovs_flow_free(flow, false); |
ccb1352e JG |
589 | err_kfree_skb: |
590 | kfree_skb(packet); | |
591 | err: | |
592 | return err; | |
593 | } | |
594 | ||
595 | static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = { | |
dded45fc | 596 | [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN }, |
ccb1352e JG |
597 | [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED }, |
598 | [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED }, | |
599 | }; | |
600 | ||
4534de83 | 601 | static const struct genl_ops dp_packet_genl_ops[] = { |
ccb1352e JG |
602 | { .cmd = OVS_PACKET_CMD_EXECUTE, |
603 | .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ | |
604 | .policy = packet_policy, | |
605 | .doit = ovs_packet_cmd_execute | |
606 | } | |
607 | }; | |
608 | ||
0c200ef9 PS |
609 | static struct genl_family dp_packet_genl_family = { |
610 | .id = GENL_ID_GENERATE, | |
611 | .hdrsize = sizeof(struct ovs_header), | |
612 | .name = OVS_PACKET_FAMILY, | |
613 | .version = OVS_PACKET_VERSION, | |
614 | .maxattr = OVS_PACKET_ATTR_MAX, | |
615 | .netnsok = true, | |
616 | .parallel_ops = true, | |
617 | .ops = dp_packet_genl_ops, | |
618 | .n_ops = ARRAY_SIZE(dp_packet_genl_ops), | |
619 | }; | |
620 | ||
1bd7116f AZ |
621 | static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats, |
622 | struct ovs_dp_megaflow_stats *mega_stats) | |
ccb1352e JG |
623 | { |
624 | int i; | |
ccb1352e | 625 | |
1bd7116f AZ |
626 | memset(mega_stats, 0, sizeof(*mega_stats)); |
627 | ||
b637e498 | 628 | stats->n_flows = ovs_flow_tbl_count(&dp->table); |
1bd7116f | 629 | mega_stats->n_masks = ovs_flow_tbl_num_masks(&dp->table); |
ccb1352e JG |
630 | |
631 | stats->n_hit = stats->n_missed = stats->n_lost = 0; | |
1bd7116f | 632 | |
ccb1352e JG |
633 | for_each_possible_cpu(i) { |
634 | const struct dp_stats_percpu *percpu_stats; | |
635 | struct dp_stats_percpu local_stats; | |
636 | unsigned int start; | |
637 | ||
638 | percpu_stats = per_cpu_ptr(dp->stats_percpu, i); | |
639 | ||
640 | do { | |
57a7744e | 641 | start = u64_stats_fetch_begin_irq(&percpu_stats->syncp); |
ccb1352e | 642 | local_stats = *percpu_stats; |
57a7744e | 643 | } while (u64_stats_fetch_retry_irq(&percpu_stats->syncp, start)); |
ccb1352e JG |
644 | |
645 | stats->n_hit += local_stats.n_hit; | |
646 | stats->n_missed += local_stats.n_missed; | |
647 | stats->n_lost += local_stats.n_lost; | |
1bd7116f | 648 | mega_stats->n_mask_hit += local_stats.n_mask_hit; |
ccb1352e JG |
649 | } |
650 | } | |
651 | ||
c3ff8cfe TG |
652 | static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts) |
653 | { | |
654 | return NLMSG_ALIGN(sizeof(struct ovs_header)) | |
655 | + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_KEY */ | |
03f0d916 | 656 | + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_MASK */ |
c3ff8cfe TG |
657 | + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */ |
658 | + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */ | |
659 | + nla_total_size(8) /* OVS_FLOW_ATTR_USED */ | |
660 | + nla_total_size(acts->actions_len); /* OVS_FLOW_ATTR_ACTIONS */ | |
661 | } | |
662 | ||
bb6f9a70 | 663 | /* Called with ovs_mutex or RCU read lock. */ |
0e9796b4 | 664 | static int ovs_flow_cmd_fill_info(const struct sw_flow *flow, int dp_ifindex, |
15e47304 | 665 | struct sk_buff *skb, u32 portid, |
ccb1352e JG |
666 | u32 seq, u32 flags, u8 cmd) |
667 | { | |
668 | const int skb_orig_len = skb->len; | |
74f84a57 | 669 | struct nlattr *start; |
ccb1352e | 670 | struct ovs_flow_stats stats; |
e298e505 PS |
671 | __be16 tcp_flags; |
672 | unsigned long used; | |
ccb1352e JG |
673 | struct ovs_header *ovs_header; |
674 | struct nlattr *nla; | |
ccb1352e JG |
675 | int err; |
676 | ||
15e47304 | 677 | ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd); |
ccb1352e JG |
678 | if (!ovs_header) |
679 | return -EMSGSIZE; | |
680 | ||
0e9796b4 | 681 | ovs_header->dp_ifindex = dp_ifindex; |
ccb1352e | 682 | |
03f0d916 | 683 | /* Fill flow key. */ |
ccb1352e JG |
684 | nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY); |
685 | if (!nla) | |
686 | goto nla_put_failure; | |
03f0d916 | 687 | |
e6445719 | 688 | err = ovs_nla_put_flow(&flow->unmasked_key, &flow->unmasked_key, skb); |
03f0d916 AZ |
689 | if (err) |
690 | goto error; | |
691 | nla_nest_end(skb, nla); | |
692 | ||
693 | nla = nla_nest_start(skb, OVS_FLOW_ATTR_MASK); | |
694 | if (!nla) | |
695 | goto nla_put_failure; | |
696 | ||
e6445719 | 697 | err = ovs_nla_put_flow(&flow->key, &flow->mask->key, skb); |
ccb1352e JG |
698 | if (err) |
699 | goto error; | |
03f0d916 | 700 | |
ccb1352e JG |
701 | nla_nest_end(skb, nla); |
702 | ||
e298e505 | 703 | ovs_flow_stats_get(flow, &stats, &used, &tcp_flags); |
0e9796b4 | 704 | |
028d6a67 DM |
705 | if (used && |
706 | nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used))) | |
707 | goto nla_put_failure; | |
ccb1352e | 708 | |
028d6a67 | 709 | if (stats.n_packets && |
e298e505 | 710 | nla_put(skb, OVS_FLOW_ATTR_STATS, sizeof(struct ovs_flow_stats), &stats)) |
028d6a67 | 711 | goto nla_put_failure; |
ccb1352e | 712 | |
e298e505 PS |
713 | if ((u8)ntohs(tcp_flags) && |
714 | nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, (u8)ntohs(tcp_flags))) | |
028d6a67 | 715 | goto nla_put_failure; |
ccb1352e JG |
716 | |
717 | /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if | |
718 | * this is the first flow to be dumped into 'skb'. This is unusual for | |
719 | * Netlink but individual action lists can be longer than | |
720 | * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this. | |
721 | * The userspace caller can always fetch the actions separately if it | |
722 | * really wants them. (Most userspace callers in fact don't care.) | |
723 | * | |
724 | * This can only fail for dump operations because the skb is always | |
725 | * properly sized for single flows. | |
726 | */ | |
74f84a57 PS |
727 | start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS); |
728 | if (start) { | |
d57170b1 PS |
729 | const struct sw_flow_actions *sf_acts; |
730 | ||
663efa36 | 731 | sf_acts = rcu_dereference_ovsl(flow->sf_acts); |
e6445719 PS |
732 | err = ovs_nla_put_actions(sf_acts->actions, |
733 | sf_acts->actions_len, skb); | |
0e9796b4 | 734 | |
74f84a57 PS |
735 | if (!err) |
736 | nla_nest_end(skb, start); | |
737 | else { | |
738 | if (skb_orig_len) | |
739 | goto error; | |
740 | ||
741 | nla_nest_cancel(skb, start); | |
742 | } | |
743 | } else if (skb_orig_len) | |
744 | goto nla_put_failure; | |
ccb1352e JG |
745 | |
746 | return genlmsg_end(skb, ovs_header); | |
747 | ||
748 | nla_put_failure: | |
749 | err = -EMSGSIZE; | |
750 | error: | |
751 | genlmsg_cancel(skb, ovs_header); | |
752 | return err; | |
753 | } | |
754 | ||
0e9796b4 JR |
755 | /* May not be called with RCU read lock. */ |
756 | static struct sk_buff *ovs_flow_cmd_alloc_info(const struct sw_flow_actions *acts, | |
fb5d1e9e JR |
757 | struct genl_info *info, |
758 | bool always) | |
ccb1352e | 759 | { |
fb5d1e9e | 760 | struct sk_buff *skb; |
ccb1352e | 761 | |
fb5d1e9e JR |
762 | if (!always && !ovs_must_notify(info, &ovs_dp_flow_multicast_group)) |
763 | return NULL; | |
764 | ||
0e9796b4 | 765 | skb = genlmsg_new_unicast(ovs_flow_cmd_msg_size(acts), info, GFP_KERNEL); |
fb5d1e9e JR |
766 | if (!skb) |
767 | return ERR_PTR(-ENOMEM); | |
768 | ||
769 | return skb; | |
ccb1352e JG |
770 | } |
771 | ||
0e9796b4 JR |
772 | /* Called with ovs_mutex. */ |
773 | static struct sk_buff *ovs_flow_cmd_build_info(const struct sw_flow *flow, | |
774 | int dp_ifindex, | |
775 | struct genl_info *info, u8 cmd, | |
776 | bool always) | |
ccb1352e JG |
777 | { |
778 | struct sk_buff *skb; | |
779 | int retval; | |
780 | ||
0e9796b4 JR |
781 | skb = ovs_flow_cmd_alloc_info(ovsl_dereference(flow->sf_acts), info, |
782 | always); | |
fb5d1e9e JR |
783 | if (!skb || IS_ERR(skb)) |
784 | return skb; | |
ccb1352e | 785 | |
0e9796b4 JR |
786 | retval = ovs_flow_cmd_fill_info(flow, dp_ifindex, skb, |
787 | info->snd_portid, info->snd_seq, 0, | |
788 | cmd); | |
ccb1352e JG |
789 | BUG_ON(retval < 0); |
790 | return skb; | |
791 | } | |
792 | ||
37bdc87b | 793 | static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info) |
ccb1352e JG |
794 | { |
795 | struct nlattr **a = info->attrs; | |
796 | struct ovs_header *ovs_header = info->userhdr; | |
893f139b | 797 | struct sw_flow *flow, *new_flow; |
03f0d916 | 798 | struct sw_flow_mask mask; |
ccb1352e JG |
799 | struct sk_buff *reply; |
800 | struct datapath *dp; | |
37bdc87b | 801 | struct sw_flow_actions *acts; |
03f0d916 | 802 | struct sw_flow_match match; |
ccb1352e | 803 | int error; |
ccb1352e | 804 | |
893f139b | 805 | /* Must have key and actions. */ |
ccb1352e JG |
806 | error = -EINVAL; |
807 | if (!a[OVS_FLOW_ATTR_KEY]) | |
808 | goto error; | |
893f139b JR |
809 | if (!a[OVS_FLOW_ATTR_ACTIONS]) |
810 | goto error; | |
03f0d916 | 811 | |
893f139b JR |
812 | /* Most of the time we need to allocate a new flow, do it before |
813 | * locking. | |
814 | */ | |
815 | new_flow = ovs_flow_alloc(); | |
816 | if (IS_ERR(new_flow)) { | |
817 | error = PTR_ERR(new_flow); | |
818 | goto error; | |
819 | } | |
820 | ||
821 | /* Extract key. */ | |
822 | ovs_match_init(&match, &new_flow->unmasked_key, &mask); | |
23dabf88 | 823 | error = ovs_nla_get_match(&match, |
e6445719 | 824 | a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]); |
ccb1352e | 825 | if (error) |
893f139b | 826 | goto err_kfree_flow; |
ccb1352e | 827 | |
893f139b | 828 | ovs_flow_mask_key(&new_flow->key, &new_flow->unmasked_key, &mask); |
74f84a57 | 829 | |
893f139b | 830 | /* Validate actions. */ |
37bdc87b JR |
831 | acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_FLOW_ATTR_ACTIONS])); |
832 | error = PTR_ERR(acts); | |
833 | if (IS_ERR(acts)) | |
893f139b | 834 | goto err_kfree_flow; |
37bdc87b | 835 | |
893f139b JR |
836 | error = ovs_nla_copy_actions(a[OVS_FLOW_ATTR_ACTIONS], &new_flow->key, |
837 | 0, &acts); | |
37bdc87b JR |
838 | if (error) { |
839 | OVS_NLERR("Flow actions may not be safe on all matching packets.\n"); | |
893f139b JR |
840 | goto err_kfree_acts; |
841 | } | |
842 | ||
843 | reply = ovs_flow_cmd_alloc_info(acts, info, false); | |
844 | if (IS_ERR(reply)) { | |
845 | error = PTR_ERR(reply); | |
846 | goto err_kfree_acts; | |
ccb1352e JG |
847 | } |
848 | ||
8e4e1713 | 849 | ovs_lock(); |
46df7b81 | 850 | dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex); |
893f139b JR |
851 | if (unlikely(!dp)) { |
852 | error = -ENODEV; | |
8e4e1713 | 853 | goto err_unlock_ovs; |
893f139b | 854 | } |
03f0d916 | 855 | /* Check if this is a duplicate flow */ |
893f139b JR |
856 | flow = ovs_flow_tbl_lookup(&dp->table, &new_flow->unmasked_key); |
857 | if (likely(!flow)) { | |
858 | rcu_assign_pointer(new_flow->sf_acts, acts); | |
ccb1352e JG |
859 | |
860 | /* Put flow in bucket. */ | |
893f139b JR |
861 | error = ovs_flow_tbl_insert(&dp->table, new_flow, &mask); |
862 | if (unlikely(error)) { | |
618ed0c8 | 863 | acts = NULL; |
893f139b JR |
864 | goto err_unlock_ovs; |
865 | } | |
866 | ||
867 | if (unlikely(reply)) { | |
868 | error = ovs_flow_cmd_fill_info(new_flow, | |
869 | ovs_header->dp_ifindex, | |
870 | reply, info->snd_portid, | |
871 | info->snd_seq, 0, | |
872 | OVS_FLOW_CMD_NEW); | |
873 | BUG_ON(error < 0); | |
618ed0c8 | 874 | } |
893f139b | 875 | ovs_unlock(); |
ccb1352e | 876 | } else { |
37bdc87b JR |
877 | struct sw_flow_actions *old_acts; |
878 | ||
ccb1352e JG |
879 | /* Bail out if we're not allowed to modify an existing flow. |
880 | * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL | |
881 | * because Generic Netlink treats the latter as a dump | |
882 | * request. We also accept NLM_F_EXCL in case that bug ever | |
883 | * gets fixed. | |
884 | */ | |
893f139b JR |
885 | if (unlikely(info->nlhdr->nlmsg_flags & (NLM_F_CREATE |
886 | | NLM_F_EXCL))) { | |
887 | error = -EEXIST; | |
8e4e1713 | 888 | goto err_unlock_ovs; |
893f139b | 889 | } |
03f0d916 | 890 | /* The unmasked key has to be the same for flow updates. */ |
893f139b | 891 | if (unlikely(!ovs_flow_cmp_unmasked_key(flow, &match))) { |
4a46b24e AW |
892 | flow = ovs_flow_tbl_lookup_exact(&dp->table, &match); |
893 | if (!flow) { | |
894 | error = -ENOENT; | |
895 | goto err_unlock_ovs; | |
896 | } | |
893f139b | 897 | } |
37bdc87b JR |
898 | /* Update actions. */ |
899 | old_acts = ovsl_dereference(flow->sf_acts); | |
900 | rcu_assign_pointer(flow->sf_acts, acts); | |
37bdc87b | 901 | |
893f139b JR |
902 | if (unlikely(reply)) { |
903 | error = ovs_flow_cmd_fill_info(flow, | |
904 | ovs_header->dp_ifindex, | |
905 | reply, info->snd_portid, | |
906 | info->snd_seq, 0, | |
907 | OVS_FLOW_CMD_NEW); | |
908 | BUG_ON(error < 0); | |
909 | } | |
910 | ovs_unlock(); | |
37bdc87b | 911 | |
893f139b JR |
912 | ovs_nla_free_flow_actions(old_acts); |
913 | ovs_flow_free(new_flow, false); | |
37bdc87b | 914 | } |
893f139b JR |
915 | |
916 | if (reply) | |
917 | ovs_notify(&dp_flow_genl_family, reply, info); | |
37bdc87b JR |
918 | return 0; |
919 | ||
37bdc87b JR |
920 | err_unlock_ovs: |
921 | ovs_unlock(); | |
893f139b JR |
922 | kfree_skb(reply); |
923 | err_kfree_acts: | |
37bdc87b | 924 | kfree(acts); |
893f139b JR |
925 | err_kfree_flow: |
926 | ovs_flow_free(new_flow, false); | |
37bdc87b JR |
927 | error: |
928 | return error; | |
929 | } | |
ccb1352e | 930 | |
37bdc87b JR |
931 | static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info) |
932 | { | |
933 | struct nlattr **a = info->attrs; | |
934 | struct ovs_header *ovs_header = info->userhdr; | |
935 | struct sw_flow_key key, masked_key; | |
936 | struct sw_flow *flow; | |
937 | struct sw_flow_mask mask; | |
938 | struct sk_buff *reply = NULL; | |
939 | struct datapath *dp; | |
893f139b | 940 | struct sw_flow_actions *old_acts = NULL, *acts = NULL; |
37bdc87b JR |
941 | struct sw_flow_match match; |
942 | int error; | |
943 | ||
944 | /* Extract key. */ | |
945 | error = -EINVAL; | |
946 | if (!a[OVS_FLOW_ATTR_KEY]) | |
947 | goto error; | |
948 | ||
949 | ovs_match_init(&match, &key, &mask); | |
950 | error = ovs_nla_get_match(&match, | |
951 | a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]); | |
952 | if (error) | |
953 | goto error; | |
954 | ||
955 | /* Validate actions. */ | |
956 | if (a[OVS_FLOW_ATTR_ACTIONS]) { | |
957 | acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_FLOW_ATTR_ACTIONS])); | |
958 | error = PTR_ERR(acts); | |
959 | if (IS_ERR(acts)) | |
960 | goto error; | |
961 | ||
962 | ovs_flow_mask_key(&masked_key, &key, &mask); | |
963 | error = ovs_nla_copy_actions(a[OVS_FLOW_ATTR_ACTIONS], | |
964 | &masked_key, 0, &acts); | |
965 | if (error) { | |
966 | OVS_NLERR("Flow actions may not be safe on all matching packets.\n"); | |
893f139b JR |
967 | goto err_kfree_acts; |
968 | } | |
969 | } | |
970 | ||
971 | /* Can allocate before locking if have acts. */ | |
972 | if (acts) { | |
973 | reply = ovs_flow_cmd_alloc_info(acts, info, false); | |
974 | if (IS_ERR(reply)) { | |
975 | error = PTR_ERR(reply); | |
976 | goto err_kfree_acts; | |
be52c9e9 | 977 | } |
37bdc87b | 978 | } |
0e9796b4 | 979 | |
37bdc87b JR |
980 | ovs_lock(); |
981 | dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex); | |
893f139b JR |
982 | if (unlikely(!dp)) { |
983 | error = -ENODEV; | |
37bdc87b | 984 | goto err_unlock_ovs; |
893f139b | 985 | } |
37bdc87b | 986 | /* Check that the flow exists. */ |
4a46b24e | 987 | flow = ovs_flow_tbl_lookup_exact(&dp->table, &match); |
893f139b JR |
988 | if (unlikely(!flow)) { |
989 | error = -ENOENT; | |
37bdc87b | 990 | goto err_unlock_ovs; |
893f139b | 991 | } |
4a46b24e | 992 | |
37bdc87b | 993 | /* Update actions, if present. */ |
893f139b | 994 | if (likely(acts)) { |
37bdc87b JR |
995 | old_acts = ovsl_dereference(flow->sf_acts); |
996 | rcu_assign_pointer(flow->sf_acts, acts); | |
893f139b JR |
997 | |
998 | if (unlikely(reply)) { | |
999 | error = ovs_flow_cmd_fill_info(flow, | |
1000 | ovs_header->dp_ifindex, | |
1001 | reply, info->snd_portid, | |
1002 | info->snd_seq, 0, | |
1003 | OVS_FLOW_CMD_NEW); | |
1004 | BUG_ON(error < 0); | |
1005 | } | |
1006 | } else { | |
1007 | /* Could not alloc without acts before locking. */ | |
1008 | reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex, | |
1009 | info, OVS_FLOW_CMD_NEW, false); | |
1010 | if (unlikely(IS_ERR(reply))) { | |
1011 | error = PTR_ERR(reply); | |
1012 | goto err_unlock_ovs; | |
1013 | } | |
ccb1352e | 1014 | } |
37bdc87b | 1015 | |
37bdc87b JR |
1016 | /* Clear stats. */ |
1017 | if (a[OVS_FLOW_ATTR_CLEAR]) | |
1018 | ovs_flow_stats_clear(flow); | |
8e4e1713 | 1019 | ovs_unlock(); |
ccb1352e | 1020 | |
893f139b JR |
1021 | if (reply) |
1022 | ovs_notify(&dp_flow_genl_family, reply, info); | |
1023 | if (old_acts) | |
1024 | ovs_nla_free_flow_actions(old_acts); | |
fb5d1e9e | 1025 | |
ccb1352e JG |
1026 | return 0; |
1027 | ||
8e4e1713 PS |
1028 | err_unlock_ovs: |
1029 | ovs_unlock(); | |
893f139b JR |
1030 | kfree_skb(reply); |
1031 | err_kfree_acts: | |
74f84a57 | 1032 | kfree(acts); |
ccb1352e JG |
1033 | error: |
1034 | return error; | |
1035 | } | |
1036 | ||
1037 | static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info) | |
1038 | { | |
1039 | struct nlattr **a = info->attrs; | |
1040 | struct ovs_header *ovs_header = info->userhdr; | |
1041 | struct sw_flow_key key; | |
1042 | struct sk_buff *reply; | |
1043 | struct sw_flow *flow; | |
1044 | struct datapath *dp; | |
03f0d916 | 1045 | struct sw_flow_match match; |
ccb1352e | 1046 | int err; |
ccb1352e | 1047 | |
03f0d916 AZ |
1048 | if (!a[OVS_FLOW_ATTR_KEY]) { |
1049 | OVS_NLERR("Flow get message rejected, Key attribute missing.\n"); | |
ccb1352e | 1050 | return -EINVAL; |
03f0d916 AZ |
1051 | } |
1052 | ||
1053 | ovs_match_init(&match, &key, NULL); | |
23dabf88 | 1054 | err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL); |
ccb1352e JG |
1055 | if (err) |
1056 | return err; | |
1057 | ||
8e4e1713 | 1058 | ovs_lock(); |
46df7b81 | 1059 | dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex); |
8e4e1713 PS |
1060 | if (!dp) { |
1061 | err = -ENODEV; | |
1062 | goto unlock; | |
1063 | } | |
ccb1352e | 1064 | |
4a46b24e AW |
1065 | flow = ovs_flow_tbl_lookup_exact(&dp->table, &match); |
1066 | if (!flow) { | |
8e4e1713 PS |
1067 | err = -ENOENT; |
1068 | goto unlock; | |
1069 | } | |
ccb1352e | 1070 | |
0e9796b4 JR |
1071 | reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex, info, |
1072 | OVS_FLOW_CMD_NEW, true); | |
8e4e1713 PS |
1073 | if (IS_ERR(reply)) { |
1074 | err = PTR_ERR(reply); | |
1075 | goto unlock; | |
1076 | } | |
ccb1352e | 1077 | |
8e4e1713 | 1078 | ovs_unlock(); |
ccb1352e | 1079 | return genlmsg_reply(reply, info); |
8e4e1713 PS |
1080 | unlock: |
1081 | ovs_unlock(); | |
1082 | return err; | |
ccb1352e JG |
1083 | } |
1084 | ||
1085 | static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info) | |
1086 | { | |
1087 | struct nlattr **a = info->attrs; | |
1088 | struct ovs_header *ovs_header = info->userhdr; | |
1089 | struct sw_flow_key key; | |
1090 | struct sk_buff *reply; | |
1091 | struct sw_flow *flow; | |
1092 | struct datapath *dp; | |
03f0d916 | 1093 | struct sw_flow_match match; |
ccb1352e | 1094 | int err; |
ccb1352e | 1095 | |
aed06778 JR |
1096 | if (likely(a[OVS_FLOW_ATTR_KEY])) { |
1097 | ovs_match_init(&match, &key, NULL); | |
1098 | err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL); | |
1099 | if (unlikely(err)) | |
1100 | return err; | |
1101 | } | |
1102 | ||
8e4e1713 | 1103 | ovs_lock(); |
46df7b81 | 1104 | dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex); |
aed06778 | 1105 | if (unlikely(!dp)) { |
8e4e1713 PS |
1106 | err = -ENODEV; |
1107 | goto unlock; | |
1108 | } | |
46df7b81 | 1109 | |
aed06778 | 1110 | if (unlikely(!a[OVS_FLOW_ATTR_KEY])) { |
b637e498 | 1111 | err = ovs_flow_tbl_flush(&dp->table); |
8e4e1713 PS |
1112 | goto unlock; |
1113 | } | |
03f0d916 | 1114 | |
4a46b24e AW |
1115 | flow = ovs_flow_tbl_lookup_exact(&dp->table, &match); |
1116 | if (unlikely(!flow)) { | |
8e4e1713 PS |
1117 | err = -ENOENT; |
1118 | goto unlock; | |
1119 | } | |
ccb1352e | 1120 | |
b637e498 | 1121 | ovs_flow_tbl_remove(&dp->table, flow); |
aed06778 | 1122 | ovs_unlock(); |
ccb1352e | 1123 | |
aed06778 JR |
1124 | reply = ovs_flow_cmd_alloc_info((const struct sw_flow_actions __force *) flow->sf_acts, |
1125 | info, false); | |
1126 | if (likely(reply)) { | |
1127 | if (likely(!IS_ERR(reply))) { | |
1128 | rcu_read_lock(); /*To keep RCU checker happy. */ | |
1129 | err = ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex, | |
1130 | reply, info->snd_portid, | |
1131 | info->snd_seq, 0, | |
1132 | OVS_FLOW_CMD_DEL); | |
1133 | rcu_read_unlock(); | |
1134 | BUG_ON(err < 0); | |
1135 | ||
1136 | ovs_notify(&dp_flow_genl_family, reply, info); | |
1137 | } else { | |
1138 | netlink_set_err(sock_net(skb->sk)->genl_sock, 0, 0, PTR_ERR(reply)); | |
1139 | } | |
fb5d1e9e | 1140 | } |
ccb1352e | 1141 | |
aed06778 | 1142 | ovs_flow_free(flow, true); |
ccb1352e | 1143 | return 0; |
8e4e1713 PS |
1144 | unlock: |
1145 | ovs_unlock(); | |
1146 | return err; | |
ccb1352e JG |
1147 | } |
1148 | ||
1149 | static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb) | |
1150 | { | |
1151 | struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh)); | |
b637e498 | 1152 | struct table_instance *ti; |
ccb1352e | 1153 | struct datapath *dp; |
ccb1352e | 1154 | |
d57170b1 | 1155 | rcu_read_lock(); |
46df7b81 | 1156 | dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex); |
8e4e1713 | 1157 | if (!dp) { |
d57170b1 | 1158 | rcu_read_unlock(); |
ccb1352e | 1159 | return -ENODEV; |
8e4e1713 | 1160 | } |
ccb1352e | 1161 | |
b637e498 | 1162 | ti = rcu_dereference(dp->table.ti); |
ccb1352e JG |
1163 | for (;;) { |
1164 | struct sw_flow *flow; | |
1165 | u32 bucket, obj; | |
1166 | ||
1167 | bucket = cb->args[0]; | |
1168 | obj = cb->args[1]; | |
b637e498 | 1169 | flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj); |
ccb1352e JG |
1170 | if (!flow) |
1171 | break; | |
1172 | ||
0e9796b4 | 1173 | if (ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex, skb, |
15e47304 | 1174 | NETLINK_CB(cb->skb).portid, |
ccb1352e JG |
1175 | cb->nlh->nlmsg_seq, NLM_F_MULTI, |
1176 | OVS_FLOW_CMD_NEW) < 0) | |
1177 | break; | |
1178 | ||
1179 | cb->args[0] = bucket; | |
1180 | cb->args[1] = obj; | |
1181 | } | |
d57170b1 | 1182 | rcu_read_unlock(); |
ccb1352e JG |
1183 | return skb->len; |
1184 | } | |
1185 | ||
0c200ef9 PS |
1186 | static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = { |
1187 | [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED }, | |
1188 | [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED }, | |
1189 | [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG }, | |
1190 | }; | |
1191 | ||
1192 | static struct genl_ops dp_flow_genl_ops[] = { | |
ccb1352e JG |
1193 | { .cmd = OVS_FLOW_CMD_NEW, |
1194 | .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ | |
1195 | .policy = flow_policy, | |
37bdc87b | 1196 | .doit = ovs_flow_cmd_new |
ccb1352e JG |
1197 | }, |
1198 | { .cmd = OVS_FLOW_CMD_DEL, | |
1199 | .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ | |
1200 | .policy = flow_policy, | |
1201 | .doit = ovs_flow_cmd_del | |
1202 | }, | |
1203 | { .cmd = OVS_FLOW_CMD_GET, | |
1204 | .flags = 0, /* OK for unprivileged users. */ | |
1205 | .policy = flow_policy, | |
1206 | .doit = ovs_flow_cmd_get, | |
1207 | .dumpit = ovs_flow_cmd_dump | |
1208 | }, | |
1209 | { .cmd = OVS_FLOW_CMD_SET, | |
1210 | .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ | |
1211 | .policy = flow_policy, | |
37bdc87b | 1212 | .doit = ovs_flow_cmd_set, |
ccb1352e JG |
1213 | }, |
1214 | }; | |
1215 | ||
0c200ef9 | 1216 | static struct genl_family dp_flow_genl_family = { |
ccb1352e JG |
1217 | .id = GENL_ID_GENERATE, |
1218 | .hdrsize = sizeof(struct ovs_header), | |
0c200ef9 PS |
1219 | .name = OVS_FLOW_FAMILY, |
1220 | .version = OVS_FLOW_VERSION, | |
1221 | .maxattr = OVS_FLOW_ATTR_MAX, | |
3a4e0d6a PS |
1222 | .netnsok = true, |
1223 | .parallel_ops = true, | |
0c200ef9 PS |
1224 | .ops = dp_flow_genl_ops, |
1225 | .n_ops = ARRAY_SIZE(dp_flow_genl_ops), | |
1226 | .mcgrps = &ovs_dp_flow_multicast_group, | |
1227 | .n_mcgrps = 1, | |
ccb1352e JG |
1228 | }; |
1229 | ||
c3ff8cfe TG |
1230 | static size_t ovs_dp_cmd_msg_size(void) |
1231 | { | |
1232 | size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header)); | |
1233 | ||
1234 | msgsize += nla_total_size(IFNAMSIZ); | |
1235 | msgsize += nla_total_size(sizeof(struct ovs_dp_stats)); | |
1bd7116f | 1236 | msgsize += nla_total_size(sizeof(struct ovs_dp_megaflow_stats)); |
45fb9c35 | 1237 | msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_USER_FEATURES */ |
c3ff8cfe TG |
1238 | |
1239 | return msgsize; | |
1240 | } | |
1241 | ||
bb6f9a70 | 1242 | /* Called with ovs_mutex or RCU read lock. */ |
ccb1352e | 1243 | static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb, |
15e47304 | 1244 | u32 portid, u32 seq, u32 flags, u8 cmd) |
ccb1352e JG |
1245 | { |
1246 | struct ovs_header *ovs_header; | |
1247 | struct ovs_dp_stats dp_stats; | |
1bd7116f | 1248 | struct ovs_dp_megaflow_stats dp_megaflow_stats; |
ccb1352e JG |
1249 | int err; |
1250 | ||
15e47304 | 1251 | ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family, |
ccb1352e JG |
1252 | flags, cmd); |
1253 | if (!ovs_header) | |
1254 | goto error; | |
1255 | ||
1256 | ovs_header->dp_ifindex = get_dpifindex(dp); | |
1257 | ||
ccb1352e | 1258 | err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp)); |
ccb1352e JG |
1259 | if (err) |
1260 | goto nla_put_failure; | |
1261 | ||
1bd7116f AZ |
1262 | get_dp_stats(dp, &dp_stats, &dp_megaflow_stats); |
1263 | if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats), | |
1264 | &dp_stats)) | |
1265 | goto nla_put_failure; | |
1266 | ||
1267 | if (nla_put(skb, OVS_DP_ATTR_MEGAFLOW_STATS, | |
1268 | sizeof(struct ovs_dp_megaflow_stats), | |
1269 | &dp_megaflow_stats)) | |
028d6a67 | 1270 | goto nla_put_failure; |
ccb1352e | 1271 | |
43d4be9c TG |
1272 | if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, dp->user_features)) |
1273 | goto nla_put_failure; | |
1274 | ||
ccb1352e JG |
1275 | return genlmsg_end(skb, ovs_header); |
1276 | ||
1277 | nla_put_failure: | |
1278 | genlmsg_cancel(skb, ovs_header); | |
1279 | error: | |
1280 | return -EMSGSIZE; | |
1281 | } | |
1282 | ||
6093ae9a | 1283 | static struct sk_buff *ovs_dp_cmd_alloc_info(struct genl_info *info) |
ccb1352e | 1284 | { |
6093ae9a | 1285 | return genlmsg_new_unicast(ovs_dp_cmd_msg_size(), info, GFP_KERNEL); |
ccb1352e JG |
1286 | } |
1287 | ||
bb6f9a70 | 1288 | /* Called with rcu_read_lock or ovs_mutex. */ |
46df7b81 PS |
1289 | static struct datapath *lookup_datapath(struct net *net, |
1290 | struct ovs_header *ovs_header, | |
ccb1352e JG |
1291 | struct nlattr *a[OVS_DP_ATTR_MAX + 1]) |
1292 | { | |
1293 | struct datapath *dp; | |
1294 | ||
1295 | if (!a[OVS_DP_ATTR_NAME]) | |
46df7b81 | 1296 | dp = get_dp(net, ovs_header->dp_ifindex); |
ccb1352e JG |
1297 | else { |
1298 | struct vport *vport; | |
1299 | ||
46df7b81 | 1300 | vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME])); |
ccb1352e | 1301 | dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL; |
ccb1352e JG |
1302 | } |
1303 | return dp ? dp : ERR_PTR(-ENODEV); | |
1304 | } | |
1305 | ||
44da5ae5 TG |
1306 | static void ovs_dp_reset_user_features(struct sk_buff *skb, struct genl_info *info) |
1307 | { | |
1308 | struct datapath *dp; | |
1309 | ||
1310 | dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs); | |
3c7eacfc | 1311 | if (IS_ERR(dp)) |
44da5ae5 TG |
1312 | return; |
1313 | ||
1314 | WARN(dp->user_features, "Dropping previously announced user features\n"); | |
1315 | dp->user_features = 0; | |
1316 | } | |
1317 | ||
43d4be9c TG |
1318 | static void ovs_dp_change(struct datapath *dp, struct nlattr **a) |
1319 | { | |
1320 | if (a[OVS_DP_ATTR_USER_FEATURES]) | |
1321 | dp->user_features = nla_get_u32(a[OVS_DP_ATTR_USER_FEATURES]); | |
1322 | } | |
1323 | ||
ccb1352e JG |
1324 | static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info) |
1325 | { | |
1326 | struct nlattr **a = info->attrs; | |
1327 | struct vport_parms parms; | |
1328 | struct sk_buff *reply; | |
1329 | struct datapath *dp; | |
1330 | struct vport *vport; | |
46df7b81 | 1331 | struct ovs_net *ovs_net; |
15eac2a7 | 1332 | int err, i; |
ccb1352e JG |
1333 | |
1334 | err = -EINVAL; | |
1335 | if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID]) | |
1336 | goto err; | |
1337 | ||
6093ae9a JR |
1338 | reply = ovs_dp_cmd_alloc_info(info); |
1339 | if (!reply) | |
1340 | return -ENOMEM; | |
ccb1352e JG |
1341 | |
1342 | err = -ENOMEM; | |
1343 | dp = kzalloc(sizeof(*dp), GFP_KERNEL); | |
1344 | if (dp == NULL) | |
6093ae9a | 1345 | goto err_free_reply; |
46df7b81 | 1346 | |
46df7b81 | 1347 | ovs_dp_set_net(dp, hold_net(sock_net(skb->sk))); |
ccb1352e JG |
1348 | |
1349 | /* Allocate table. */ | |
b637e498 PS |
1350 | err = ovs_flow_tbl_init(&dp->table); |
1351 | if (err) | |
ccb1352e JG |
1352 | goto err_free_dp; |
1353 | ||
1c213bd2 | 1354 | dp->stats_percpu = netdev_alloc_pcpu_stats(struct dp_stats_percpu); |
ccb1352e JG |
1355 | if (!dp->stats_percpu) { |
1356 | err = -ENOMEM; | |
1357 | goto err_destroy_table; | |
1358 | } | |
1359 | ||
15eac2a7 | 1360 | dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head), |
e6445719 | 1361 | GFP_KERNEL); |
15eac2a7 PS |
1362 | if (!dp->ports) { |
1363 | err = -ENOMEM; | |
1364 | goto err_destroy_percpu; | |
1365 | } | |
1366 | ||
1367 | for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) | |
1368 | INIT_HLIST_HEAD(&dp->ports[i]); | |
1369 | ||
ccb1352e JG |
1370 | /* Set up our datapath device. */ |
1371 | parms.name = nla_data(a[OVS_DP_ATTR_NAME]); | |
1372 | parms.type = OVS_VPORT_TYPE_INTERNAL; | |
1373 | parms.options = NULL; | |
1374 | parms.dp = dp; | |
1375 | parms.port_no = OVSP_LOCAL; | |
15e47304 | 1376 | parms.upcall_portid = nla_get_u32(a[OVS_DP_ATTR_UPCALL_PID]); |
ccb1352e | 1377 | |
43d4be9c TG |
1378 | ovs_dp_change(dp, a); |
1379 | ||
6093ae9a JR |
1380 | /* So far only local changes have been made, now need the lock. */ |
1381 | ovs_lock(); | |
1382 | ||
ccb1352e JG |
1383 | vport = new_vport(&parms); |
1384 | if (IS_ERR(vport)) { | |
1385 | err = PTR_ERR(vport); | |
1386 | if (err == -EBUSY) | |
1387 | err = -EEXIST; | |
1388 | ||
44da5ae5 TG |
1389 | if (err == -EEXIST) { |
1390 | /* An outdated user space instance that does not understand | |
1391 | * the concept of user_features has attempted to create a new | |
1392 | * datapath and is likely to reuse it. Drop all user features. | |
1393 | */ | |
1394 | if (info->genlhdr->version < OVS_DP_VER_FEATURES) | |
1395 | ovs_dp_reset_user_features(skb, info); | |
1396 | } | |
1397 | ||
15eac2a7 | 1398 | goto err_destroy_ports_array; |
ccb1352e JG |
1399 | } |
1400 | ||
6093ae9a JR |
1401 | err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid, |
1402 | info->snd_seq, 0, OVS_DP_CMD_NEW); | |
1403 | BUG_ON(err < 0); | |
ccb1352e | 1404 | |
46df7b81 | 1405 | ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id); |
59a35d60 | 1406 | list_add_tail_rcu(&dp->list_node, &ovs_net->dps); |
8e4e1713 PS |
1407 | |
1408 | ovs_unlock(); | |
ccb1352e | 1409 | |
2a94fe48 | 1410 | ovs_notify(&dp_datapath_genl_family, reply, info); |
ccb1352e JG |
1411 | return 0; |
1412 | ||
15eac2a7 | 1413 | err_destroy_ports_array: |
6093ae9a | 1414 | ovs_unlock(); |
15eac2a7 | 1415 | kfree(dp->ports); |
ccb1352e JG |
1416 | err_destroy_percpu: |
1417 | free_percpu(dp->stats_percpu); | |
1418 | err_destroy_table: | |
e80857cc | 1419 | ovs_flow_tbl_destroy(&dp->table, false); |
ccb1352e | 1420 | err_free_dp: |
46df7b81 | 1421 | release_net(ovs_dp_get_net(dp)); |
ccb1352e | 1422 | kfree(dp); |
6093ae9a JR |
1423 | err_free_reply: |
1424 | kfree_skb(reply); | |
ccb1352e JG |
1425 | err: |
1426 | return err; | |
1427 | } | |
1428 | ||
8e4e1713 | 1429 | /* Called with ovs_mutex. */ |
46df7b81 | 1430 | static void __dp_destroy(struct datapath *dp) |
ccb1352e | 1431 | { |
15eac2a7 | 1432 | int i; |
ccb1352e | 1433 | |
15eac2a7 PS |
1434 | for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) { |
1435 | struct vport *vport; | |
b67bfe0d | 1436 | struct hlist_node *n; |
15eac2a7 | 1437 | |
b67bfe0d | 1438 | hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node) |
15eac2a7 PS |
1439 | if (vport->port_no != OVSP_LOCAL) |
1440 | ovs_dp_detach_port(vport); | |
1441 | } | |
ccb1352e | 1442 | |
59a35d60 | 1443 | list_del_rcu(&dp->list_node); |
ccb1352e | 1444 | |
8e4e1713 | 1445 | /* OVSP_LOCAL is datapath internal port. We need to make sure that |
e80857cc | 1446 | * all ports in datapath are destroyed first before freeing datapath. |
ccb1352e | 1447 | */ |
8e4e1713 | 1448 | ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL)); |
ccb1352e | 1449 | |
e80857cc AZ |
1450 | /* RCU destroy the flow table */ |
1451 | ovs_flow_tbl_destroy(&dp->table, true); | |
1452 | ||
ccb1352e | 1453 | call_rcu(&dp->rcu, destroy_dp_rcu); |
46df7b81 PS |
1454 | } |
1455 | ||
1456 | static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info) | |
1457 | { | |
1458 | struct sk_buff *reply; | |
1459 | struct datapath *dp; | |
1460 | int err; | |
1461 | ||
6093ae9a JR |
1462 | reply = ovs_dp_cmd_alloc_info(info); |
1463 | if (!reply) | |
1464 | return -ENOMEM; | |
1465 | ||
8e4e1713 | 1466 | ovs_lock(); |
46df7b81 PS |
1467 | dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs); |
1468 | err = PTR_ERR(dp); | |
1469 | if (IS_ERR(dp)) | |
6093ae9a | 1470 | goto err_unlock_free; |
46df7b81 | 1471 | |
6093ae9a JR |
1472 | err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid, |
1473 | info->snd_seq, 0, OVS_DP_CMD_DEL); | |
1474 | BUG_ON(err < 0); | |
46df7b81 PS |
1475 | |
1476 | __dp_destroy(dp); | |
8e4e1713 | 1477 | ovs_unlock(); |
ccb1352e | 1478 | |
2a94fe48 | 1479 | ovs_notify(&dp_datapath_genl_family, reply, info); |
ccb1352e JG |
1480 | |
1481 | return 0; | |
6093ae9a JR |
1482 | |
1483 | err_unlock_free: | |
8e4e1713 | 1484 | ovs_unlock(); |
6093ae9a | 1485 | kfree_skb(reply); |
8e4e1713 | 1486 | return err; |
ccb1352e JG |
1487 | } |
1488 | ||
1489 | static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info) | |
1490 | { | |
1491 | struct sk_buff *reply; | |
1492 | struct datapath *dp; | |
1493 | int err; | |
1494 | ||
6093ae9a JR |
1495 | reply = ovs_dp_cmd_alloc_info(info); |
1496 | if (!reply) | |
1497 | return -ENOMEM; | |
1498 | ||
8e4e1713 | 1499 | ovs_lock(); |
46df7b81 | 1500 | dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs); |
8e4e1713 | 1501 | err = PTR_ERR(dp); |
ccb1352e | 1502 | if (IS_ERR(dp)) |
6093ae9a | 1503 | goto err_unlock_free; |
ccb1352e | 1504 | |
43d4be9c TG |
1505 | ovs_dp_change(dp, info->attrs); |
1506 | ||
6093ae9a JR |
1507 | err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid, |
1508 | info->snd_seq, 0, OVS_DP_CMD_NEW); | |
1509 | BUG_ON(err < 0); | |
ccb1352e | 1510 | |
8e4e1713 | 1511 | ovs_unlock(); |
2a94fe48 | 1512 | ovs_notify(&dp_datapath_genl_family, reply, info); |
ccb1352e JG |
1513 | |
1514 | return 0; | |
6093ae9a JR |
1515 | |
1516 | err_unlock_free: | |
8e4e1713 | 1517 | ovs_unlock(); |
6093ae9a | 1518 | kfree_skb(reply); |
8e4e1713 | 1519 | return err; |
ccb1352e JG |
1520 | } |
1521 | ||
1522 | static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info) | |
1523 | { | |
1524 | struct sk_buff *reply; | |
1525 | struct datapath *dp; | |
8e4e1713 | 1526 | int err; |
ccb1352e | 1527 | |
6093ae9a JR |
1528 | reply = ovs_dp_cmd_alloc_info(info); |
1529 | if (!reply) | |
1530 | return -ENOMEM; | |
1531 | ||
1532 | rcu_read_lock(); | |
46df7b81 | 1533 | dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs); |
8e4e1713 PS |
1534 | if (IS_ERR(dp)) { |
1535 | err = PTR_ERR(dp); | |
6093ae9a | 1536 | goto err_unlock_free; |
8e4e1713 | 1537 | } |
6093ae9a JR |
1538 | err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid, |
1539 | info->snd_seq, 0, OVS_DP_CMD_NEW); | |
1540 | BUG_ON(err < 0); | |
1541 | rcu_read_unlock(); | |
ccb1352e JG |
1542 | |
1543 | return genlmsg_reply(reply, info); | |
8e4e1713 | 1544 | |
6093ae9a JR |
1545 | err_unlock_free: |
1546 | rcu_read_unlock(); | |
1547 | kfree_skb(reply); | |
8e4e1713 | 1548 | return err; |
ccb1352e JG |
1549 | } |
1550 | ||
1551 | static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb) | |
1552 | { | |
46df7b81 | 1553 | struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id); |
ccb1352e JG |
1554 | struct datapath *dp; |
1555 | int skip = cb->args[0]; | |
1556 | int i = 0; | |
1557 | ||
59a35d60 PS |
1558 | rcu_read_lock(); |
1559 | list_for_each_entry_rcu(dp, &ovs_net->dps, list_node) { | |
77676fdb | 1560 | if (i >= skip && |
15e47304 | 1561 | ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid, |
ccb1352e JG |
1562 | cb->nlh->nlmsg_seq, NLM_F_MULTI, |
1563 | OVS_DP_CMD_NEW) < 0) | |
1564 | break; | |
1565 | i++; | |
1566 | } | |
59a35d60 | 1567 | rcu_read_unlock(); |
ccb1352e JG |
1568 | |
1569 | cb->args[0] = i; | |
1570 | ||
1571 | return skb->len; | |
1572 | } | |
1573 | ||
0c200ef9 PS |
1574 | static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = { |
1575 | [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 }, | |
1576 | [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 }, | |
1577 | [OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 }, | |
1578 | }; | |
1579 | ||
1580 | static struct genl_ops dp_datapath_genl_ops[] = { | |
ccb1352e JG |
1581 | { .cmd = OVS_DP_CMD_NEW, |
1582 | .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ | |
1583 | .policy = datapath_policy, | |
1584 | .doit = ovs_dp_cmd_new | |
1585 | }, | |
1586 | { .cmd = OVS_DP_CMD_DEL, | |
1587 | .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ | |
1588 | .policy = datapath_policy, | |
1589 | .doit = ovs_dp_cmd_del | |
1590 | }, | |
1591 | { .cmd = OVS_DP_CMD_GET, | |
1592 | .flags = 0, /* OK for unprivileged users. */ | |
1593 | .policy = datapath_policy, | |
1594 | .doit = ovs_dp_cmd_get, | |
1595 | .dumpit = ovs_dp_cmd_dump | |
1596 | }, | |
1597 | { .cmd = OVS_DP_CMD_SET, | |
1598 | .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ | |
1599 | .policy = datapath_policy, | |
1600 | .doit = ovs_dp_cmd_set, | |
1601 | }, | |
1602 | }; | |
1603 | ||
0c200ef9 | 1604 | static struct genl_family dp_datapath_genl_family = { |
ccb1352e JG |
1605 | .id = GENL_ID_GENERATE, |
1606 | .hdrsize = sizeof(struct ovs_header), | |
0c200ef9 PS |
1607 | .name = OVS_DATAPATH_FAMILY, |
1608 | .version = OVS_DATAPATH_VERSION, | |
1609 | .maxattr = OVS_DP_ATTR_MAX, | |
3a4e0d6a PS |
1610 | .netnsok = true, |
1611 | .parallel_ops = true, | |
0c200ef9 PS |
1612 | .ops = dp_datapath_genl_ops, |
1613 | .n_ops = ARRAY_SIZE(dp_datapath_genl_ops), | |
1614 | .mcgrps = &ovs_dp_datapath_multicast_group, | |
1615 | .n_mcgrps = 1, | |
ccb1352e JG |
1616 | }; |
1617 | ||
8e4e1713 | 1618 | /* Called with ovs_mutex or RCU read lock. */ |
ccb1352e | 1619 | static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb, |
15e47304 | 1620 | u32 portid, u32 seq, u32 flags, u8 cmd) |
ccb1352e JG |
1621 | { |
1622 | struct ovs_header *ovs_header; | |
1623 | struct ovs_vport_stats vport_stats; | |
1624 | int err; | |
1625 | ||
15e47304 | 1626 | ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family, |
ccb1352e JG |
1627 | flags, cmd); |
1628 | if (!ovs_header) | |
1629 | return -EMSGSIZE; | |
1630 | ||
1631 | ovs_header->dp_ifindex = get_dpifindex(vport->dp); | |
1632 | ||
028d6a67 DM |
1633 | if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) || |
1634 | nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) || | |
1635 | nla_put_string(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport)) || | |
15e47304 | 1636 | nla_put_u32(skb, OVS_VPORT_ATTR_UPCALL_PID, vport->upcall_portid)) |
028d6a67 | 1637 | goto nla_put_failure; |
ccb1352e JG |
1638 | |
1639 | ovs_vport_get_stats(vport, &vport_stats); | |
028d6a67 DM |
1640 | if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats), |
1641 | &vport_stats)) | |
1642 | goto nla_put_failure; | |
ccb1352e JG |
1643 | |
1644 | err = ovs_vport_get_options(vport, skb); | |
1645 | if (err == -EMSGSIZE) | |
1646 | goto error; | |
1647 | ||
1648 | return genlmsg_end(skb, ovs_header); | |
1649 | ||
1650 | nla_put_failure: | |
1651 | err = -EMSGSIZE; | |
1652 | error: | |
1653 | genlmsg_cancel(skb, ovs_header); | |
1654 | return err; | |
1655 | } | |
1656 | ||
6093ae9a JR |
1657 | static struct sk_buff *ovs_vport_cmd_alloc_info(void) |
1658 | { | |
1659 | return nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); | |
1660 | } | |
1661 | ||
1662 | /* Called with ovs_mutex, only via ovs_dp_notify_wq(). */ | |
15e47304 | 1663 | struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid, |
ccb1352e JG |
1664 | u32 seq, u8 cmd) |
1665 | { | |
1666 | struct sk_buff *skb; | |
1667 | int retval; | |
1668 | ||
1669 | skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC); | |
1670 | if (!skb) | |
1671 | return ERR_PTR(-ENOMEM); | |
1672 | ||
15e47304 | 1673 | retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd); |
a9341512 JG |
1674 | BUG_ON(retval < 0); |
1675 | ||
ccb1352e JG |
1676 | return skb; |
1677 | } | |
1678 | ||
8e4e1713 | 1679 | /* Called with ovs_mutex or RCU read lock. */ |
46df7b81 PS |
1680 | static struct vport *lookup_vport(struct net *net, |
1681 | struct ovs_header *ovs_header, | |
ccb1352e JG |
1682 | struct nlattr *a[OVS_VPORT_ATTR_MAX + 1]) |
1683 | { | |
1684 | struct datapath *dp; | |
1685 | struct vport *vport; | |
1686 | ||
1687 | if (a[OVS_VPORT_ATTR_NAME]) { | |
46df7b81 | 1688 | vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME])); |
ccb1352e JG |
1689 | if (!vport) |
1690 | return ERR_PTR(-ENODEV); | |
651a68ea BP |
1691 | if (ovs_header->dp_ifindex && |
1692 | ovs_header->dp_ifindex != get_dpifindex(vport->dp)) | |
1693 | return ERR_PTR(-ENODEV); | |
ccb1352e JG |
1694 | return vport; |
1695 | } else if (a[OVS_VPORT_ATTR_PORT_NO]) { | |
1696 | u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]); | |
1697 | ||
1698 | if (port_no >= DP_MAX_PORTS) | |
1699 | return ERR_PTR(-EFBIG); | |
1700 | ||
46df7b81 | 1701 | dp = get_dp(net, ovs_header->dp_ifindex); |
ccb1352e JG |
1702 | if (!dp) |
1703 | return ERR_PTR(-ENODEV); | |
1704 | ||
8e4e1713 | 1705 | vport = ovs_vport_ovsl_rcu(dp, port_no); |
ccb1352e | 1706 | if (!vport) |
14408dba | 1707 | return ERR_PTR(-ENODEV); |
ccb1352e JG |
1708 | return vport; |
1709 | } else | |
1710 | return ERR_PTR(-EINVAL); | |
1711 | } | |
1712 | ||
1713 | static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info) | |
1714 | { | |
1715 | struct nlattr **a = info->attrs; | |
1716 | struct ovs_header *ovs_header = info->userhdr; | |
1717 | struct vport_parms parms; | |
1718 | struct sk_buff *reply; | |
1719 | struct vport *vport; | |
1720 | struct datapath *dp; | |
1721 | u32 port_no; | |
1722 | int err; | |
1723 | ||
ccb1352e JG |
1724 | if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] || |
1725 | !a[OVS_VPORT_ATTR_UPCALL_PID]) | |
6093ae9a JR |
1726 | return -EINVAL; |
1727 | ||
1728 | port_no = a[OVS_VPORT_ATTR_PORT_NO] | |
1729 | ? nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]) : 0; | |
1730 | if (port_no >= DP_MAX_PORTS) | |
1731 | return -EFBIG; | |
1732 | ||
1733 | reply = ovs_vport_cmd_alloc_info(); | |
1734 | if (!reply) | |
1735 | return -ENOMEM; | |
ccb1352e | 1736 | |
8e4e1713 | 1737 | ovs_lock(); |
46df7b81 | 1738 | dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex); |
ccb1352e JG |
1739 | err = -ENODEV; |
1740 | if (!dp) | |
6093ae9a | 1741 | goto exit_unlock_free; |
ccb1352e | 1742 | |
6093ae9a | 1743 | if (port_no) { |
8e4e1713 | 1744 | vport = ovs_vport_ovsl(dp, port_no); |
ccb1352e JG |
1745 | err = -EBUSY; |
1746 | if (vport) | |
6093ae9a | 1747 | goto exit_unlock_free; |
ccb1352e JG |
1748 | } else { |
1749 | for (port_no = 1; ; port_no++) { | |
1750 | if (port_no >= DP_MAX_PORTS) { | |
1751 | err = -EFBIG; | |
6093ae9a | 1752 | goto exit_unlock_free; |
ccb1352e | 1753 | } |
8e4e1713 | 1754 | vport = ovs_vport_ovsl(dp, port_no); |
ccb1352e JG |
1755 | if (!vport) |
1756 | break; | |
1757 | } | |
1758 | } | |
1759 | ||
1760 | parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]); | |
1761 | parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]); | |
1762 | parms.options = a[OVS_VPORT_ATTR_OPTIONS]; | |
1763 | parms.dp = dp; | |
1764 | parms.port_no = port_no; | |
15e47304 | 1765 | parms.upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]); |
ccb1352e JG |
1766 | |
1767 | vport = new_vport(&parms); | |
1768 | err = PTR_ERR(vport); | |
1769 | if (IS_ERR(vport)) | |
6093ae9a | 1770 | goto exit_unlock_free; |
ccb1352e | 1771 | |
6093ae9a JR |
1772 | err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid, |
1773 | info->snd_seq, 0, OVS_VPORT_CMD_NEW); | |
1774 | BUG_ON(err < 0); | |
1775 | ovs_unlock(); | |
ed661185 | 1776 | |
2a94fe48 | 1777 | ovs_notify(&dp_vport_genl_family, reply, info); |
6093ae9a | 1778 | return 0; |
ccb1352e | 1779 | |
6093ae9a | 1780 | exit_unlock_free: |
8e4e1713 | 1781 | ovs_unlock(); |
6093ae9a | 1782 | kfree_skb(reply); |
ccb1352e JG |
1783 | return err; |
1784 | } | |
1785 | ||
1786 | static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info) | |
1787 | { | |
1788 | struct nlattr **a = info->attrs; | |
1789 | struct sk_buff *reply; | |
1790 | struct vport *vport; | |
1791 | int err; | |
1792 | ||
6093ae9a JR |
1793 | reply = ovs_vport_cmd_alloc_info(); |
1794 | if (!reply) | |
1795 | return -ENOMEM; | |
1796 | ||
8e4e1713 | 1797 | ovs_lock(); |
46df7b81 | 1798 | vport = lookup_vport(sock_net(skb->sk), info->userhdr, a); |
ccb1352e JG |
1799 | err = PTR_ERR(vport); |
1800 | if (IS_ERR(vport)) | |
6093ae9a | 1801 | goto exit_unlock_free; |
ccb1352e | 1802 | |
ccb1352e | 1803 | if (a[OVS_VPORT_ATTR_TYPE] && |
f44f3408 | 1804 | nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) { |
ccb1352e | 1805 | err = -EINVAL; |
6093ae9a | 1806 | goto exit_unlock_free; |
a9341512 JG |
1807 | } |
1808 | ||
f44f3408 | 1809 | if (a[OVS_VPORT_ATTR_OPTIONS]) { |
ccb1352e | 1810 | err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]); |
f44f3408 | 1811 | if (err) |
6093ae9a | 1812 | goto exit_unlock_free; |
f44f3408 | 1813 | } |
a9341512 | 1814 | |
03fbf8b3 | 1815 | if (a[OVS_VPORT_ATTR_UPCALL_PID]) |
15e47304 | 1816 | vport->upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]); |
ccb1352e | 1817 | |
a9341512 JG |
1818 | err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid, |
1819 | info->snd_seq, 0, OVS_VPORT_CMD_NEW); | |
1820 | BUG_ON(err < 0); | |
ccb1352e | 1821 | |
8e4e1713 | 1822 | ovs_unlock(); |
2a94fe48 | 1823 | ovs_notify(&dp_vport_genl_family, reply, info); |
8e4e1713 | 1824 | return 0; |
ccb1352e | 1825 | |
6093ae9a | 1826 | exit_unlock_free: |
8e4e1713 | 1827 | ovs_unlock(); |
6093ae9a | 1828 | kfree_skb(reply); |
ccb1352e JG |
1829 | return err; |
1830 | } | |
1831 | ||
1832 | static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info) | |
1833 | { | |
1834 | struct nlattr **a = info->attrs; | |
1835 | struct sk_buff *reply; | |
1836 | struct vport *vport; | |
1837 | int err; | |
1838 | ||
6093ae9a JR |
1839 | reply = ovs_vport_cmd_alloc_info(); |
1840 | if (!reply) | |
1841 | return -ENOMEM; | |
1842 | ||
8e4e1713 | 1843 | ovs_lock(); |
46df7b81 | 1844 | vport = lookup_vport(sock_net(skb->sk), info->userhdr, a); |
ccb1352e JG |
1845 | err = PTR_ERR(vport); |
1846 | if (IS_ERR(vport)) | |
6093ae9a | 1847 | goto exit_unlock_free; |
ccb1352e JG |
1848 | |
1849 | if (vport->port_no == OVSP_LOCAL) { | |
1850 | err = -EINVAL; | |
6093ae9a | 1851 | goto exit_unlock_free; |
ccb1352e JG |
1852 | } |
1853 | ||
6093ae9a JR |
1854 | err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid, |
1855 | info->snd_seq, 0, OVS_VPORT_CMD_DEL); | |
1856 | BUG_ON(err < 0); | |
ccb1352e | 1857 | ovs_dp_detach_port(vport); |
6093ae9a | 1858 | ovs_unlock(); |
ccb1352e | 1859 | |
2a94fe48 | 1860 | ovs_notify(&dp_vport_genl_family, reply, info); |
6093ae9a | 1861 | return 0; |
ccb1352e | 1862 | |
6093ae9a | 1863 | exit_unlock_free: |
8e4e1713 | 1864 | ovs_unlock(); |
6093ae9a | 1865 | kfree_skb(reply); |
ccb1352e JG |
1866 | return err; |
1867 | } | |
1868 | ||
1869 | static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info) | |
1870 | { | |
1871 | struct nlattr **a = info->attrs; | |
1872 | struct ovs_header *ovs_header = info->userhdr; | |
1873 | struct sk_buff *reply; | |
1874 | struct vport *vport; | |
1875 | int err; | |
1876 | ||
6093ae9a JR |
1877 | reply = ovs_vport_cmd_alloc_info(); |
1878 | if (!reply) | |
1879 | return -ENOMEM; | |
1880 | ||
ccb1352e | 1881 | rcu_read_lock(); |
46df7b81 | 1882 | vport = lookup_vport(sock_net(skb->sk), ovs_header, a); |
ccb1352e JG |
1883 | err = PTR_ERR(vport); |
1884 | if (IS_ERR(vport)) | |
6093ae9a JR |
1885 | goto exit_unlock_free; |
1886 | err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid, | |
1887 | info->snd_seq, 0, OVS_VPORT_CMD_NEW); | |
1888 | BUG_ON(err < 0); | |
ccb1352e JG |
1889 | rcu_read_unlock(); |
1890 | ||
1891 | return genlmsg_reply(reply, info); | |
1892 | ||
6093ae9a | 1893 | exit_unlock_free: |
ccb1352e | 1894 | rcu_read_unlock(); |
6093ae9a | 1895 | kfree_skb(reply); |
ccb1352e JG |
1896 | return err; |
1897 | } | |
1898 | ||
1899 | static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb) | |
1900 | { | |
1901 | struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh)); | |
1902 | struct datapath *dp; | |
15eac2a7 PS |
1903 | int bucket = cb->args[0], skip = cb->args[1]; |
1904 | int i, j = 0; | |
ccb1352e | 1905 | |
42ee19e2 | 1906 | rcu_read_lock(); |
46df7b81 | 1907 | dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex); |
42ee19e2 JR |
1908 | if (!dp) { |
1909 | rcu_read_unlock(); | |
ccb1352e | 1910 | return -ENODEV; |
42ee19e2 | 1911 | } |
15eac2a7 | 1912 | for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) { |
ccb1352e | 1913 | struct vport *vport; |
15eac2a7 PS |
1914 | |
1915 | j = 0; | |
b67bfe0d | 1916 | hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) { |
15eac2a7 PS |
1917 | if (j >= skip && |
1918 | ovs_vport_cmd_fill_info(vport, skb, | |
15e47304 | 1919 | NETLINK_CB(cb->skb).portid, |
15eac2a7 PS |
1920 | cb->nlh->nlmsg_seq, |
1921 | NLM_F_MULTI, | |
1922 | OVS_VPORT_CMD_NEW) < 0) | |
1923 | goto out; | |
1924 | ||
1925 | j++; | |
1926 | } | |
1927 | skip = 0; | |
ccb1352e | 1928 | } |
15eac2a7 | 1929 | out: |
ccb1352e JG |
1930 | rcu_read_unlock(); |
1931 | ||
15eac2a7 PS |
1932 | cb->args[0] = i; |
1933 | cb->args[1] = j; | |
ccb1352e | 1934 | |
15eac2a7 | 1935 | return skb->len; |
ccb1352e JG |
1936 | } |
1937 | ||
0c200ef9 PS |
1938 | static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = { |
1939 | [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 }, | |
1940 | [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) }, | |
1941 | [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 }, | |
1942 | [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 }, | |
1943 | [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 }, | |
1944 | [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED }, | |
1945 | }; | |
1946 | ||
1947 | static struct genl_ops dp_vport_genl_ops[] = { | |
ccb1352e JG |
1948 | { .cmd = OVS_VPORT_CMD_NEW, |
1949 | .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ | |
1950 | .policy = vport_policy, | |
1951 | .doit = ovs_vport_cmd_new | |
1952 | }, | |
1953 | { .cmd = OVS_VPORT_CMD_DEL, | |
1954 | .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ | |
1955 | .policy = vport_policy, | |
1956 | .doit = ovs_vport_cmd_del | |
1957 | }, | |
1958 | { .cmd = OVS_VPORT_CMD_GET, | |
1959 | .flags = 0, /* OK for unprivileged users. */ | |
1960 | .policy = vport_policy, | |
1961 | .doit = ovs_vport_cmd_get, | |
1962 | .dumpit = ovs_vport_cmd_dump | |
1963 | }, | |
1964 | { .cmd = OVS_VPORT_CMD_SET, | |
1965 | .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ | |
1966 | .policy = vport_policy, | |
1967 | .doit = ovs_vport_cmd_set, | |
1968 | }, | |
1969 | }; | |
1970 | ||
0c200ef9 PS |
1971 | struct genl_family dp_vport_genl_family = { |
1972 | .id = GENL_ID_GENERATE, | |
1973 | .hdrsize = sizeof(struct ovs_header), | |
1974 | .name = OVS_VPORT_FAMILY, | |
1975 | .version = OVS_VPORT_VERSION, | |
1976 | .maxattr = OVS_VPORT_ATTR_MAX, | |
1977 | .netnsok = true, | |
1978 | .parallel_ops = true, | |
1979 | .ops = dp_vport_genl_ops, | |
1980 | .n_ops = ARRAY_SIZE(dp_vport_genl_ops), | |
1981 | .mcgrps = &ovs_dp_vport_multicast_group, | |
1982 | .n_mcgrps = 1, | |
ccb1352e JG |
1983 | }; |
1984 | ||
0c200ef9 PS |
1985 | static struct genl_family * const dp_genl_families[] = { |
1986 | &dp_datapath_genl_family, | |
1987 | &dp_vport_genl_family, | |
1988 | &dp_flow_genl_family, | |
1989 | &dp_packet_genl_family, | |
ccb1352e JG |
1990 | }; |
1991 | ||
1992 | static void dp_unregister_genl(int n_families) | |
1993 | { | |
1994 | int i; | |
1995 | ||
1996 | for (i = 0; i < n_families; i++) | |
0c200ef9 | 1997 | genl_unregister_family(dp_genl_families[i]); |
ccb1352e JG |
1998 | } |
1999 | ||
2000 | static int dp_register_genl(void) | |
2001 | { | |
ccb1352e JG |
2002 | int err; |
2003 | int i; | |
2004 | ||
ccb1352e | 2005 | for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) { |
ccb1352e | 2006 | |
0c200ef9 | 2007 | err = genl_register_family(dp_genl_families[i]); |
ccb1352e JG |
2008 | if (err) |
2009 | goto error; | |
ccb1352e JG |
2010 | } |
2011 | ||
2012 | return 0; | |
2013 | ||
2014 | error: | |
0c200ef9 | 2015 | dp_unregister_genl(i); |
ccb1352e JG |
2016 | return err; |
2017 | } | |
2018 | ||
46df7b81 PS |
2019 | static int __net_init ovs_init_net(struct net *net) |
2020 | { | |
2021 | struct ovs_net *ovs_net = net_generic(net, ovs_net_id); | |
2022 | ||
2023 | INIT_LIST_HEAD(&ovs_net->dps); | |
8e4e1713 | 2024 | INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq); |
46df7b81 PS |
2025 | return 0; |
2026 | } | |
2027 | ||
2028 | static void __net_exit ovs_exit_net(struct net *net) | |
2029 | { | |
46df7b81 | 2030 | struct datapath *dp, *dp_next; |
8e4e1713 | 2031 | struct ovs_net *ovs_net = net_generic(net, ovs_net_id); |
46df7b81 | 2032 | |
8e4e1713 | 2033 | ovs_lock(); |
46df7b81 PS |
2034 | list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node) |
2035 | __dp_destroy(dp); | |
8e4e1713 PS |
2036 | ovs_unlock(); |
2037 | ||
2038 | cancel_work_sync(&ovs_net->dp_notify_work); | |
46df7b81 PS |
2039 | } |
2040 | ||
2041 | static struct pernet_operations ovs_net_ops = { | |
2042 | .init = ovs_init_net, | |
2043 | .exit = ovs_exit_net, | |
2044 | .id = &ovs_net_id, | |
2045 | .size = sizeof(struct ovs_net), | |
2046 | }; | |
2047 | ||
ccb1352e JG |
2048 | static int __init dp_init(void) |
2049 | { | |
ccb1352e JG |
2050 | int err; |
2051 | ||
3523b29b | 2052 | BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb)); |
ccb1352e JG |
2053 | |
2054 | pr_info("Open vSwitch switching datapath\n"); | |
2055 | ||
5b9e7e16 | 2056 | err = ovs_internal_dev_rtnl_link_register(); |
ccb1352e JG |
2057 | if (err) |
2058 | goto error; | |
2059 | ||
5b9e7e16 JP |
2060 | err = ovs_flow_init(); |
2061 | if (err) | |
2062 | goto error_unreg_rtnl_link; | |
2063 | ||
ccb1352e JG |
2064 | err = ovs_vport_init(); |
2065 | if (err) | |
2066 | goto error_flow_exit; | |
2067 | ||
46df7b81 | 2068 | err = register_pernet_device(&ovs_net_ops); |
ccb1352e JG |
2069 | if (err) |
2070 | goto error_vport_exit; | |
2071 | ||
46df7b81 PS |
2072 | err = register_netdevice_notifier(&ovs_dp_device_notifier); |
2073 | if (err) | |
2074 | goto error_netns_exit; | |
2075 | ||
ccb1352e JG |
2076 | err = dp_register_genl(); |
2077 | if (err < 0) | |
2078 | goto error_unreg_notifier; | |
2079 | ||
ccb1352e JG |
2080 | return 0; |
2081 | ||
2082 | error_unreg_notifier: | |
2083 | unregister_netdevice_notifier(&ovs_dp_device_notifier); | |
46df7b81 PS |
2084 | error_netns_exit: |
2085 | unregister_pernet_device(&ovs_net_ops); | |
ccb1352e JG |
2086 | error_vport_exit: |
2087 | ovs_vport_exit(); | |
2088 | error_flow_exit: | |
2089 | ovs_flow_exit(); | |
5b9e7e16 JP |
2090 | error_unreg_rtnl_link: |
2091 | ovs_internal_dev_rtnl_link_unregister(); | |
ccb1352e JG |
2092 | error: |
2093 | return err; | |
2094 | } | |
2095 | ||
2096 | static void dp_cleanup(void) | |
2097 | { | |
ccb1352e JG |
2098 | dp_unregister_genl(ARRAY_SIZE(dp_genl_families)); |
2099 | unregister_netdevice_notifier(&ovs_dp_device_notifier); | |
46df7b81 PS |
2100 | unregister_pernet_device(&ovs_net_ops); |
2101 | rcu_barrier(); | |
ccb1352e JG |
2102 | ovs_vport_exit(); |
2103 | ovs_flow_exit(); | |
5b9e7e16 | 2104 | ovs_internal_dev_rtnl_link_unregister(); |
ccb1352e JG |
2105 | } |
2106 | ||
2107 | module_init(dp_init); | |
2108 | module_exit(dp_cleanup); | |
2109 | ||
2110 | MODULE_DESCRIPTION("Open vSwitch switching datapath"); | |
2111 | MODULE_LICENSE("GPL"); |