]>
Commit | Line | Data |
---|---|---|
064af421 | 1 | /* |
e0edde6f | 2 | * Copyright (c) 2007-2012 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> |
36956a7d | 51 | #include <net/genetlink.h> |
2a4999f3 PS |
52 | #include <net/net_namespace.h> |
53 | #include <net/netns/generic.h> | |
064af421 | 54 | |
dd8d6b8c | 55 | #include "checksum.h" |
064af421 | 56 | #include "datapath.h" |
064af421 | 57 | #include "flow.h" |
b9c15df9 | 58 | #include "genl_exec.h" |
303708cc | 59 | #include "vlan.h" |
3544358a | 60 | #include "tunnel.h" |
f2459fe7 | 61 | #include "vport-internal_dev.h" |
064af421 | 62 | |
4cf41591 | 63 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18) || \ |
64807dfb JP |
64 | LINUX_VERSION_CODE >= KERNEL_VERSION(3,9,0) |
65 | #error Kernels before 2.6.18 or after 3.8 are not supported by this version of Open vSwitch. | |
4cf41591 JG |
66 | #endif |
67 | ||
acd051f1 PS |
68 | #define REHASH_FLOW_INTERVAL (10 * 60 * HZ) |
69 | static void rehash_flow_table(struct work_struct *work); | |
70 | static DECLARE_DELAYED_WORK(rehash_flow_wq, rehash_flow_table); | |
71 | ||
2a4999f3 PS |
72 | int ovs_net_id __read_mostly; |
73 | ||
ed099e92 BP |
74 | /** |
75 | * DOC: Locking: | |
064af421 | 76 | * |
ed099e92 BP |
77 | * Writes to device state (add/remove datapath, port, set operations on vports, |
78 | * etc.) are protected by RTNL. | |
064af421 | 79 | * |
ed099e92 | 80 | * Writes to other state (flow table modifications, set miscellaneous datapath |
7257b535 BP |
81 | * parameters, etc.) are protected by genl_mutex. The RTNL lock nests inside |
82 | * genl_mutex. | |
ed099e92 BP |
83 | * |
84 | * Reads are protected by RCU. | |
85 | * | |
86 | * There are a few special cases (mostly stats) that have their own | |
87 | * synchronization but they nest under all of above and don't interact with | |
88 | * each other. | |
064af421 | 89 | */ |
ed099e92 | 90 | |
c19e6535 | 91 | static struct vport *new_vport(const struct vport_parms *); |
2a4999f3 | 92 | static int queue_gso_packets(struct net *, int dp_ifindex, struct sk_buff *, |
7257b535 | 93 | const struct dp_upcall_info *); |
2a4999f3 PS |
94 | static int queue_userspace_packet(struct net *, int dp_ifindex, |
95 | struct sk_buff *, | |
7257b535 | 96 | const struct dp_upcall_info *); |
064af421 | 97 | |
ed099e92 | 98 | /* Must be called with rcu_read_lock, genl_mutex, or RTNL lock. */ |
2a4999f3 | 99 | static struct datapath *get_dp(struct net *net, int dp_ifindex) |
064af421 | 100 | { |
254f2dc8 BP |
101 | struct datapath *dp = NULL; |
102 | struct net_device *dev; | |
ed099e92 | 103 | |
254f2dc8 | 104 | rcu_read_lock(); |
2a4999f3 | 105 | dev = dev_get_by_index_rcu(net, dp_ifindex); |
254f2dc8 | 106 | if (dev) { |
850b6b3b | 107 | struct vport *vport = ovs_internal_dev_get_vport(dev); |
254f2dc8 BP |
108 | if (vport) |
109 | dp = vport->dp; | |
110 | } | |
111 | rcu_read_unlock(); | |
112 | ||
113 | return dp; | |
064af421 | 114 | } |
064af421 | 115 | |
f2459fe7 | 116 | /* Must be called with rcu_read_lock or RTNL lock. */ |
850b6b3b | 117 | const char *ovs_dp_name(const struct datapath *dp) |
f2459fe7 | 118 | { |
95b1d73a | 119 | struct vport *vport = ovs_vport_rtnl_rcu(dp, OVSP_LOCAL); |
16b82e84 | 120 | return vport->ops->get_name(vport); |
f2459fe7 JG |
121 | } |
122 | ||
99769a40 JG |
123 | static int get_dpifindex(struct datapath *dp) |
124 | { | |
125 | struct vport *local; | |
126 | int ifindex; | |
127 | ||
128 | rcu_read_lock(); | |
129 | ||
95b1d73a | 130 | local = ovs_vport_rcu(dp, OVSP_LOCAL); |
99769a40 | 131 | if (local) |
16b82e84 | 132 | ifindex = local->ops->get_ifindex(local); |
99769a40 JG |
133 | else |
134 | ifindex = 0; | |
135 | ||
136 | rcu_read_unlock(); | |
137 | ||
138 | return ifindex; | |
139 | } | |
140 | ||
46c6a11d JG |
141 | static void destroy_dp_rcu(struct rcu_head *rcu) |
142 | { | |
143 | struct datapath *dp = container_of(rcu, struct datapath, rcu); | |
46c6a11d | 144 | |
850b6b3b | 145 | ovs_flow_tbl_destroy((__force struct flow_table *)dp->table); |
46c6a11d | 146 | free_percpu(dp->stats_percpu); |
2a4999f3 | 147 | release_net(ovs_dp_get_net(dp)); |
95b1d73a | 148 | kfree(dp->ports); |
5ca1ba48 | 149 | kfree(dp); |
46c6a11d JG |
150 | } |
151 | ||
95b1d73a PS |
152 | static struct hlist_head *vport_hash_bucket(const struct datapath *dp, |
153 | u16 port_no) | |
154 | { | |
155 | return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)]; | |
156 | } | |
157 | ||
158 | struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no) | |
159 | { | |
160 | struct vport *vport; | |
161 | struct hlist_node *n; | |
162 | struct hlist_head *head; | |
163 | ||
164 | head = vport_hash_bucket(dp, port_no); | |
165 | hlist_for_each_entry_rcu(vport, n, head, dp_hash_node) { | |
166 | if (vport->port_no == port_no) | |
167 | return vport; | |
168 | } | |
169 | return NULL; | |
170 | } | |
171 | ||
ed099e92 | 172 | /* Called with RTNL lock and genl_lock. */ |
c19e6535 | 173 | static struct vport *new_vport(const struct vport_parms *parms) |
064af421 | 174 | { |
f2459fe7 | 175 | struct vport *vport; |
f2459fe7 | 176 | |
850b6b3b | 177 | vport = ovs_vport_add(parms); |
c19e6535 BP |
178 | if (!IS_ERR(vport)) { |
179 | struct datapath *dp = parms->dp; | |
95b1d73a | 180 | struct hlist_head *head = vport_hash_bucket(dp, vport->port_no); |
064af421 | 181 | |
95b1d73a | 182 | hlist_add_head_rcu(&vport->dp_hash_node, head); |
c19e6535 | 183 | } |
c19e6535 | 184 | return vport; |
064af421 BP |
185 | } |
186 | ||
ed099e92 | 187 | /* Called with RTNL lock. */ |
850b6b3b | 188 | void ovs_dp_detach_port(struct vport *p) |
064af421 BP |
189 | { |
190 | ASSERT_RTNL(); | |
191 | ||
064af421 | 192 | /* First drop references to device. */ |
95b1d73a | 193 | hlist_del_rcu(&p->dp_hash_node); |
f2459fe7 | 194 | |
7237e4f4 | 195 | /* Then destroy it. */ |
850b6b3b | 196 | ovs_vport_del(p); |
064af421 BP |
197 | } |
198 | ||
8819fac7 | 199 | /* Must be called with rcu_read_lock. */ |
850b6b3b | 200 | void ovs_dp_process_received_packet(struct vport *p, struct sk_buff *skb) |
064af421 BP |
201 | { |
202 | struct datapath *dp = p->dp; | |
3544358a | 203 | struct sw_flow *flow; |
064af421 | 204 | struct dp_stats_percpu *stats; |
e9141eec | 205 | u64 *stats_counter; |
4c1ad233 | 206 | int error; |
064af421 | 207 | |
70dbc259 | 208 | stats = this_cpu_ptr(dp->stats_percpu); |
a063b0df | 209 | |
3976f6d5 | 210 | if (!OVS_CB(skb)->flow) { |
36956a7d | 211 | struct sw_flow_key key; |
76abe283 | 212 | int key_len; |
4c1ad233 | 213 | |
3976f6d5 | 214 | /* Extract flow from 'skb' into 'key'. */ |
850b6b3b | 215 | error = ovs_flow_extract(skb, p->port_no, &key, &key_len); |
3976f6d5 JG |
216 | if (unlikely(error)) { |
217 | kfree_skb(skb); | |
218 | return; | |
219 | } | |
064af421 | 220 | |
3976f6d5 | 221 | /* Look up flow. */ |
850b6b3b JG |
222 | flow = ovs_flow_tbl_lookup(rcu_dereference(dp->table), |
223 | &key, key_len); | |
3544358a | 224 | if (unlikely(!flow)) { |
856081f6 BP |
225 | struct dp_upcall_info upcall; |
226 | ||
df2c07f4 | 227 | upcall.cmd = OVS_PACKET_CMD_MISS; |
856081f6 | 228 | upcall.key = &key; |
98403001 | 229 | upcall.userdata = NULL; |
28aea917 | 230 | upcall.portid = p->upcall_portid; |
850b6b3b | 231 | ovs_dp_upcall(dp, skb, &upcall); |
d9e214da | 232 | consume_skb(skb); |
e9141eec | 233 | stats_counter = &stats->n_missed; |
3976f6d5 JG |
234 | goto out; |
235 | } | |
236 | ||
3544358a | 237 | OVS_CB(skb)->flow = flow; |
55574bb0 BP |
238 | } |
239 | ||
e9141eec | 240 | stats_counter = &stats->n_hit; |
850b6b3b JG |
241 | ovs_flow_used(OVS_CB(skb)->flow, skb); |
242 | ovs_execute_actions(dp, skb); | |
55574bb0 | 243 | |
8819fac7 | 244 | out: |
55574bb0 | 245 | /* Update datapath statistics. */ |
821cb9fa | 246 | u64_stats_update_begin(&stats->sync); |
e9141eec | 247 | (*stats_counter)++; |
821cb9fa | 248 | u64_stats_update_end(&stats->sync); |
064af421 BP |
249 | } |
250 | ||
aa5a8fdc JG |
251 | static struct genl_family dp_packet_genl_family = { |
252 | .id = GENL_ID_GENERATE, | |
df2c07f4 JP |
253 | .hdrsize = sizeof(struct ovs_header), |
254 | .name = OVS_PACKET_FAMILY, | |
69685a88 | 255 | .version = OVS_PACKET_VERSION, |
2a4999f3 PS |
256 | .maxattr = OVS_PACKET_ATTR_MAX, |
257 | SET_NETNSOK | |
aa5a8fdc JG |
258 | }; |
259 | ||
850b6b3b JG |
260 | int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb, |
261 | const struct dp_upcall_info *upcall_info) | |
aa5a8fdc JG |
262 | { |
263 | struct dp_stats_percpu *stats; | |
7257b535 | 264 | int dp_ifindex; |
aa5a8fdc JG |
265 | int err; |
266 | ||
28aea917 | 267 | if (upcall_info->portid == 0) { |
b063d9f0 | 268 | err = -ENOTCONN; |
b063d9f0 JG |
269 | goto err; |
270 | } | |
271 | ||
7257b535 BP |
272 | dp_ifindex = get_dpifindex(dp); |
273 | if (!dp_ifindex) { | |
274 | err = -ENODEV; | |
275 | goto err; | |
aa5a8fdc JG |
276 | } |
277 | ||
7257b535 | 278 | forward_ip_summed(skb, true); |
36ce148c | 279 | |
7257b535 | 280 | if (!skb_is_gso(skb)) |
2a4999f3 | 281 | err = queue_userspace_packet(ovs_dp_get_net(dp), dp_ifindex, skb, upcall_info); |
7257b535 | 282 | else |
2a4999f3 | 283 | err = queue_gso_packets(ovs_dp_get_net(dp), dp_ifindex, skb, upcall_info); |
d76195db JG |
284 | if (err) |
285 | goto err; | |
286 | ||
287 | return 0; | |
aa5a8fdc | 288 | |
aa5a8fdc | 289 | err: |
70dbc259 | 290 | stats = this_cpu_ptr(dp->stats_percpu); |
aa5a8fdc | 291 | |
821cb9fa | 292 | u64_stats_update_begin(&stats->sync); |
aa5a8fdc | 293 | stats->n_lost++; |
821cb9fa | 294 | u64_stats_update_end(&stats->sync); |
aa5a8fdc | 295 | |
aa5a8fdc | 296 | return err; |
982b8810 BP |
297 | } |
298 | ||
2a4999f3 PS |
299 | static int queue_gso_packets(struct net *net, int dp_ifindex, |
300 | struct sk_buff *skb, | |
7257b535 | 301 | const struct dp_upcall_info *upcall_info) |
cb5087ca | 302 | { |
d4cba1f8 | 303 | unsigned short gso_type = skb_shinfo(skb)->gso_type; |
7257b535 BP |
304 | struct dp_upcall_info later_info; |
305 | struct sw_flow_key later_key; | |
306 | struct sk_buff *segs, *nskb; | |
307 | int err; | |
cb5087ca | 308 | |
7257b535 | 309 | segs = skb_gso_segment(skb, NETIF_F_SG | NETIF_F_HW_CSUM); |
79089764 PS |
310 | if (IS_ERR(segs)) |
311 | return PTR_ERR(segs); | |
99769a40 | 312 | |
7257b535 BP |
313 | /* Queue all of the segments. */ |
314 | skb = segs; | |
cb5087ca | 315 | do { |
2a4999f3 | 316 | err = queue_userspace_packet(net, dp_ifindex, skb, upcall_info); |
982b8810 | 317 | if (err) |
7257b535 | 318 | break; |
856081f6 | 319 | |
d4cba1f8 | 320 | if (skb == segs && gso_type & SKB_GSO_UDP) { |
e1cf87ff JG |
321 | /* The initial flow key extracted by ovs_flow_extract() |
322 | * in this case is for a first fragment, so we need to | |
7257b535 BP |
323 | * properly mark later fragments. |
324 | */ | |
325 | later_key = *upcall_info->key; | |
9e44d715 | 326 | later_key.ip.frag = OVS_FRAG_TYPE_LATER; |
7257b535 BP |
327 | |
328 | later_info = *upcall_info; | |
329 | later_info.key = &later_key; | |
330 | upcall_info = &later_info; | |
331 | } | |
36ce148c | 332 | } while ((skb = skb->next)); |
cb5087ca | 333 | |
7257b535 BP |
334 | /* Free all of the segments. */ |
335 | skb = segs; | |
336 | do { | |
337 | nskb = skb->next; | |
338 | if (err) | |
339 | kfree_skb(skb); | |
340 | else | |
341 | consume_skb(skb); | |
342 | } while ((skb = nskb)); | |
343 | return err; | |
344 | } | |
345 | ||
2a4999f3 PS |
346 | static int queue_userspace_packet(struct net *net, int dp_ifindex, |
347 | struct sk_buff *skb, | |
7257b535 BP |
348 | const struct dp_upcall_info *upcall_info) |
349 | { | |
350 | struct ovs_header *upcall; | |
6161d3fd | 351 | struct sk_buff *nskb = NULL; |
7257b535 BP |
352 | struct sk_buff *user_skb; /* to be queued to userspace */ |
353 | struct nlattr *nla; | |
354 | unsigned int len; | |
355 | int err; | |
356 | ||
6161d3fd JG |
357 | if (vlan_tx_tag_present(skb)) { |
358 | nskb = skb_clone(skb, GFP_ATOMIC); | |
359 | if (!nskb) | |
360 | return -ENOMEM; | |
361 | ||
362 | err = vlan_deaccel_tag(nskb); | |
363 | if (err) | |
364 | return err; | |
7257b535 | 365 | |
6161d3fd JG |
366 | skb = nskb; |
367 | } | |
368 | ||
369 | if (nla_attr_size(skb->len) > USHRT_MAX) { | |
370 | err = -EFBIG; | |
371 | goto out; | |
372 | } | |
7257b535 BP |
373 | |
374 | len = sizeof(struct ovs_header); | |
375 | len += nla_total_size(skb->len); | |
376 | len += nla_total_size(FLOW_BUFSIZE); | |
377 | if (upcall_info->cmd == OVS_PACKET_CMD_ACTION) | |
378 | len += nla_total_size(8); | |
379 | ||
380 | user_skb = genlmsg_new(len, GFP_ATOMIC); | |
6161d3fd JG |
381 | if (!user_skb) { |
382 | err = -ENOMEM; | |
383 | goto out; | |
384 | } | |
7257b535 BP |
385 | |
386 | upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family, | |
387 | 0, upcall_info->cmd); | |
388 | upcall->dp_ifindex = dp_ifindex; | |
389 | ||
390 | nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY); | |
850b6b3b | 391 | ovs_flow_to_nlattrs(upcall_info->key, user_skb); |
7257b535 BP |
392 | nla_nest_end(user_skb, nla); |
393 | ||
394 | if (upcall_info->userdata) | |
395 | nla_put_u64(user_skb, OVS_PACKET_ATTR_USERDATA, | |
396 | nla_get_u64(upcall_info->userdata)); | |
397 | ||
398 | nla = __nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, skb->len); | |
bed53bd1 PS |
399 | |
400 | skb_copy_and_csum_dev(skb, nla_data(nla)); | |
7257b535 | 401 | |
28aea917 | 402 | err = genlmsg_unicast(net, user_skb, upcall_info->portid); |
6161d3fd JG |
403 | |
404 | out: | |
405 | kfree_skb(nskb); | |
406 | return err; | |
cb5087ca BP |
407 | } |
408 | ||
ed099e92 | 409 | /* Called with genl_mutex. */ |
2a4999f3 | 410 | static int flush_flows(struct datapath *dp) |
064af421 | 411 | { |
3544358a PS |
412 | struct flow_table *old_table; |
413 | struct flow_table *new_table; | |
8d5ebd83 | 414 | |
20d035b2 | 415 | old_table = genl_dereference(dp->table); |
850b6b3b | 416 | new_table = ovs_flow_tbl_alloc(TBL_MIN_BUCKETS); |
8d5ebd83 | 417 | if (!new_table) |
ed099e92 | 418 | return -ENOMEM; |
8d5ebd83 JG |
419 | |
420 | rcu_assign_pointer(dp->table, new_table); | |
421 | ||
850b6b3b | 422 | ovs_flow_tbl_deferred_destroy(old_table); |
ed099e92 | 423 | return 0; |
064af421 BP |
424 | } |
425 | ||
4edb9ae9 PS |
426 | static int validate_actions(const struct nlattr *attr, |
427 | const struct sw_flow_key *key, int depth); | |
6ff686f2 | 428 | |
4edb9ae9 PS |
429 | static int validate_sample(const struct nlattr *attr, |
430 | const struct sw_flow_key *key, int depth) | |
6ff686f2 | 431 | { |
4be00e48 BP |
432 | const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1]; |
433 | const struct nlattr *probability, *actions; | |
434 | const struct nlattr *a; | |
435 | int rem; | |
436 | ||
437 | memset(attrs, 0, sizeof(attrs)); | |
6455100f | 438 | nla_for_each_nested(a, attr, rem) { |
4be00e48 BP |
439 | int type = nla_type(a); |
440 | if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type]) | |
441 | return -EINVAL; | |
442 | attrs[type] = a; | |
443 | } | |
444 | if (rem) | |
6ff686f2 | 445 | return -EINVAL; |
4be00e48 BP |
446 | |
447 | probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY]; | |
448 | if (!probability || nla_len(probability) != sizeof(u32)) | |
6ff686f2 PS |
449 | return -EINVAL; |
450 | ||
4be00e48 BP |
451 | actions = attrs[OVS_SAMPLE_ATTR_ACTIONS]; |
452 | if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN)) | |
453 | return -EINVAL; | |
454 | return validate_actions(actions, key, depth + 1); | |
4edb9ae9 PS |
455 | } |
456 | ||
b1323f59 PS |
457 | static int validate_tp_port(const struct sw_flow_key *flow_key) |
458 | { | |
459 | if (flow_key->eth.type == htons(ETH_P_IP)) { | |
6e9bea4d | 460 | if (flow_key->ipv4.tp.src || flow_key->ipv4.tp.dst) |
b1323f59 PS |
461 | return 0; |
462 | } else if (flow_key->eth.type == htons(ETH_P_IPV6)) { | |
6e9bea4d | 463 | if (flow_key->ipv6.tp.src || flow_key->ipv6.tp.dst) |
b1323f59 PS |
464 | return 0; |
465 | } | |
466 | ||
467 | return -EINVAL; | |
468 | } | |
469 | ||
fea393b1 BP |
470 | static int validate_set(const struct nlattr *a, |
471 | const struct sw_flow_key *flow_key) | |
4edb9ae9 | 472 | { |
4edb9ae9 PS |
473 | const struct nlattr *ovs_key = nla_data(a); |
474 | int key_type = nla_type(ovs_key); | |
475 | ||
476 | /* There can be only one key in a action */ | |
477 | if (nla_total_size(nla_len(ovs_key)) != nla_len(a)) | |
478 | return -EINVAL; | |
479 | ||
480 | if (key_type > OVS_KEY_ATTR_MAX || | |
481 | nla_len(ovs_key) != ovs_key_lens[key_type]) | |
482 | return -EINVAL; | |
483 | ||
fea393b1 | 484 | switch (key_type) { |
4edb9ae9 | 485 | const struct ovs_key_ipv4 *ipv4_key; |
356af50b | 486 | const struct ovs_key_ipv4_tunnel *tun_key; |
bc7a5acd | 487 | const struct ovs_key_ipv6 *ipv6_key; |
4edb9ae9 | 488 | |
fea393b1 BP |
489 | case OVS_KEY_ATTR_PRIORITY: |
490 | case OVS_KEY_ATTR_TUN_ID: | |
491 | case OVS_KEY_ATTR_ETHERNET: | |
4edb9ae9 PS |
492 | break; |
493 | ||
72e8bf28 AA |
494 | case OVS_KEY_ATTR_SKB_MARK: |
495 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20) && !defined(CONFIG_NETFILTER) | |
496 | if (nla_get_u32(ovs_key) != 0) | |
497 | return -EINVAL; | |
498 | #endif | |
499 | break; | |
500 | ||
356af50b KM |
501 | case OVS_KEY_ATTR_IPV4_TUNNEL: |
502 | tun_key = nla_data(ovs_key); | |
503 | if (!tun_key->ipv4_dst) | |
504 | return -EINVAL; | |
505 | break; | |
506 | ||
fea393b1 | 507 | case OVS_KEY_ATTR_IPV4: |
4edb9ae9 PS |
508 | if (flow_key->eth.type != htons(ETH_P_IP)) |
509 | return -EINVAL; | |
510 | ||
6e9bea4d | 511 | if (!flow_key->ip.proto) |
4edb9ae9 PS |
512 | return -EINVAL; |
513 | ||
514 | ipv4_key = nla_data(ovs_key); | |
515 | if (ipv4_key->ipv4_proto != flow_key->ip.proto) | |
516 | return -EINVAL; | |
517 | ||
9e44d715 | 518 | if (ipv4_key->ipv4_frag != flow_key->ip.frag) |
7257b535 BP |
519 | return -EINVAL; |
520 | ||
4edb9ae9 PS |
521 | break; |
522 | ||
bc7a5acd AA |
523 | case OVS_KEY_ATTR_IPV6: |
524 | if (flow_key->eth.type != htons(ETH_P_IPV6)) | |
525 | return -EINVAL; | |
526 | ||
527 | if (!flow_key->ip.proto) | |
528 | return -EINVAL; | |
529 | ||
530 | ipv6_key = nla_data(ovs_key); | |
531 | if (ipv6_key->ipv6_proto != flow_key->ip.proto) | |
532 | return -EINVAL; | |
533 | ||
534 | if (ipv6_key->ipv6_frag != flow_key->ip.frag) | |
535 | return -EINVAL; | |
536 | ||
537 | if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000) | |
538 | return -EINVAL; | |
539 | ||
540 | break; | |
541 | ||
fea393b1 | 542 | case OVS_KEY_ATTR_TCP: |
4edb9ae9 PS |
543 | if (flow_key->ip.proto != IPPROTO_TCP) |
544 | return -EINVAL; | |
545 | ||
b1323f59 | 546 | return validate_tp_port(flow_key); |
4edb9ae9 | 547 | |
fea393b1 | 548 | case OVS_KEY_ATTR_UDP: |
4edb9ae9 PS |
549 | if (flow_key->ip.proto != IPPROTO_UDP) |
550 | return -EINVAL; | |
551 | ||
b1323f59 | 552 | return validate_tp_port(flow_key); |
4edb9ae9 PS |
553 | |
554 | default: | |
555 | return -EINVAL; | |
556 | } | |
fea393b1 | 557 | |
4edb9ae9 | 558 | return 0; |
6ff686f2 PS |
559 | } |
560 | ||
98403001 BP |
561 | static int validate_userspace(const struct nlattr *attr) |
562 | { | |
6455100f | 563 | static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = { |
98403001 BP |
564 | [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 }, |
565 | [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_U64 }, | |
566 | }; | |
567 | struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1]; | |
568 | int error; | |
569 | ||
6455100f PS |
570 | error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX, |
571 | attr, userspace_policy); | |
98403001 BP |
572 | if (error) |
573 | return error; | |
574 | ||
6455100f PS |
575 | if (!a[OVS_USERSPACE_ATTR_PID] || |
576 | !nla_get_u32(a[OVS_USERSPACE_ATTR_PID])) | |
98403001 BP |
577 | return -EINVAL; |
578 | ||
579 | return 0; | |
580 | } | |
581 | ||
4edb9ae9 PS |
582 | static int validate_actions(const struct nlattr *attr, |
583 | const struct sw_flow_key *key, int depth) | |
064af421 | 584 | { |
23cad98c | 585 | const struct nlattr *a; |
6ff686f2 PS |
586 | int rem, err; |
587 | ||
588 | if (depth >= SAMPLE_ACTION_DEPTH) | |
589 | return -EOVERFLOW; | |
23cad98c | 590 | |
37a1300c | 591 | nla_for_each_nested(a, attr, rem) { |
98403001 | 592 | /* Expected argument lengths, (u32)-1 for variable length. */ |
df2c07f4 | 593 | static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = { |
fea393b1 | 594 | [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32), |
98403001 | 595 | [OVS_ACTION_ATTR_USERSPACE] = (u32)-1, |
fea393b1 BP |
596 | [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan), |
597 | [OVS_ACTION_ATTR_POP_VLAN] = 0, | |
4edb9ae9 | 598 | [OVS_ACTION_ATTR_SET] = (u32)-1, |
98403001 | 599 | [OVS_ACTION_ATTR_SAMPLE] = (u32)-1 |
23cad98c | 600 | }; |
fea393b1 | 601 | const struct ovs_action_push_vlan *vlan; |
23cad98c BP |
602 | int type = nla_type(a); |
603 | ||
6ff686f2 | 604 | if (type > OVS_ACTION_ATTR_MAX || |
98403001 BP |
605 | (action_lens[type] != nla_len(a) && |
606 | action_lens[type] != (u32)-1)) | |
23cad98c BP |
607 | return -EINVAL; |
608 | ||
609 | switch (type) { | |
df2c07f4 | 610 | case OVS_ACTION_ATTR_UNSPEC: |
cdee00fd | 611 | return -EINVAL; |
064af421 | 612 | |
98403001 BP |
613 | case OVS_ACTION_ATTR_USERSPACE: |
614 | err = validate_userspace(a); | |
615 | if (err) | |
616 | return err; | |
617 | break; | |
618 | ||
df2c07f4 | 619 | case OVS_ACTION_ATTR_OUTPUT: |
23cad98c BP |
620 | if (nla_get_u32(a) >= DP_MAX_PORTS) |
621 | return -EINVAL; | |
3b1fc5f3 | 622 | break; |
cdee00fd | 623 | |
4edb9ae9 | 624 | |
fea393b1 BP |
625 | case OVS_ACTION_ATTR_POP_VLAN: |
626 | break; | |
627 | ||
628 | case OVS_ACTION_ATTR_PUSH_VLAN: | |
629 | vlan = nla_data(a); | |
630 | if (vlan->vlan_tpid != htons(ETH_P_8021Q)) | |
631 | return -EINVAL; | |
8ddc056d | 632 | if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT))) |
064af421 | 633 | return -EINVAL; |
23cad98c | 634 | break; |
064af421 | 635 | |
4edb9ae9 | 636 | case OVS_ACTION_ATTR_SET: |
fea393b1 | 637 | err = validate_set(a, key); |
4edb9ae9 PS |
638 | if (err) |
639 | return err; | |
23cad98c | 640 | break; |
064af421 | 641 | |
6ff686f2 | 642 | case OVS_ACTION_ATTR_SAMPLE: |
4edb9ae9 | 643 | err = validate_sample(a, key, depth); |
6ff686f2 PS |
644 | if (err) |
645 | return err; | |
646 | break; | |
647 | ||
23cad98c | 648 | default: |
4edb9ae9 | 649 | return -EINVAL; |
23cad98c BP |
650 | } |
651 | } | |
3c5f6de3 | 652 | |
23cad98c BP |
653 | if (rem > 0) |
654 | return -EINVAL; | |
064af421 | 655 | |
23cad98c | 656 | return 0; |
064af421 | 657 | } |
4edb9ae9 | 658 | |
064af421 BP |
659 | static void clear_stats(struct sw_flow *flow) |
660 | { | |
6bfafa55 | 661 | flow->used = 0; |
064af421 | 662 | flow->tcp_flags = 0; |
064af421 BP |
663 | flow->packet_count = 0; |
664 | flow->byte_count = 0; | |
665 | } | |
666 | ||
df2c07f4 | 667 | static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info) |
064af421 | 668 | { |
df2c07f4 | 669 | struct ovs_header *ovs_header = info->userhdr; |
982b8810 | 670 | struct nlattr **a = info->attrs; |
e0e57990 | 671 | struct sw_flow_actions *acts; |
982b8810 | 672 | struct sk_buff *packet; |
e0e57990 | 673 | struct sw_flow *flow; |
f7cd0081 | 674 | struct datapath *dp; |
d6569377 | 675 | struct ethhdr *eth; |
3f19d399 | 676 | int len; |
d6569377 | 677 | int err; |
76abe283 | 678 | int key_len; |
064af421 | 679 | |
f7cd0081 | 680 | err = -EINVAL; |
df2c07f4 JP |
681 | if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] || |
682 | !a[OVS_PACKET_ATTR_ACTIONS] || | |
683 | nla_len(a[OVS_PACKET_ATTR_PACKET]) < ETH_HLEN) | |
e5cad958 | 684 | goto err; |
064af421 | 685 | |
df2c07f4 | 686 | len = nla_len(a[OVS_PACKET_ATTR_PACKET]); |
3f19d399 | 687 | packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL); |
f7cd0081 BP |
688 | err = -ENOMEM; |
689 | if (!packet) | |
e5cad958 | 690 | goto err; |
3f19d399 BP |
691 | skb_reserve(packet, NET_IP_ALIGN); |
692 | ||
df2c07f4 | 693 | memcpy(__skb_put(packet, len), nla_data(a[OVS_PACKET_ATTR_PACKET]), len); |
8d5ebd83 | 694 | |
f7cd0081 BP |
695 | skb_reset_mac_header(packet); |
696 | eth = eth_hdr(packet); | |
064af421 | 697 | |
d6569377 BP |
698 | /* Normally, setting the skb 'protocol' field would be handled by a |
699 | * call to eth_type_trans(), but it assumes there's a sending | |
700 | * device, which we may not have. */ | |
701 | if (ntohs(eth->h_proto) >= 1536) | |
f7cd0081 | 702 | packet->protocol = eth->h_proto; |
d6569377 | 703 | else |
f7cd0081 | 704 | packet->protocol = htons(ETH_P_802_2); |
d3c54451 | 705 | |
e0e57990 | 706 | /* Build an sw_flow for sending this packet. */ |
850b6b3b | 707 | flow = ovs_flow_alloc(); |
e0e57990 BP |
708 | err = PTR_ERR(flow); |
709 | if (IS_ERR(flow)) | |
e5cad958 | 710 | goto err_kfree_skb; |
064af421 | 711 | |
850b6b3b | 712 | err = ovs_flow_extract(packet, -1, &flow->key, &key_len); |
e0e57990 | 713 | if (err) |
9321954a | 714 | goto err_flow_free; |
e0e57990 | 715 | |
13e24889 | 716 | err = ovs_flow_metadata_from_nlattrs(flow, key_len, a[OVS_PACKET_ATTR_KEY]); |
80e5eed9 | 717 | if (err) |
9321954a | 718 | goto err_flow_free; |
80e5eed9 | 719 | |
4edb9ae9 PS |
720 | err = validate_actions(a[OVS_PACKET_ATTR_ACTIONS], &flow->key, 0); |
721 | if (err) | |
9321954a | 722 | goto err_flow_free; |
4edb9ae9 | 723 | |
850b6b3b | 724 | acts = ovs_flow_actions_alloc(a[OVS_PACKET_ATTR_ACTIONS]); |
e0e57990 BP |
725 | err = PTR_ERR(acts); |
726 | if (IS_ERR(acts)) | |
9321954a | 727 | goto err_flow_free; |
e0e57990 BP |
728 | rcu_assign_pointer(flow->sf_acts, acts); |
729 | ||
730 | OVS_CB(packet)->flow = flow; | |
abff858b | 731 | packet->priority = flow->key.phy.priority; |
72e8bf28 | 732 | skb_set_mark(packet, flow->key.phy.skb_mark); |
e0e57990 | 733 | |
d6569377 | 734 | rcu_read_lock(); |
2a4999f3 | 735 | dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex); |
f7cd0081 | 736 | err = -ENODEV; |
e5cad958 BP |
737 | if (!dp) |
738 | goto err_unlock; | |
cc4015df | 739 | |
e9141eec | 740 | local_bh_disable(); |
850b6b3b | 741 | err = ovs_execute_actions(dp, packet); |
e9141eec | 742 | local_bh_enable(); |
d6569377 | 743 | rcu_read_unlock(); |
e0e57990 | 744 | |
9321954a | 745 | ovs_flow_free(flow); |
e5cad958 | 746 | return err; |
064af421 | 747 | |
e5cad958 BP |
748 | err_unlock: |
749 | rcu_read_unlock(); | |
9321954a JG |
750 | err_flow_free: |
751 | ovs_flow_free(flow); | |
e5cad958 BP |
752 | err_kfree_skb: |
753 | kfree_skb(packet); | |
754 | err: | |
d6569377 | 755 | return err; |
064af421 BP |
756 | } |
757 | ||
df2c07f4 JP |
758 | static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = { |
759 | [OVS_PACKET_ATTR_PACKET] = { .type = NLA_UNSPEC }, | |
760 | [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED }, | |
761 | [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED }, | |
982b8810 BP |
762 | }; |
763 | ||
764 | static struct genl_ops dp_packet_genl_ops[] = { | |
df2c07f4 | 765 | { .cmd = OVS_PACKET_CMD_EXECUTE, |
982b8810 BP |
766 | .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ |
767 | .policy = packet_policy, | |
df2c07f4 | 768 | .doit = ovs_packet_cmd_execute |
982b8810 BP |
769 | } |
770 | }; | |
771 | ||
df2c07f4 | 772 | static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats) |
064af421 | 773 | { |
d6569377 | 774 | int i; |
20d035b2 | 775 | struct flow_table *table = genl_dereference(dp->table); |
f180c2e2 | 776 | |
850b6b3b | 777 | stats->n_flows = ovs_flow_tbl_count(table); |
064af421 | 778 | |
7257b535 | 779 | stats->n_hit = stats->n_missed = stats->n_lost = 0; |
d6569377 BP |
780 | for_each_possible_cpu(i) { |
781 | const struct dp_stats_percpu *percpu_stats; | |
782 | struct dp_stats_percpu local_stats; | |
821cb9fa | 783 | unsigned int start; |
44e05eca | 784 | |
d6569377 | 785 | percpu_stats = per_cpu_ptr(dp->stats_percpu, i); |
064af421 | 786 | |
d6569377 | 787 | do { |
821cb9fa | 788 | start = u64_stats_fetch_begin_bh(&percpu_stats->sync); |
d6569377 | 789 | local_stats = *percpu_stats; |
821cb9fa | 790 | } while (u64_stats_fetch_retry_bh(&percpu_stats->sync, start)); |
064af421 | 791 | |
d6569377 BP |
792 | stats->n_hit += local_stats.n_hit; |
793 | stats->n_missed += local_stats.n_missed; | |
794 | stats->n_lost += local_stats.n_lost; | |
795 | } | |
796 | } | |
064af421 | 797 | |
df2c07f4 JP |
798 | static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = { |
799 | [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED }, | |
800 | [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED }, | |
801 | [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG }, | |
d6569377 | 802 | }; |
36956a7d | 803 | |
37a1300c BP |
804 | static struct genl_family dp_flow_genl_family = { |
805 | .id = GENL_ID_GENERATE, | |
df2c07f4 JP |
806 | .hdrsize = sizeof(struct ovs_header), |
807 | .name = OVS_FLOW_FAMILY, | |
69685a88 | 808 | .version = OVS_FLOW_VERSION, |
2a4999f3 PS |
809 | .maxattr = OVS_FLOW_ATTR_MAX, |
810 | SET_NETNSOK | |
37a1300c | 811 | }; |
ed099e92 | 812 | |
850b6b3b | 813 | static struct genl_multicast_group ovs_dp_flow_multicast_group = { |
df2c07f4 | 814 | .name = OVS_FLOW_MCGROUP |
37a1300c BP |
815 | }; |
816 | ||
817 | /* Called with genl_lock. */ | |
df2c07f4 | 818 | static int ovs_flow_cmd_fill_info(struct sw_flow *flow, struct datapath *dp, |
28aea917 | 819 | struct sk_buff *skb, u32 portid, |
6455100f | 820 | u32 seq, u32 flags, u8 cmd) |
d6569377 | 821 | { |
37a1300c | 822 | const int skb_orig_len = skb->len; |
d6569377 | 823 | const struct sw_flow_actions *sf_acts; |
df2c07f4 JP |
824 | struct ovs_flow_stats stats; |
825 | struct ovs_header *ovs_header; | |
d6569377 BP |
826 | struct nlattr *nla; |
827 | unsigned long used; | |
828 | u8 tcp_flags; | |
829 | int err; | |
064af421 | 830 | |
d6569377 | 831 | sf_acts = rcu_dereference_protected(flow->sf_acts, |
ed099e92 | 832 | lockdep_genl_is_held()); |
064af421 | 833 | |
28aea917 | 834 | ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd); |
df2c07f4 | 835 | if (!ovs_header) |
37a1300c | 836 | return -EMSGSIZE; |
d6569377 | 837 | |
99769a40 | 838 | ovs_header->dp_ifindex = get_dpifindex(dp); |
d6569377 | 839 | |
df2c07f4 | 840 | nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY); |
d6569377 BP |
841 | if (!nla) |
842 | goto nla_put_failure; | |
850b6b3b | 843 | err = ovs_flow_to_nlattrs(&flow->key, skb); |
d6569377 | 844 | if (err) |
37a1300c | 845 | goto error; |
d6569377 BP |
846 | nla_nest_end(skb, nla); |
847 | ||
848 | spin_lock_bh(&flow->lock); | |
849 | used = flow->used; | |
850 | stats.n_packets = flow->packet_count; | |
851 | stats.n_bytes = flow->byte_count; | |
852 | tcp_flags = flow->tcp_flags; | |
853 | spin_unlock_bh(&flow->lock); | |
854 | ||
c3cc8c03 DM |
855 | if (used && |
856 | nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used))) | |
857 | goto nla_put_failure; | |
d6569377 | 858 | |
c3cc8c03 DM |
859 | if (stats.n_packets && |
860 | nla_put(skb, OVS_FLOW_ATTR_STATS, | |
861 | sizeof(struct ovs_flow_stats), &stats)) | |
862 | goto nla_put_failure; | |
d6569377 | 863 | |
c3cc8c03 DM |
864 | if (tcp_flags && |
865 | nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, tcp_flags)) | |
866 | goto nla_put_failure; | |
d6569377 | 867 | |
df2c07f4 | 868 | /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if |
30053024 BP |
869 | * this is the first flow to be dumped into 'skb'. This is unusual for |
870 | * Netlink but individual action lists can be longer than | |
871 | * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this. | |
872 | * The userspace caller can always fetch the actions separately if it | |
873 | * really wants them. (Most userspace callers in fact don't care.) | |
874 | * | |
875 | * This can only fail for dump operations because the skb is always | |
876 | * properly sized for single flows. | |
877 | */ | |
df2c07f4 | 878 | err = nla_put(skb, OVS_FLOW_ATTR_ACTIONS, sf_acts->actions_len, |
30053024 BP |
879 | sf_acts->actions); |
880 | if (err < 0 && skb_orig_len) | |
881 | goto error; | |
37a1300c | 882 | |
df2c07f4 | 883 | return genlmsg_end(skb, ovs_header); |
d6569377 BP |
884 | |
885 | nla_put_failure: | |
886 | err = -EMSGSIZE; | |
37a1300c | 887 | error: |
df2c07f4 | 888 | genlmsg_cancel(skb, ovs_header); |
d6569377 | 889 | return err; |
44e05eca BP |
890 | } |
891 | ||
df2c07f4 | 892 | static struct sk_buff *ovs_flow_cmd_alloc_info(struct sw_flow *flow) |
44e05eca | 893 | { |
37a1300c BP |
894 | const struct sw_flow_actions *sf_acts; |
895 | int len; | |
d6569377 | 896 | |
37a1300c BP |
897 | sf_acts = rcu_dereference_protected(flow->sf_acts, |
898 | lockdep_genl_is_held()); | |
d6569377 | 899 | |
6455100f PS |
900 | /* OVS_FLOW_ATTR_KEY */ |
901 | len = nla_total_size(FLOW_BUFSIZE); | |
902 | /* OVS_FLOW_ATTR_ACTIONS */ | |
903 | len += nla_total_size(sf_acts->actions_len); | |
904 | /* OVS_FLOW_ATTR_STATS */ | |
905 | len += nla_total_size(sizeof(struct ovs_flow_stats)); | |
906 | /* OVS_FLOW_ATTR_TCP_FLAGS */ | |
907 | len += nla_total_size(1); | |
908 | /* OVS_FLOW_ATTR_USED */ | |
909 | len += nla_total_size(8); | |
910 | ||
911 | len += NLMSG_ALIGN(sizeof(struct ovs_header)); | |
912 | ||
913 | return genlmsg_new(len, GFP_KERNEL); | |
37a1300c | 914 | } |
8d5ebd83 | 915 | |
6455100f PS |
916 | static struct sk_buff *ovs_flow_cmd_build_info(struct sw_flow *flow, |
917 | struct datapath *dp, | |
28aea917 | 918 | u32 portid, u32 seq, u8 cmd) |
37a1300c BP |
919 | { |
920 | struct sk_buff *skb; | |
921 | int retval; | |
d6569377 | 922 | |
df2c07f4 | 923 | skb = ovs_flow_cmd_alloc_info(flow); |
37a1300c BP |
924 | if (!skb) |
925 | return ERR_PTR(-ENOMEM); | |
d6569377 | 926 | |
28aea917 | 927 | retval = ovs_flow_cmd_fill_info(flow, dp, skb, portid, seq, 0, cmd); |
37a1300c | 928 | BUG_ON(retval < 0); |
d6569377 | 929 | return skb; |
064af421 BP |
930 | } |
931 | ||
df2c07f4 | 932 | static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info) |
064af421 | 933 | { |
37a1300c | 934 | struct nlattr **a = info->attrs; |
df2c07f4 | 935 | struct ovs_header *ovs_header = info->userhdr; |
37a1300c | 936 | struct sw_flow_key key; |
d6569377 | 937 | struct sw_flow *flow; |
37a1300c | 938 | struct sk_buff *reply; |
9c52546b | 939 | struct datapath *dp; |
3544358a | 940 | struct flow_table *table; |
bc4a05c6 | 941 | int error; |
76abe283 | 942 | int key_len; |
064af421 | 943 | |
37a1300c BP |
944 | /* Extract key. */ |
945 | error = -EINVAL; | |
df2c07f4 | 946 | if (!a[OVS_FLOW_ATTR_KEY]) |
37a1300c | 947 | goto error; |
850b6b3b | 948 | error = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]); |
37a1300c BP |
949 | if (error) |
950 | goto error; | |
064af421 | 951 | |
37a1300c | 952 | /* Validate actions. */ |
df2c07f4 | 953 | if (a[OVS_FLOW_ATTR_ACTIONS]) { |
4edb9ae9 | 954 | error = validate_actions(a[OVS_FLOW_ATTR_ACTIONS], &key, 0); |
37a1300c BP |
955 | if (error) |
956 | goto error; | |
df2c07f4 | 957 | } else if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW) { |
37a1300c BP |
958 | error = -EINVAL; |
959 | goto error; | |
960 | } | |
961 | ||
2a4999f3 | 962 | dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex); |
d6569377 | 963 | error = -ENODEV; |
9c52546b | 964 | if (!dp) |
37a1300c | 965 | goto error; |
704a1e09 | 966 | |
20d035b2 | 967 | table = genl_dereference(dp->table); |
850b6b3b | 968 | flow = ovs_flow_tbl_lookup(table, &key, key_len); |
3544358a | 969 | if (!flow) { |
d6569377 BP |
970 | struct sw_flow_actions *acts; |
971 | ||
972 | /* Bail out if we're not allowed to create a new flow. */ | |
973 | error = -ENOENT; | |
df2c07f4 | 974 | if (info->genlhdr->cmd == OVS_FLOW_CMD_SET) |
37a1300c | 975 | goto error; |
d6569377 BP |
976 | |
977 | /* Expand table, if necessary, to make room. */ | |
850b6b3b | 978 | if (ovs_flow_tbl_need_to_expand(table)) { |
3544358a PS |
979 | struct flow_table *new_table; |
980 | ||
850b6b3b | 981 | new_table = ovs_flow_tbl_expand(table); |
3544358a PS |
982 | if (!IS_ERR(new_table)) { |
983 | rcu_assign_pointer(dp->table, new_table); | |
850b6b3b | 984 | ovs_flow_tbl_deferred_destroy(table); |
20d035b2 | 985 | table = genl_dereference(dp->table); |
3544358a | 986 | } |
d6569377 BP |
987 | } |
988 | ||
989 | /* Allocate flow. */ | |
850b6b3b | 990 | flow = ovs_flow_alloc(); |
d6569377 BP |
991 | if (IS_ERR(flow)) { |
992 | error = PTR_ERR(flow); | |
37a1300c | 993 | goto error; |
d6569377 | 994 | } |
d6569377 BP |
995 | clear_stats(flow); |
996 | ||
997 | /* Obtain actions. */ | |
850b6b3b | 998 | acts = ovs_flow_actions_alloc(a[OVS_FLOW_ATTR_ACTIONS]); |
d6569377 BP |
999 | error = PTR_ERR(acts); |
1000 | if (IS_ERR(acts)) | |
1001 | goto error_free_flow; | |
1002 | rcu_assign_pointer(flow->sf_acts, acts); | |
1003 | ||
d6569377 | 1004 | /* Put flow in bucket. */ |
13e24889 | 1005 | ovs_flow_tbl_insert(table, flow, &key, key_len); |
37a1300c | 1006 | |
28aea917 | 1007 | reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid, |
6455100f PS |
1008 | info->snd_seq, |
1009 | OVS_FLOW_CMD_NEW); | |
d6569377 BP |
1010 | } else { |
1011 | /* We found a matching flow. */ | |
1012 | struct sw_flow_actions *old_acts; | |
6455100f | 1013 | struct nlattr *acts_attrs; |
d6569377 BP |
1014 | |
1015 | /* Bail out if we're not allowed to modify an existing flow. | |
1016 | * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL | |
1017 | * because Generic Netlink treats the latter as a dump | |
1018 | * request. We also accept NLM_F_EXCL in case that bug ever | |
1019 | * gets fixed. | |
1020 | */ | |
1021 | error = -EEXIST; | |
df2c07f4 | 1022 | if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW && |
37a1300c BP |
1023 | info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL)) |
1024 | goto error; | |
d6569377 BP |
1025 | |
1026 | /* Update actions. */ | |
d6569377 | 1027 | old_acts = rcu_dereference_protected(flow->sf_acts, |
ed099e92 | 1028 | lockdep_genl_is_held()); |
6455100f PS |
1029 | acts_attrs = a[OVS_FLOW_ATTR_ACTIONS]; |
1030 | if (acts_attrs && | |
1031 | (old_acts->actions_len != nla_len(acts_attrs) || | |
1032 | memcmp(old_acts->actions, nla_data(acts_attrs), | |
1033 | old_acts->actions_len))) { | |
d6569377 BP |
1034 | struct sw_flow_actions *new_acts; |
1035 | ||
850b6b3b | 1036 | new_acts = ovs_flow_actions_alloc(acts_attrs); |
d6569377 BP |
1037 | error = PTR_ERR(new_acts); |
1038 | if (IS_ERR(new_acts)) | |
37a1300c | 1039 | goto error; |
d6569377 BP |
1040 | |
1041 | rcu_assign_pointer(flow->sf_acts, new_acts); | |
850b6b3b | 1042 | ovs_flow_deferred_free_acts(old_acts); |
d6569377 BP |
1043 | } |
1044 | ||
28aea917 | 1045 | reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid, |
6455100f | 1046 | info->snd_seq, OVS_FLOW_CMD_NEW); |
d6569377 BP |
1047 | |
1048 | /* Clear stats. */ | |
df2c07f4 | 1049 | if (a[OVS_FLOW_ATTR_CLEAR]) { |
d6569377 BP |
1050 | spin_lock_bh(&flow->lock); |
1051 | clear_stats(flow); | |
1052 | spin_unlock_bh(&flow->lock); | |
1053 | } | |
9c52546b | 1054 | } |
37a1300c BP |
1055 | |
1056 | if (!IS_ERR(reply)) | |
28aea917 | 1057 | genl_notify(reply, genl_info_net(info), info->snd_portid, |
850b6b3b JG |
1058 | ovs_dp_flow_multicast_group.id, info->nlhdr, |
1059 | GFP_KERNEL); | |
37a1300c | 1060 | else |
2a4999f3 PS |
1061 | netlink_set_err(GENL_SOCK(sock_net(skb->sk)), 0, |
1062 | ovs_dp_flow_multicast_group.id, PTR_ERR(reply)); | |
d6569377 | 1063 | return 0; |
704a1e09 | 1064 | |
d6569377 | 1065 | error_free_flow: |
9321954a | 1066 | ovs_flow_free(flow); |
37a1300c | 1067 | error: |
9c52546b | 1068 | return error; |
704a1e09 BP |
1069 | } |
1070 | ||
df2c07f4 | 1071 | static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info) |
704a1e09 | 1072 | { |
37a1300c | 1073 | struct nlattr **a = info->attrs; |
df2c07f4 | 1074 | struct ovs_header *ovs_header = info->userhdr; |
37a1300c | 1075 | struct sw_flow_key key; |
37a1300c | 1076 | struct sk_buff *reply; |
704a1e09 | 1077 | struct sw_flow *flow; |
9c52546b | 1078 | struct datapath *dp; |
3544358a | 1079 | struct flow_table *table; |
9c52546b | 1080 | int err; |
76abe283 | 1081 | int key_len; |
704a1e09 | 1082 | |
df2c07f4 | 1083 | if (!a[OVS_FLOW_ATTR_KEY]) |
37a1300c | 1084 | return -EINVAL; |
850b6b3b | 1085 | err = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]); |
37a1300c BP |
1086 | if (err) |
1087 | return err; | |
704a1e09 | 1088 | |
2a4999f3 | 1089 | dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex); |
9c52546b | 1090 | if (!dp) |
ed099e92 | 1091 | return -ENODEV; |
704a1e09 | 1092 | |
20d035b2 | 1093 | table = genl_dereference(dp->table); |
850b6b3b | 1094 | flow = ovs_flow_tbl_lookup(table, &key, key_len); |
3544358a | 1095 | if (!flow) |
ed099e92 | 1096 | return -ENOENT; |
d6569377 | 1097 | |
28aea917 | 1098 | reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid, |
6455100f | 1099 | info->snd_seq, OVS_FLOW_CMD_NEW); |
37a1300c BP |
1100 | if (IS_ERR(reply)) |
1101 | return PTR_ERR(reply); | |
36956a7d | 1102 | |
37a1300c | 1103 | return genlmsg_reply(reply, info); |
d6569377 | 1104 | } |
9c52546b | 1105 | |
df2c07f4 | 1106 | static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info) |
d6569377 | 1107 | { |
37a1300c | 1108 | struct nlattr **a = info->attrs; |
df2c07f4 | 1109 | struct ovs_header *ovs_header = info->userhdr; |
37a1300c | 1110 | struct sw_flow_key key; |
37a1300c | 1111 | struct sk_buff *reply; |
d6569377 | 1112 | struct sw_flow *flow; |
d6569377 | 1113 | struct datapath *dp; |
3544358a | 1114 | struct flow_table *table; |
d6569377 | 1115 | int err; |
76abe283 | 1116 | int key_len; |
36956a7d | 1117 | |
2a4999f3 PS |
1118 | dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex); |
1119 | if (!dp) | |
1120 | return -ENODEV; | |
1121 | ||
df2c07f4 | 1122 | if (!a[OVS_FLOW_ATTR_KEY]) |
2a4999f3 PS |
1123 | return flush_flows(dp); |
1124 | ||
850b6b3b | 1125 | err = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]); |
37a1300c BP |
1126 | if (err) |
1127 | return err; | |
d6569377 | 1128 | |
20d035b2 | 1129 | table = genl_dereference(dp->table); |
850b6b3b | 1130 | flow = ovs_flow_tbl_lookup(table, &key, key_len); |
3544358a | 1131 | if (!flow) |
37a1300c | 1132 | return -ENOENT; |
d6569377 | 1133 | |
df2c07f4 | 1134 | reply = ovs_flow_cmd_alloc_info(flow); |
37a1300c BP |
1135 | if (!reply) |
1136 | return -ENOMEM; | |
1137 | ||
850b6b3b | 1138 | ovs_flow_tbl_remove(table, flow); |
37a1300c | 1139 | |
28aea917 | 1140 | err = ovs_flow_cmd_fill_info(flow, dp, reply, info->snd_portid, |
df2c07f4 | 1141 | info->snd_seq, 0, OVS_FLOW_CMD_DEL); |
37a1300c BP |
1142 | BUG_ON(err < 0); |
1143 | ||
850b6b3b | 1144 | ovs_flow_deferred_free(flow); |
37a1300c | 1145 | |
28aea917 | 1146 | genl_notify(reply, genl_info_net(info), info->snd_portid, |
850b6b3b | 1147 | ovs_dp_flow_multicast_group.id, info->nlhdr, GFP_KERNEL); |
37a1300c BP |
1148 | return 0; |
1149 | } | |
1150 | ||
df2c07f4 | 1151 | static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb) |
37a1300c | 1152 | { |
df2c07f4 | 1153 | struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh)); |
37a1300c | 1154 | struct datapath *dp; |
20d035b2 | 1155 | struct flow_table *table; |
37a1300c | 1156 | |
2a4999f3 | 1157 | dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex); |
37a1300c BP |
1158 | if (!dp) |
1159 | return -ENODEV; | |
1160 | ||
20d035b2 JG |
1161 | table = genl_dereference(dp->table); |
1162 | ||
37a1300c | 1163 | for (;;) { |
37a1300c BP |
1164 | struct sw_flow *flow; |
1165 | u32 bucket, obj; | |
1166 | ||
1167 | bucket = cb->args[0]; | |
1168 | obj = cb->args[1]; | |
850b6b3b | 1169 | flow = ovs_flow_tbl_next(table, &bucket, &obj); |
3544358a | 1170 | if (!flow) |
37a1300c BP |
1171 | break; |
1172 | ||
6455100f | 1173 | if (ovs_flow_cmd_fill_info(flow, dp, skb, |
28aea917 | 1174 | NETLINK_CB(cb->skb).portid, |
37a1300c | 1175 | cb->nlh->nlmsg_seq, NLM_F_MULTI, |
df2c07f4 | 1176 | OVS_FLOW_CMD_NEW) < 0) |
37a1300c BP |
1177 | break; |
1178 | ||
1179 | cb->args[0] = bucket; | |
1180 | cb->args[1] = obj; | |
1181 | } | |
1182 | return skb->len; | |
704a1e09 BP |
1183 | } |
1184 | ||
37a1300c | 1185 | static struct genl_ops dp_flow_genl_ops[] = { |
df2c07f4 | 1186 | { .cmd = OVS_FLOW_CMD_NEW, |
37a1300c BP |
1187 | .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ |
1188 | .policy = flow_policy, | |
df2c07f4 | 1189 | .doit = ovs_flow_cmd_new_or_set |
37a1300c | 1190 | }, |
df2c07f4 | 1191 | { .cmd = OVS_FLOW_CMD_DEL, |
37a1300c BP |
1192 | .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ |
1193 | .policy = flow_policy, | |
df2c07f4 | 1194 | .doit = ovs_flow_cmd_del |
37a1300c | 1195 | }, |
df2c07f4 | 1196 | { .cmd = OVS_FLOW_CMD_GET, |
37a1300c BP |
1197 | .flags = 0, /* OK for unprivileged users. */ |
1198 | .policy = flow_policy, | |
df2c07f4 JP |
1199 | .doit = ovs_flow_cmd_get, |
1200 | .dumpit = ovs_flow_cmd_dump | |
37a1300c | 1201 | }, |
df2c07f4 | 1202 | { .cmd = OVS_FLOW_CMD_SET, |
37a1300c BP |
1203 | .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ |
1204 | .policy = flow_policy, | |
df2c07f4 | 1205 | .doit = ovs_flow_cmd_new_or_set, |
37a1300c BP |
1206 | }, |
1207 | }; | |
1208 | ||
df2c07f4 | 1209 | static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = { |
aaff4b55 | 1210 | #ifdef HAVE_NLA_NUL_STRING |
df2c07f4 | 1211 | [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 }, |
aaff4b55 | 1212 | #endif |
b063d9f0 | 1213 | [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 }, |
d6569377 BP |
1214 | }; |
1215 | ||
aaff4b55 BP |
1216 | static struct genl_family dp_datapath_genl_family = { |
1217 | .id = GENL_ID_GENERATE, | |
df2c07f4 JP |
1218 | .hdrsize = sizeof(struct ovs_header), |
1219 | .name = OVS_DATAPATH_FAMILY, | |
69685a88 | 1220 | .version = OVS_DATAPATH_VERSION, |
2a4999f3 PS |
1221 | .maxattr = OVS_DP_ATTR_MAX, |
1222 | SET_NETNSOK | |
aaff4b55 BP |
1223 | }; |
1224 | ||
850b6b3b | 1225 | static struct genl_multicast_group ovs_dp_datapath_multicast_group = { |
df2c07f4 | 1226 | .name = OVS_DATAPATH_MCGROUP |
aaff4b55 BP |
1227 | }; |
1228 | ||
df2c07f4 | 1229 | static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb, |
28aea917 | 1230 | u32 portid, u32 seq, u32 flags, u8 cmd) |
064af421 | 1231 | { |
df2c07f4 | 1232 | struct ovs_header *ovs_header; |
e926dfe3 | 1233 | struct ovs_dp_stats dp_stats; |
064af421 BP |
1234 | int err; |
1235 | ||
28aea917 | 1236 | ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family, |
aaff4b55 | 1237 | flags, cmd); |
df2c07f4 | 1238 | if (!ovs_header) |
aaff4b55 | 1239 | goto error; |
064af421 | 1240 | |
b063d9f0 | 1241 | ovs_header->dp_ifindex = get_dpifindex(dp); |
064af421 | 1242 | |
d6569377 | 1243 | rcu_read_lock(); |
850b6b3b | 1244 | err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp)); |
d6569377 | 1245 | rcu_read_unlock(); |
064af421 | 1246 | if (err) |
d6569377 | 1247 | goto nla_put_failure; |
064af421 | 1248 | |
e926dfe3 | 1249 | get_dp_stats(dp, &dp_stats); |
c3cc8c03 DM |
1250 | if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats), &dp_stats)) |
1251 | goto nla_put_failure; | |
d6569377 | 1252 | |
df2c07f4 | 1253 | return genlmsg_end(skb, ovs_header); |
d6569377 BP |
1254 | |
1255 | nla_put_failure: | |
df2c07f4 | 1256 | genlmsg_cancel(skb, ovs_header); |
aaff4b55 BP |
1257 | error: |
1258 | return -EMSGSIZE; | |
d6569377 BP |
1259 | } |
1260 | ||
28aea917 | 1261 | static struct sk_buff *ovs_dp_cmd_build_info(struct datapath *dp, u32 portid, |
aaff4b55 | 1262 | u32 seq, u8 cmd) |
d6569377 | 1263 | { |
d6569377 | 1264 | struct sk_buff *skb; |
aaff4b55 | 1265 | int retval; |
d6569377 | 1266 | |
aaff4b55 | 1267 | skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
064af421 | 1268 | if (!skb) |
d6569377 | 1269 | return ERR_PTR(-ENOMEM); |
659586ef | 1270 | |
28aea917 | 1271 | retval = ovs_dp_cmd_fill_info(dp, skb, portid, seq, 0, cmd); |
aaff4b55 BP |
1272 | if (retval < 0) { |
1273 | kfree_skb(skb); | |
1274 | return ERR_PTR(retval); | |
1275 | } | |
1276 | return skb; | |
1277 | } | |
9dca7bd5 | 1278 | |
df2c07f4 | 1279 | static int ovs_dp_cmd_validate(struct nlattr *a[OVS_DP_ATTR_MAX + 1]) |
aaff4b55 | 1280 | { |
df2c07f4 | 1281 | return CHECK_NUL_STRING(a[OVS_DP_ATTR_NAME], IFNAMSIZ - 1); |
d6569377 BP |
1282 | } |
1283 | ||
ed099e92 | 1284 | /* Called with genl_mutex and optionally with RTNL lock also. */ |
2a4999f3 PS |
1285 | static struct datapath *lookup_datapath(struct net *net, |
1286 | struct ovs_header *ovs_header, | |
6455100f | 1287 | struct nlattr *a[OVS_DP_ATTR_MAX + 1]) |
d6569377 | 1288 | { |
254f2dc8 BP |
1289 | struct datapath *dp; |
1290 | ||
df2c07f4 | 1291 | if (!a[OVS_DP_ATTR_NAME]) |
2a4999f3 | 1292 | dp = get_dp(net, ovs_header->dp_ifindex); |
254f2dc8 | 1293 | else { |
d6569377 | 1294 | struct vport *vport; |
d6569377 | 1295 | |
057dd6d2 | 1296 | rcu_read_lock(); |
2a4999f3 | 1297 | vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME])); |
df2c07f4 | 1298 | dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL; |
057dd6d2 | 1299 | rcu_read_unlock(); |
d6569377 | 1300 | } |
254f2dc8 | 1301 | return dp ? dp : ERR_PTR(-ENODEV); |
d6569377 BP |
1302 | } |
1303 | ||
df2c07f4 | 1304 | static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info) |
d6569377 | 1305 | { |
aaff4b55 | 1306 | struct nlattr **a = info->attrs; |
d6569377 | 1307 | struct vport_parms parms; |
aaff4b55 | 1308 | struct sk_buff *reply; |
d6569377 BP |
1309 | struct datapath *dp; |
1310 | struct vport *vport; | |
2a4999f3 | 1311 | struct ovs_net *ovs_net; |
95b1d73a | 1312 | int err, i; |
d6569377 | 1313 | |
d6569377 | 1314 | err = -EINVAL; |
ea36840f | 1315 | if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID]) |
aaff4b55 BP |
1316 | goto err; |
1317 | ||
df2c07f4 | 1318 | err = ovs_dp_cmd_validate(a); |
aaff4b55 BP |
1319 | if (err) |
1320 | goto err; | |
d6569377 BP |
1321 | |
1322 | rtnl_lock(); | |
d6569377 | 1323 | |
d6569377 BP |
1324 | err = -ENOMEM; |
1325 | dp = kzalloc(sizeof(*dp), GFP_KERNEL); | |
1326 | if (dp == NULL) | |
2a4999f3 PS |
1327 | goto err_unlock_rtnl; |
1328 | ||
0ceaa66c JG |
1329 | ovs_dp_set_net(dp, hold_net(sock_net(skb->sk))); |
1330 | ||
d6569377 BP |
1331 | /* Allocate table. */ |
1332 | err = -ENOMEM; | |
850b6b3b | 1333 | rcu_assign_pointer(dp->table, ovs_flow_tbl_alloc(TBL_MIN_BUCKETS)); |
d6569377 BP |
1334 | if (!dp->table) |
1335 | goto err_free_dp; | |
1336 | ||
99769a40 JG |
1337 | dp->stats_percpu = alloc_percpu(struct dp_stats_percpu); |
1338 | if (!dp->stats_percpu) { | |
1339 | err = -ENOMEM; | |
1340 | goto err_destroy_table; | |
1341 | } | |
1342 | ||
95b1d73a PS |
1343 | dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head), |
1344 | GFP_KERNEL); | |
1345 | if (!dp->ports) { | |
1346 | err = -ENOMEM; | |
1347 | goto err_destroy_percpu; | |
1348 | } | |
1349 | ||
1350 | for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) | |
1351 | INIT_HLIST_HEAD(&dp->ports[i]); | |
1352 | ||
d6569377 | 1353 | /* Set up our datapath device. */ |
df2c07f4 JP |
1354 | parms.name = nla_data(a[OVS_DP_ATTR_NAME]); |
1355 | parms.type = OVS_VPORT_TYPE_INTERNAL; | |
d6569377 BP |
1356 | parms.options = NULL; |
1357 | parms.dp = dp; | |
df2c07f4 | 1358 | parms.port_no = OVSP_LOCAL; |
28aea917 | 1359 | parms.upcall_portid = nla_get_u32(a[OVS_DP_ATTR_UPCALL_PID]); |
b063d9f0 | 1360 | |
d6569377 BP |
1361 | vport = new_vport(&parms); |
1362 | if (IS_ERR(vport)) { | |
1363 | err = PTR_ERR(vport); | |
1364 | if (err == -EBUSY) | |
1365 | err = -EEXIST; | |
1366 | ||
95b1d73a | 1367 | goto err_destroy_ports_array; |
d6569377 | 1368 | } |
d6569377 | 1369 | |
28aea917 | 1370 | reply = ovs_dp_cmd_build_info(dp, info->snd_portid, |
6455100f | 1371 | info->snd_seq, OVS_DP_CMD_NEW); |
aaff4b55 BP |
1372 | err = PTR_ERR(reply); |
1373 | if (IS_ERR(reply)) | |
1374 | goto err_destroy_local_port; | |
1375 | ||
2a4999f3 PS |
1376 | ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id); |
1377 | list_add_tail(&dp->list_node, &ovs_net->dps); | |
d6569377 | 1378 | |
d6569377 BP |
1379 | rtnl_unlock(); |
1380 | ||
28aea917 | 1381 | genl_notify(reply, genl_info_net(info), info->snd_portid, |
850b6b3b JG |
1382 | ovs_dp_datapath_multicast_group.id, info->nlhdr, |
1383 | GFP_KERNEL); | |
d6569377 BP |
1384 | return 0; |
1385 | ||
1386 | err_destroy_local_port: | |
95b1d73a PS |
1387 | ovs_dp_detach_port(ovs_vport_rtnl(dp, OVSP_LOCAL)); |
1388 | err_destroy_ports_array: | |
1389 | kfree(dp->ports); | |
99769a40 JG |
1390 | err_destroy_percpu: |
1391 | free_percpu(dp->stats_percpu); | |
d6569377 | 1392 | err_destroy_table: |
850b6b3b | 1393 | ovs_flow_tbl_destroy(genl_dereference(dp->table)); |
d6569377 | 1394 | err_free_dp: |
0ceaa66c | 1395 | release_net(ovs_dp_get_net(dp)); |
d6569377 | 1396 | kfree(dp); |
ed099e92 | 1397 | err_unlock_rtnl: |
d6569377 | 1398 | rtnl_unlock(); |
d6569377 | 1399 | err: |
064af421 BP |
1400 | return err; |
1401 | } | |
1402 | ||
2a4999f3 PS |
1403 | /* Called with genl_mutex. */ |
1404 | static void __dp_destroy(struct datapath *dp) | |
44e05eca | 1405 | { |
95b1d73a | 1406 | int i; |
44e05eca | 1407 | |
d6569377 | 1408 | rtnl_lock(); |
95b1d73a PS |
1409 | |
1410 | for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) { | |
1411 | struct vport *vport; | |
1412 | struct hlist_node *node, *n; | |
1413 | ||
1414 | hlist_for_each_entry_safe(vport, node, n, &dp->ports[i], dp_hash_node) | |
1415 | if (vport->port_no != OVSP_LOCAL) | |
1416 | ovs_dp_detach_port(vport); | |
1417 | } | |
ed099e92 | 1418 | |
254f2dc8 | 1419 | list_del(&dp->list_node); |
95b1d73a | 1420 | ovs_dp_detach_port(ovs_vport_rtnl(dp, OVSP_LOCAL)); |
ed099e92 | 1421 | |
99620d2c JG |
1422 | /* rtnl_unlock() will wait until all the references to devices that |
1423 | * are pending unregistration have been dropped. We do it here to | |
1424 | * ensure that any internal devices (which contain DP pointers) are | |
1425 | * fully destroyed before freeing the datapath. | |
1426 | */ | |
1427 | rtnl_unlock(); | |
1428 | ||
ed099e92 | 1429 | call_rcu(&dp->rcu, destroy_dp_rcu); |
2a4999f3 PS |
1430 | } |
1431 | ||
1432 | static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info) | |
1433 | { | |
1434 | struct sk_buff *reply; | |
1435 | struct datapath *dp; | |
1436 | int err; | |
1437 | ||
1438 | err = ovs_dp_cmd_validate(info->attrs); | |
1439 | if (err) | |
1440 | return err; | |
1441 | ||
1442 | dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs); | |
1443 | err = PTR_ERR(dp); | |
1444 | if (IS_ERR(dp)) | |
1445 | return err; | |
1446 | ||
28aea917 | 1447 | reply = ovs_dp_cmd_build_info(dp, info->snd_portid, |
2a4999f3 PS |
1448 | info->snd_seq, OVS_DP_CMD_DEL); |
1449 | err = PTR_ERR(reply); | |
1450 | if (IS_ERR(reply)) | |
1451 | return err; | |
1452 | ||
1453 | __dp_destroy(dp); | |
ed099e92 | 1454 | |
28aea917 | 1455 | genl_notify(reply, genl_info_net(info), info->snd_portid, |
850b6b3b JG |
1456 | ovs_dp_datapath_multicast_group.id, info->nlhdr, |
1457 | GFP_KERNEL); | |
99620d2c JG |
1458 | |
1459 | return 0; | |
44e05eca BP |
1460 | } |
1461 | ||
df2c07f4 | 1462 | static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info) |
064af421 | 1463 | { |
aaff4b55 | 1464 | struct sk_buff *reply; |
d6569377 | 1465 | struct datapath *dp; |
d6569377 | 1466 | int err; |
064af421 | 1467 | |
df2c07f4 | 1468 | err = ovs_dp_cmd_validate(info->attrs); |
aaff4b55 BP |
1469 | if (err) |
1470 | return err; | |
38c6ecbc | 1471 | |
2a4999f3 | 1472 | dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs); |
d6569377 | 1473 | if (IS_ERR(dp)) |
aaff4b55 | 1474 | return PTR_ERR(dp); |
38c6ecbc | 1475 | |
28aea917 | 1476 | reply = ovs_dp_cmd_build_info(dp, info->snd_portid, |
6455100f | 1477 | info->snd_seq, OVS_DP_CMD_NEW); |
aaff4b55 BP |
1478 | if (IS_ERR(reply)) { |
1479 | err = PTR_ERR(reply); | |
2a4999f3 | 1480 | netlink_set_err(GENL_SOCK(sock_net(skb->sk)), 0, |
850b6b3b | 1481 | ovs_dp_datapath_multicast_group.id, err); |
aaff4b55 BP |
1482 | return 0; |
1483 | } | |
1484 | ||
28aea917 | 1485 | genl_notify(reply, genl_info_net(info), info->snd_portid, |
850b6b3b JG |
1486 | ovs_dp_datapath_multicast_group.id, info->nlhdr, |
1487 | GFP_KERNEL); | |
1488 | ||
aaff4b55 | 1489 | return 0; |
064af421 BP |
1490 | } |
1491 | ||
df2c07f4 | 1492 | static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info) |
1dcf111b | 1493 | { |
aaff4b55 | 1494 | struct sk_buff *reply; |
d6569377 | 1495 | struct datapath *dp; |
d6569377 | 1496 | int err; |
1dcf111b | 1497 | |
df2c07f4 | 1498 | err = ovs_dp_cmd_validate(info->attrs); |
aaff4b55 BP |
1499 | if (err) |
1500 | return err; | |
1dcf111b | 1501 | |
2a4999f3 | 1502 | dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs); |
d6569377 | 1503 | if (IS_ERR(dp)) |
aaff4b55 | 1504 | return PTR_ERR(dp); |
1dcf111b | 1505 | |
28aea917 | 1506 | reply = ovs_dp_cmd_build_info(dp, info->snd_portid, |
6455100f | 1507 | info->snd_seq, OVS_DP_CMD_NEW); |
aaff4b55 BP |
1508 | if (IS_ERR(reply)) |
1509 | return PTR_ERR(reply); | |
1510 | ||
1511 | return genlmsg_reply(reply, info); | |
1dcf111b JP |
1512 | } |
1513 | ||
df2c07f4 | 1514 | static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb) |
a7786963 | 1515 | { |
2a4999f3 | 1516 | struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id); |
254f2dc8 BP |
1517 | struct datapath *dp; |
1518 | int skip = cb->args[0]; | |
1519 | int i = 0; | |
a7786963 | 1520 | |
2a4999f3 | 1521 | list_for_each_entry(dp, &ovs_net->dps, list_node) { |
a2bab2f0 | 1522 | if (i >= skip && |
28aea917 | 1523 | ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid, |
aaff4b55 | 1524 | cb->nlh->nlmsg_seq, NLM_F_MULTI, |
df2c07f4 | 1525 | OVS_DP_CMD_NEW) < 0) |
aaff4b55 | 1526 | break; |
254f2dc8 | 1527 | i++; |
a7786963 | 1528 | } |
aaff4b55 | 1529 | |
254f2dc8 BP |
1530 | cb->args[0] = i; |
1531 | ||
aaff4b55 | 1532 | return skb->len; |
c19e6535 BP |
1533 | } |
1534 | ||
aaff4b55 | 1535 | static struct genl_ops dp_datapath_genl_ops[] = { |
df2c07f4 | 1536 | { .cmd = OVS_DP_CMD_NEW, |
aaff4b55 BP |
1537 | .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ |
1538 | .policy = datapath_policy, | |
df2c07f4 | 1539 | .doit = ovs_dp_cmd_new |
aaff4b55 | 1540 | }, |
df2c07f4 | 1541 | { .cmd = OVS_DP_CMD_DEL, |
aaff4b55 BP |
1542 | .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ |
1543 | .policy = datapath_policy, | |
df2c07f4 | 1544 | .doit = ovs_dp_cmd_del |
aaff4b55 | 1545 | }, |
df2c07f4 | 1546 | { .cmd = OVS_DP_CMD_GET, |
aaff4b55 BP |
1547 | .flags = 0, /* OK for unprivileged users. */ |
1548 | .policy = datapath_policy, | |
df2c07f4 JP |
1549 | .doit = ovs_dp_cmd_get, |
1550 | .dumpit = ovs_dp_cmd_dump | |
aaff4b55 | 1551 | }, |
df2c07f4 | 1552 | { .cmd = OVS_DP_CMD_SET, |
aaff4b55 BP |
1553 | .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ |
1554 | .policy = datapath_policy, | |
df2c07f4 | 1555 | .doit = ovs_dp_cmd_set, |
aaff4b55 BP |
1556 | }, |
1557 | }; | |
1558 | ||
df2c07f4 | 1559 | static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = { |
f0fef760 | 1560 | #ifdef HAVE_NLA_NUL_STRING |
df2c07f4 | 1561 | [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 }, |
f613a0d7 | 1562 | [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) }, |
df2c07f4 | 1563 | [OVS_VPORT_ATTR_ADDRESS] = { .len = ETH_ALEN }, |
f0fef760 | 1564 | #else |
f613a0d7 | 1565 | [OVS_VPORT_ATTR_STATS] = { .minlen = sizeof(struct ovs_vport_stats) }, |
df2c07f4 | 1566 | [OVS_VPORT_ATTR_ADDRESS] = { .minlen = ETH_ALEN }, |
f0fef760 | 1567 | #endif |
d48c88ec JG |
1568 | [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 }, |
1569 | [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 }, | |
b063d9f0 | 1570 | [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 }, |
df2c07f4 | 1571 | [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED }, |
c19e6535 BP |
1572 | }; |
1573 | ||
f0fef760 BP |
1574 | static struct genl_family dp_vport_genl_family = { |
1575 | .id = GENL_ID_GENERATE, | |
df2c07f4 JP |
1576 | .hdrsize = sizeof(struct ovs_header), |
1577 | .name = OVS_VPORT_FAMILY, | |
69685a88 | 1578 | .version = OVS_VPORT_VERSION, |
2a4999f3 PS |
1579 | .maxattr = OVS_VPORT_ATTR_MAX, |
1580 | SET_NETNSOK | |
f0fef760 BP |
1581 | }; |
1582 | ||
850b6b3b | 1583 | struct genl_multicast_group ovs_dp_vport_multicast_group = { |
df2c07f4 | 1584 | .name = OVS_VPORT_MCGROUP |
f0fef760 BP |
1585 | }; |
1586 | ||
1587 | /* Called with RTNL lock or RCU read lock. */ | |
df2c07f4 | 1588 | static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb, |
28aea917 | 1589 | u32 portid, u32 seq, u32 flags, u8 cmd) |
064af421 | 1590 | { |
df2c07f4 | 1591 | struct ovs_header *ovs_header; |
e926dfe3 | 1592 | struct ovs_vport_stats vport_stats; |
c19e6535 BP |
1593 | int err; |
1594 | ||
28aea917 | 1595 | ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family, |
f0fef760 | 1596 | flags, cmd); |
df2c07f4 | 1597 | if (!ovs_header) |
f0fef760 | 1598 | return -EMSGSIZE; |
c19e6535 | 1599 | |
99769a40 | 1600 | ovs_header->dp_ifindex = get_dpifindex(vport->dp); |
c19e6535 | 1601 | |
c3cc8c03 DM |
1602 | if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) || |
1603 | nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) || | |
1604 | nla_put_string(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport)) || | |
28aea917 | 1605 | nla_put_u32(skb, OVS_VPORT_ATTR_UPCALL_PID, vport->upcall_portid)) |
c3cc8c03 | 1606 | goto nla_put_failure; |
c19e6535 | 1607 | |
850b6b3b | 1608 | ovs_vport_get_stats(vport, &vport_stats); |
c3cc8c03 DM |
1609 | if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats), |
1610 | &vport_stats)) | |
1611 | goto nla_put_failure; | |
c19e6535 | 1612 | |
c3cc8c03 DM |
1613 | if (nla_put(skb, OVS_VPORT_ATTR_ADDRESS, ETH_ALEN, |
1614 | vport->ops->get_addr(vport))) | |
1615 | goto nla_put_failure; | |
c19e6535 | 1616 | |
850b6b3b | 1617 | err = ovs_vport_get_options(vport, skb); |
f0fef760 BP |
1618 | if (err == -EMSGSIZE) |
1619 | goto error; | |
c19e6535 | 1620 | |
df2c07f4 | 1621 | return genlmsg_end(skb, ovs_header); |
c19e6535 BP |
1622 | |
1623 | nla_put_failure: | |
1624 | err = -EMSGSIZE; | |
f0fef760 | 1625 | error: |
df2c07f4 | 1626 | genlmsg_cancel(skb, ovs_header); |
f0fef760 | 1627 | return err; |
064af421 BP |
1628 | } |
1629 | ||
f0fef760 | 1630 | /* Called with RTNL lock or RCU read lock. */ |
28aea917 | 1631 | struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid, |
f14d8083 | 1632 | u32 seq, u8 cmd) |
064af421 | 1633 | { |
c19e6535 | 1634 | struct sk_buff *skb; |
f0fef760 | 1635 | int retval; |
c19e6535 | 1636 | |
f0fef760 | 1637 | skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC); |
c19e6535 BP |
1638 | if (!skb) |
1639 | return ERR_PTR(-ENOMEM); | |
1640 | ||
28aea917 | 1641 | retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd); |
f0fef760 BP |
1642 | if (retval < 0) { |
1643 | kfree_skb(skb); | |
1644 | return ERR_PTR(retval); | |
1645 | } | |
c19e6535 | 1646 | return skb; |
f0fef760 | 1647 | } |
c19e6535 | 1648 | |
df2c07f4 | 1649 | static int ovs_vport_cmd_validate(struct nlattr *a[OVS_VPORT_ATTR_MAX + 1]) |
f0fef760 | 1650 | { |
df2c07f4 | 1651 | return CHECK_NUL_STRING(a[OVS_VPORT_ATTR_NAME], IFNAMSIZ - 1); |
c19e6535 | 1652 | } |
51d4d598 | 1653 | |
ed099e92 | 1654 | /* Called with RTNL lock or RCU read lock. */ |
2a4999f3 PS |
1655 | static struct vport *lookup_vport(struct net *net, |
1656 | struct ovs_header *ovs_header, | |
df2c07f4 | 1657 | struct nlattr *a[OVS_VPORT_ATTR_MAX + 1]) |
c19e6535 BP |
1658 | { |
1659 | struct datapath *dp; | |
1660 | struct vport *vport; | |
1661 | ||
df2c07f4 | 1662 | if (a[OVS_VPORT_ATTR_NAME]) { |
2a4999f3 | 1663 | vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME])); |
ed099e92 | 1664 | if (!vport) |
c19e6535 | 1665 | return ERR_PTR(-ENODEV); |
24ce832d BP |
1666 | if (ovs_header->dp_ifindex && |
1667 | ovs_header->dp_ifindex != get_dpifindex(vport->dp)) | |
1668 | return ERR_PTR(-ENODEV); | |
c19e6535 | 1669 | return vport; |
df2c07f4 JP |
1670 | } else if (a[OVS_VPORT_ATTR_PORT_NO]) { |
1671 | u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]); | |
c19e6535 BP |
1672 | |
1673 | if (port_no >= DP_MAX_PORTS) | |
f0fef760 | 1674 | return ERR_PTR(-EFBIG); |
c19e6535 | 1675 | |
2a4999f3 | 1676 | dp = get_dp(net, ovs_header->dp_ifindex); |
c19e6535 BP |
1677 | if (!dp) |
1678 | return ERR_PTR(-ENODEV); | |
f2459fe7 | 1679 | |
95b1d73a | 1680 | vport = ovs_vport_rtnl_rcu(dp, port_no); |
ed099e92 | 1681 | if (!vport) |
17535c57 | 1682 | return ERR_PTR(-ENODEV); |
c19e6535 BP |
1683 | return vport; |
1684 | } else | |
1685 | return ERR_PTR(-EINVAL); | |
064af421 BP |
1686 | } |
1687 | ||
ed099e92 | 1688 | /* Called with RTNL lock. */ |
6455100f PS |
1689 | static int change_vport(struct vport *vport, |
1690 | struct nlattr *a[OVS_VPORT_ATTR_MAX + 1]) | |
064af421 | 1691 | { |
c19e6535 | 1692 | int err = 0; |
f613a0d7 | 1693 | |
df2c07f4 | 1694 | if (a[OVS_VPORT_ATTR_STATS]) |
850b6b3b | 1695 | ovs_vport_set_stats(vport, nla_data(a[OVS_VPORT_ATTR_STATS])); |
f613a0d7 PS |
1696 | |
1697 | if (a[OVS_VPORT_ATTR_ADDRESS]) | |
850b6b3b | 1698 | err = ovs_vport_set_addr(vport, nla_data(a[OVS_VPORT_ATTR_ADDRESS])); |
f613a0d7 | 1699 | |
c19e6535 BP |
1700 | return err; |
1701 | } | |
1702 | ||
df2c07f4 | 1703 | static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info) |
c19e6535 | 1704 | { |
f0fef760 | 1705 | struct nlattr **a = info->attrs; |
df2c07f4 | 1706 | struct ovs_header *ovs_header = info->userhdr; |
c19e6535 | 1707 | struct vport_parms parms; |
ed099e92 | 1708 | struct sk_buff *reply; |
c19e6535 | 1709 | struct vport *vport; |
c19e6535 | 1710 | struct datapath *dp; |
b0ec0f27 | 1711 | u32 port_no; |
c19e6535 | 1712 | int err; |
b0ec0f27 | 1713 | |
c19e6535 | 1714 | err = -EINVAL; |
ea36840f BP |
1715 | if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] || |
1716 | !a[OVS_VPORT_ATTR_UPCALL_PID]) | |
f0fef760 BP |
1717 | goto exit; |
1718 | ||
df2c07f4 | 1719 | err = ovs_vport_cmd_validate(a); |
f0fef760 BP |
1720 | if (err) |
1721 | goto exit; | |
51d4d598 | 1722 | |
c19e6535 | 1723 | rtnl_lock(); |
2a4999f3 | 1724 | dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex); |
c19e6535 BP |
1725 | err = -ENODEV; |
1726 | if (!dp) | |
ed099e92 | 1727 | goto exit_unlock; |
c19e6535 | 1728 | |
df2c07f4 JP |
1729 | if (a[OVS_VPORT_ATTR_PORT_NO]) { |
1730 | port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]); | |
c19e6535 BP |
1731 | |
1732 | err = -EFBIG; | |
1733 | if (port_no >= DP_MAX_PORTS) | |
ed099e92 | 1734 | goto exit_unlock; |
c19e6535 | 1735 | |
95b1d73a | 1736 | vport = ovs_vport_rtnl(dp, port_no); |
c19e6535 BP |
1737 | err = -EBUSY; |
1738 | if (vport) | |
ed099e92 | 1739 | goto exit_unlock; |
c19e6535 BP |
1740 | } else { |
1741 | for (port_no = 1; ; port_no++) { | |
1742 | if (port_no >= DP_MAX_PORTS) { | |
1743 | err = -EFBIG; | |
ed099e92 | 1744 | goto exit_unlock; |
c19e6535 | 1745 | } |
95b1d73a | 1746 | vport = ovs_vport_rtnl(dp, port_no); |
c19e6535 BP |
1747 | if (!vport) |
1748 | break; | |
51d4d598 | 1749 | } |
064af421 | 1750 | } |
b0ec0f27 | 1751 | |
df2c07f4 JP |
1752 | parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]); |
1753 | parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]); | |
1754 | parms.options = a[OVS_VPORT_ATTR_OPTIONS]; | |
c19e6535 BP |
1755 | parms.dp = dp; |
1756 | parms.port_no = port_no; | |
28aea917 | 1757 | parms.upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]); |
c19e6535 BP |
1758 | |
1759 | vport = new_vport(&parms); | |
1760 | err = PTR_ERR(vport); | |
1761 | if (IS_ERR(vport)) | |
ed099e92 | 1762 | goto exit_unlock; |
c19e6535 | 1763 | |
c19e6535 | 1764 | err = change_vport(vport, a); |
f0fef760 | 1765 | if (!err) { |
28aea917 | 1766 | reply = ovs_vport_cmd_build_info(vport, info->snd_portid, |
6455100f PS |
1767 | info->snd_seq, |
1768 | OVS_VPORT_CMD_NEW); | |
f0fef760 BP |
1769 | if (IS_ERR(reply)) |
1770 | err = PTR_ERR(reply); | |
1771 | } | |
c19e6535 | 1772 | if (err) { |
850b6b3b | 1773 | ovs_dp_detach_port(vport); |
ed099e92 | 1774 | goto exit_unlock; |
c19e6535 | 1775 | } |
28aea917 | 1776 | genl_notify(reply, genl_info_net(info), info->snd_portid, |
850b6b3b | 1777 | ovs_dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL); |
c19e6535 | 1778 | |
ed099e92 | 1779 | exit_unlock: |
c19e6535 | 1780 | rtnl_unlock(); |
c19e6535 BP |
1781 | exit: |
1782 | return err; | |
44e05eca BP |
1783 | } |
1784 | ||
df2c07f4 | 1785 | static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info) |
44e05eca | 1786 | { |
f0fef760 BP |
1787 | struct nlattr **a = info->attrs; |
1788 | struct sk_buff *reply; | |
c19e6535 | 1789 | struct vport *vport; |
c19e6535 | 1790 | int err; |
44e05eca | 1791 | |
df2c07f4 | 1792 | err = ovs_vport_cmd_validate(a); |
f0fef760 | 1793 | if (err) |
c19e6535 BP |
1794 | goto exit; |
1795 | ||
1796 | rtnl_lock(); | |
2a4999f3 | 1797 | vport = lookup_vport(sock_net(skb->sk), info->userhdr, a); |
c19e6535 BP |
1798 | err = PTR_ERR(vport); |
1799 | if (IS_ERR(vport)) | |
f0fef760 | 1800 | goto exit_unlock; |
44e05eca | 1801 | |
c19e6535 | 1802 | err = 0; |
6455100f | 1803 | if (a[OVS_VPORT_ATTR_TYPE] && |
16b82e84 | 1804 | nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) |
4879d4c7 | 1805 | err = -EINVAL; |
6455100f | 1806 | |
4879d4c7 | 1807 | if (!err && a[OVS_VPORT_ATTR_OPTIONS]) |
850b6b3b | 1808 | err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]); |
c19e6535 BP |
1809 | if (!err) |
1810 | err = change_vport(vport, a); | |
53509ad5 AA |
1811 | else |
1812 | goto exit_unlock; | |
b063d9f0 | 1813 | if (!err && a[OVS_VPORT_ATTR_UPCALL_PID]) |
28aea917 | 1814 | vport->upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]); |
c19e6535 | 1815 | |
28aea917 IY |
1816 | reply = ovs_vport_cmd_build_info(vport, info->snd_portid, |
1817 | info->snd_seq, OVS_VPORT_CMD_NEW); | |
f0fef760 | 1818 | if (IS_ERR(reply)) { |
2a4999f3 | 1819 | netlink_set_err(GENL_SOCK(sock_net(skb->sk)), 0, |
7a6c067d AA |
1820 | ovs_dp_vport_multicast_group.id, PTR_ERR(reply)); |
1821 | goto exit_unlock; | |
f0fef760 BP |
1822 | } |
1823 | ||
28aea917 | 1824 | genl_notify(reply, genl_info_net(info), info->snd_portid, |
850b6b3b | 1825 | ovs_dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL); |
f0fef760 BP |
1826 | |
1827 | exit_unlock: | |
c19e6535 BP |
1828 | rtnl_unlock(); |
1829 | exit: | |
1830 | return err; | |
064af421 BP |
1831 | } |
1832 | ||
df2c07f4 | 1833 | static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info) |
7c40efc9 | 1834 | { |
f0fef760 BP |
1835 | struct nlattr **a = info->attrs; |
1836 | struct sk_buff *reply; | |
c19e6535 | 1837 | struct vport *vport; |
c19e6535 BP |
1838 | int err; |
1839 | ||
df2c07f4 | 1840 | err = ovs_vport_cmd_validate(a); |
f0fef760 | 1841 | if (err) |
c19e6535 BP |
1842 | goto exit; |
1843 | ||
1844 | rtnl_lock(); | |
2a4999f3 | 1845 | vport = lookup_vport(sock_net(skb->sk), info->userhdr, a); |
c19e6535 | 1846 | err = PTR_ERR(vport); |
f0fef760 BP |
1847 | if (IS_ERR(vport)) |
1848 | goto exit_unlock; | |
c19e6535 | 1849 | |
df2c07f4 | 1850 | if (vport->port_no == OVSP_LOCAL) { |
f0fef760 BP |
1851 | err = -EINVAL; |
1852 | goto exit_unlock; | |
1853 | } | |
1854 | ||
28aea917 IY |
1855 | reply = ovs_vport_cmd_build_info(vport, info->snd_portid, |
1856 | info->snd_seq, OVS_VPORT_CMD_DEL); | |
f0fef760 BP |
1857 | err = PTR_ERR(reply); |
1858 | if (IS_ERR(reply)) | |
1859 | goto exit_unlock; | |
1860 | ||
850b6b3b | 1861 | ovs_dp_detach_port(vport); |
f0fef760 | 1862 | |
28aea917 | 1863 | genl_notify(reply, genl_info_net(info), info->snd_portid, |
850b6b3b | 1864 | ovs_dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL); |
f0fef760 BP |
1865 | |
1866 | exit_unlock: | |
c19e6535 BP |
1867 | rtnl_unlock(); |
1868 | exit: | |
1869 | return err; | |
7c40efc9 BP |
1870 | } |
1871 | ||
df2c07f4 | 1872 | static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info) |
7c40efc9 | 1873 | { |
f0fef760 | 1874 | struct nlattr **a = info->attrs; |
df2c07f4 | 1875 | struct ovs_header *ovs_header = info->userhdr; |
ed099e92 | 1876 | struct sk_buff *reply; |
c19e6535 | 1877 | struct vport *vport; |
c19e6535 BP |
1878 | int err; |
1879 | ||
df2c07f4 | 1880 | err = ovs_vport_cmd_validate(a); |
f0fef760 BP |
1881 | if (err) |
1882 | goto exit; | |
c19e6535 | 1883 | |
ed099e92 | 1884 | rcu_read_lock(); |
2a4999f3 | 1885 | vport = lookup_vport(sock_net(skb->sk), ovs_header, a); |
c19e6535 BP |
1886 | err = PTR_ERR(vport); |
1887 | if (IS_ERR(vport)) | |
f0fef760 | 1888 | goto exit_unlock; |
c19e6535 | 1889 | |
28aea917 IY |
1890 | reply = ovs_vport_cmd_build_info(vport, info->snd_portid, |
1891 | info->snd_seq, OVS_VPORT_CMD_NEW); | |
ed099e92 BP |
1892 | err = PTR_ERR(reply); |
1893 | if (IS_ERR(reply)) | |
f0fef760 | 1894 | goto exit_unlock; |
ed099e92 | 1895 | |
df2fa9b5 JG |
1896 | rcu_read_unlock(); |
1897 | ||
1898 | return genlmsg_reply(reply, info); | |
ed099e92 | 1899 | |
f0fef760 | 1900 | exit_unlock: |
ed099e92 | 1901 | rcu_read_unlock(); |
f0fef760 | 1902 | exit: |
c19e6535 BP |
1903 | return err; |
1904 | } | |
1905 | ||
df2c07f4 | 1906 | static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb) |
c19e6535 | 1907 | { |
df2c07f4 | 1908 | struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh)); |
c19e6535 | 1909 | struct datapath *dp; |
95b1d73a PS |
1910 | int bucket = cb->args[0], skip = cb->args[1]; |
1911 | int i, j = 0; | |
c19e6535 | 1912 | |
2a4999f3 | 1913 | dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex); |
c19e6535 | 1914 | if (!dp) |
f0fef760 | 1915 | return -ENODEV; |
ed099e92 BP |
1916 | |
1917 | rcu_read_lock(); | |
95b1d73a | 1918 | for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) { |
ed099e92 | 1919 | struct vport *vport; |
95b1d73a PS |
1920 | struct hlist_node *n; |
1921 | ||
1922 | j = 0; | |
1923 | hlist_for_each_entry_rcu(vport, n, &dp->ports[i], dp_hash_node) { | |
1924 | if (j >= skip && | |
1925 | ovs_vport_cmd_fill_info(vport, skb, | |
28aea917 | 1926 | NETLINK_CB(cb->skb).portid, |
95b1d73a PS |
1927 | cb->nlh->nlmsg_seq, |
1928 | NLM_F_MULTI, | |
1929 | OVS_VPORT_CMD_NEW) < 0) | |
1930 | goto out; | |
1931 | ||
1932 | j++; | |
1933 | } | |
1934 | skip = 0; | |
c19e6535 | 1935 | } |
95b1d73a | 1936 | out: |
ed099e92 | 1937 | rcu_read_unlock(); |
c19e6535 | 1938 | |
95b1d73a PS |
1939 | cb->args[0] = i; |
1940 | cb->args[1] = j; | |
f0fef760 | 1941 | |
95b1d73a | 1942 | return skb->len; |
7c40efc9 BP |
1943 | } |
1944 | ||
f0fef760 | 1945 | static struct genl_ops dp_vport_genl_ops[] = { |
df2c07f4 | 1946 | { .cmd = OVS_VPORT_CMD_NEW, |
f0fef760 BP |
1947 | .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ |
1948 | .policy = vport_policy, | |
df2c07f4 | 1949 | .doit = ovs_vport_cmd_new |
f0fef760 | 1950 | }, |
df2c07f4 | 1951 | { .cmd = OVS_VPORT_CMD_DEL, |
f0fef760 BP |
1952 | .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ |
1953 | .policy = vport_policy, | |
df2c07f4 | 1954 | .doit = ovs_vport_cmd_del |
f0fef760 | 1955 | }, |
df2c07f4 | 1956 | { .cmd = OVS_VPORT_CMD_GET, |
f0fef760 BP |
1957 | .flags = 0, /* OK for unprivileged users. */ |
1958 | .policy = vport_policy, | |
df2c07f4 JP |
1959 | .doit = ovs_vport_cmd_get, |
1960 | .dumpit = ovs_vport_cmd_dump | |
f0fef760 | 1961 | }, |
df2c07f4 | 1962 | { .cmd = OVS_VPORT_CMD_SET, |
f0fef760 BP |
1963 | .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ |
1964 | .policy = vport_policy, | |
df2c07f4 | 1965 | .doit = ovs_vport_cmd_set, |
f0fef760 BP |
1966 | }, |
1967 | }; | |
1968 | ||
982b8810 BP |
1969 | struct genl_family_and_ops { |
1970 | struct genl_family *family; | |
1971 | struct genl_ops *ops; | |
1972 | int n_ops; | |
1973 | struct genl_multicast_group *group; | |
1974 | }; | |
ed099e92 | 1975 | |
982b8810 | 1976 | static const struct genl_family_and_ops dp_genl_families[] = { |
aaff4b55 BP |
1977 | { &dp_datapath_genl_family, |
1978 | dp_datapath_genl_ops, ARRAY_SIZE(dp_datapath_genl_ops), | |
850b6b3b | 1979 | &ovs_dp_datapath_multicast_group }, |
f0fef760 BP |
1980 | { &dp_vport_genl_family, |
1981 | dp_vport_genl_ops, ARRAY_SIZE(dp_vport_genl_ops), | |
850b6b3b | 1982 | &ovs_dp_vport_multicast_group }, |
37a1300c BP |
1983 | { &dp_flow_genl_family, |
1984 | dp_flow_genl_ops, ARRAY_SIZE(dp_flow_genl_ops), | |
850b6b3b | 1985 | &ovs_dp_flow_multicast_group }, |
982b8810 BP |
1986 | { &dp_packet_genl_family, |
1987 | dp_packet_genl_ops, ARRAY_SIZE(dp_packet_genl_ops), | |
1988 | NULL }, | |
1989 | }; | |
ed099e92 | 1990 | |
982b8810 BP |
1991 | static void dp_unregister_genl(int n_families) |
1992 | { | |
1993 | int i; | |
ed099e92 | 1994 | |
b867ca75 | 1995 | for (i = 0; i < n_families; i++) |
982b8810 | 1996 | genl_unregister_family(dp_genl_families[i].family); |
ed099e92 BP |
1997 | } |
1998 | ||
982b8810 | 1999 | static int dp_register_genl(void) |
064af421 | 2000 | { |
982b8810 BP |
2001 | int n_registered; |
2002 | int err; | |
2003 | int i; | |
064af421 | 2004 | |
982b8810 BP |
2005 | n_registered = 0; |
2006 | for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) { | |
2007 | const struct genl_family_and_ops *f = &dp_genl_families[i]; | |
064af421 | 2008 | |
982b8810 BP |
2009 | err = genl_register_family_with_ops(f->family, f->ops, |
2010 | f->n_ops); | |
2011 | if (err) | |
2012 | goto error; | |
2013 | n_registered++; | |
e22d4953 | 2014 | |
982b8810 BP |
2015 | if (f->group) { |
2016 | err = genl_register_mc_group(f->family, f->group); | |
2017 | if (err) | |
2018 | goto error; | |
2019 | } | |
2020 | } | |
9cc8b4e4 | 2021 | |
982b8810 | 2022 | return 0; |
064af421 BP |
2023 | |
2024 | error: | |
982b8810 BP |
2025 | dp_unregister_genl(n_registered); |
2026 | return err; | |
064af421 BP |
2027 | } |
2028 | ||
acd051f1 PS |
2029 | static int __rehash_flow_table(void *dummy) |
2030 | { | |
2031 | struct datapath *dp; | |
2a4999f3 PS |
2032 | struct net *net; |
2033 | ||
2034 | rtnl_lock(); | |
2035 | for_each_net(net) { | |
2036 | struct ovs_net *ovs_net = net_generic(net, ovs_net_id); | |
acd051f1 | 2037 | |
2a4999f3 PS |
2038 | list_for_each_entry(dp, &ovs_net->dps, list_node) { |
2039 | struct flow_table *old_table = genl_dereference(dp->table); | |
2040 | struct flow_table *new_table; | |
acd051f1 | 2041 | |
2a4999f3 PS |
2042 | new_table = ovs_flow_tbl_rehash(old_table); |
2043 | if (!IS_ERR(new_table)) { | |
2044 | rcu_assign_pointer(dp->table, new_table); | |
2045 | ovs_flow_tbl_deferred_destroy(old_table); | |
2046 | } | |
acd051f1 PS |
2047 | } |
2048 | } | |
2a4999f3 | 2049 | rtnl_unlock(); |
acd051f1 PS |
2050 | return 0; |
2051 | } | |
2052 | ||
2053 | static void rehash_flow_table(struct work_struct *work) | |
2054 | { | |
2055 | genl_exec(__rehash_flow_table, NULL); | |
2056 | schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL); | |
2057 | } | |
2058 | ||
2a4999f3 PS |
2059 | static int dp_destroy_all(void *data) |
2060 | { | |
2061 | struct datapath *dp, *dp_next; | |
2062 | struct ovs_net *ovs_net = data; | |
2063 | ||
2064 | list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node) | |
2065 | __dp_destroy(dp); | |
2066 | ||
2067 | return 0; | |
2068 | } | |
2069 | ||
2070 | static int __net_init ovs_init_net(struct net *net) | |
2071 | { | |
2072 | struct ovs_net *ovs_net = net_generic(net, ovs_net_id); | |
2073 | ||
2074 | INIT_LIST_HEAD(&ovs_net->dps); | |
2075 | return 0; | |
2076 | } | |
2077 | ||
2078 | static void __net_exit ovs_exit_net(struct net *net) | |
2079 | { | |
2080 | struct ovs_net *ovs_net = net_generic(net, ovs_net_id); | |
2081 | ||
2082 | genl_exec(dp_destroy_all, ovs_net); | |
2083 | } | |
2084 | ||
2085 | static struct pernet_operations ovs_net_ops = { | |
2086 | .init = ovs_init_net, | |
2087 | .exit = ovs_exit_net, | |
2088 | .id = &ovs_net_id, | |
2089 | .size = sizeof(struct ovs_net), | |
2090 | }; | |
2091 | ||
22d24ebf BP |
2092 | static int __init dp_init(void) |
2093 | { | |
2094 | int err; | |
2095 | ||
f3d85db3 | 2096 | BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb)); |
22d24ebf | 2097 | |
dc5f3fef | 2098 | pr_info("Open vSwitch switching datapath %s, built "__DATE__" "__TIME__"\n", |
8a07709c | 2099 | VERSION); |
064af421 | 2100 | |
b9c15df9 | 2101 | err = genl_exec_init(); |
064af421 BP |
2102 | if (err) |
2103 | goto error; | |
2104 | ||
16d650e5 | 2105 | err = ovs_workqueues_init(); |
b9c15df9 PS |
2106 | if (err) |
2107 | goto error_genl_exec; | |
2108 | ||
16d650e5 PS |
2109 | err = ovs_tnl_init(); |
2110 | if (err) | |
2111 | goto error_wq; | |
2112 | ||
850b6b3b | 2113 | err = ovs_flow_init(); |
3544358a PS |
2114 | if (err) |
2115 | goto error_tnl_exit; | |
2116 | ||
850b6b3b | 2117 | err = ovs_vport_init(); |
064af421 BP |
2118 | if (err) |
2119 | goto error_flow_exit; | |
2120 | ||
2a4999f3 | 2121 | err = register_pernet_device(&ovs_net_ops); |
f2459fe7 JG |
2122 | if (err) |
2123 | goto error_vport_exit; | |
2124 | ||
2a4999f3 PS |
2125 | err = register_netdevice_notifier(&ovs_dp_device_notifier); |
2126 | if (err) | |
2127 | goto error_netns_exit; | |
2128 | ||
982b8810 BP |
2129 | err = dp_register_genl(); |
2130 | if (err < 0) | |
37a1300c | 2131 | goto error_unreg_notifier; |
982b8810 | 2132 | |
acd051f1 PS |
2133 | schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL); |
2134 | ||
064af421 BP |
2135 | return 0; |
2136 | ||
2137 | error_unreg_notifier: | |
850b6b3b | 2138 | unregister_netdevice_notifier(&ovs_dp_device_notifier); |
2a4999f3 PS |
2139 | error_netns_exit: |
2140 | unregister_pernet_device(&ovs_net_ops); | |
f2459fe7 | 2141 | error_vport_exit: |
850b6b3b | 2142 | ovs_vport_exit(); |
064af421 | 2143 | error_flow_exit: |
850b6b3b | 2144 | ovs_flow_exit(); |
3544358a | 2145 | error_tnl_exit: |
850b6b3b | 2146 | ovs_tnl_exit(); |
16d650e5 PS |
2147 | error_wq: |
2148 | ovs_workqueues_exit(); | |
b9c15df9 PS |
2149 | error_genl_exec: |
2150 | genl_exec_exit(); | |
064af421 BP |
2151 | error: |
2152 | return err; | |
2153 | } | |
2154 | ||
2155 | static void dp_cleanup(void) | |
2156 | { | |
acd051f1 | 2157 | cancel_delayed_work_sync(&rehash_flow_wq); |
982b8810 | 2158 | dp_unregister_genl(ARRAY_SIZE(dp_genl_families)); |
850b6b3b | 2159 | unregister_netdevice_notifier(&ovs_dp_device_notifier); |
2a4999f3 PS |
2160 | unregister_pernet_device(&ovs_net_ops); |
2161 | rcu_barrier(); | |
850b6b3b JG |
2162 | ovs_vport_exit(); |
2163 | ovs_flow_exit(); | |
2164 | ovs_tnl_exit(); | |
16d650e5 | 2165 | ovs_workqueues_exit(); |
b9c15df9 | 2166 | genl_exec_exit(); |
064af421 BP |
2167 | } |
2168 | ||
2169 | module_init(dp_init); | |
2170 | module_exit(dp_cleanup); | |
2171 | ||
2172 | MODULE_DESCRIPTION("Open vSwitch switching datapath"); | |
2173 | MODULE_LICENSE("GPL"); | |
3d0666d2 | 2174 | MODULE_VERSION(VERSION); |