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