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