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