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