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