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