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