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