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