]>
Commit | Line | Data |
---|---|---|
064af421 | 1 | /* |
a1c564be | 2 | * Copyright (c) 2007-2013 Nicira, Inc. |
a14bc59f | 3 | * |
a9a29d22 JG |
4 | * This program is free software; you can redistribute it and/or |
5 | * modify it under the terms of version 2 of the GNU General Public | |
6 | * License as published by the Free Software Foundation. | |
7 | * | |
8 | * This program is distributed in the hope that it will be useful, but | |
9 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
11 | * General Public License for more details. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public License | |
14 | * along with this program; if not, write to the Free Software | |
15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | |
16 | * 02110-1301, USA | |
064af421 BP |
17 | */ |
18 | ||
dfffaef1 JP |
19 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
20 | ||
064af421 BP |
21 | #include <linux/init.h> |
22 | #include <linux/module.h> | |
064af421 | 23 | #include <linux/if_arp.h> |
064af421 BP |
24 | #include <linux/if_vlan.h> |
25 | #include <linux/in.h> | |
26 | #include <linux/ip.h> | |
982b8810 | 27 | #include <linux/jhash.h> |
064af421 BP |
28 | #include <linux/delay.h> |
29 | #include <linux/time.h> | |
30 | #include <linux/etherdevice.h> | |
ed099e92 | 31 | #include <linux/genetlink.h> |
064af421 BP |
32 | #include <linux/kernel.h> |
33 | #include <linux/kthread.h> | |
064af421 BP |
34 | #include <linux/mutex.h> |
35 | #include <linux/percpu.h> | |
36 | #include <linux/rcupdate.h> | |
37 | #include <linux/tcp.h> | |
38 | #include <linux/udp.h> | |
39 | #include <linux/version.h> | |
40 | #include <linux/ethtool.h> | |
064af421 | 41 | #include <linux/wait.h> |
064af421 | 42 | #include <asm/div64.h> |
656a0e37 | 43 | #include <linux/highmem.h> |
064af421 BP |
44 | #include <linux/netfilter_bridge.h> |
45 | #include <linux/netfilter_ipv4.h> | |
46 | #include <linux/inetdevice.h> | |
47 | #include <linux/list.h> | |
077257b8 | 48 | #include <linux/openvswitch.h> |
064af421 | 49 | #include <linux/rculist.h> |
064af421 | 50 | #include <linux/dmi.h> |
cd2a59e9 PS |
51 | #include <linux/genetlink.h> |
52 | #include <net/genetlink.h> | |
36956a7d | 53 | #include <net/genetlink.h> |
2a4999f3 PS |
54 | #include <net/net_namespace.h> |
55 | #include <net/netns/generic.h> | |
064af421 | 56 | |
064af421 | 57 | #include "datapath.h" |
064af421 | 58 | #include "flow.h" |
a097c0b2 | 59 | #include "flow_netlink.h" |
303708cc | 60 | #include "vlan.h" |
f2459fe7 | 61 | #include "vport-internal_dev.h" |
d5de5b0d | 62 | #include "vport-netdev.h" |
064af421 | 63 | |
acd051f1 | 64 | #define REHASH_FLOW_INTERVAL (10 * 60 * HZ) |
acd051f1 | 65 | |
2a4999f3 PS |
66 | int ovs_net_id __read_mostly; |
67 | ||
e297c6b7 TG |
68 | static void ovs_notify(struct sk_buff *skb, struct genl_info *info, |
69 | struct genl_multicast_group *grp) | |
70 | { | |
71 | genl_notify(skb, genl_info_net(info), info->snd_portid, | |
72 | grp->id, info->nlhdr, GFP_KERNEL); | |
73 | } | |
74 | ||
ed099e92 BP |
75 | /** |
76 | * DOC: Locking: | |
064af421 | 77 | * |
cd2a59e9 PS |
78 | * All writes e.g. Writes to device state (add/remove datapath, port, set |
79 | * operations on vports, etc.), Writes to other state (flow table | |
80 | * modifications, set miscellaneous datapath parameters, etc.) are protected | |
81 | * by ovs_lock. | |
ed099e92 BP |
82 | * |
83 | * Reads are protected by RCU. | |
84 | * | |
85 | * There are a few special cases (mostly stats) that have their own | |
86 | * synchronization but they nest under all of above and don't interact with | |
87 | * each other. | |
cd2a59e9 PS |
88 | * |
89 | * The RTNL lock nests inside ovs_mutex. | |
064af421 | 90 | */ |
ed099e92 | 91 | |
cd2a59e9 PS |
92 | static DEFINE_MUTEX(ovs_mutex); |
93 | ||
94 | void ovs_lock(void) | |
95 | { | |
96 | mutex_lock(&ovs_mutex); | |
97 | } | |
98 | ||
99 | void ovs_unlock(void) | |
100 | { | |
101 | mutex_unlock(&ovs_mutex); | |
102 | } | |
103 | ||
104 | #ifdef CONFIG_LOCKDEP | |
105 | int lockdep_ovsl_is_held(void) | |
106 | { | |
107 | if (debug_locks) | |
108 | return lockdep_is_held(&ovs_mutex); | |
109 | else | |
110 | return 1; | |
111 | } | |
112 | #endif | |
113 | ||
c19e6535 | 114 | static struct vport *new_vport(const struct vport_parms *); |
2a4999f3 | 115 | static int queue_gso_packets(struct net *, int dp_ifindex, struct sk_buff *, |
7257b535 | 116 | const struct dp_upcall_info *); |
2a4999f3 PS |
117 | static int queue_userspace_packet(struct net *, int dp_ifindex, |
118 | struct sk_buff *, | |
7257b535 | 119 | const struct dp_upcall_info *); |
064af421 | 120 | |
cd2a59e9 | 121 | /* Must be called with rcu_read_lock or ovs_mutex. */ |
2a4999f3 | 122 | static struct datapath *get_dp(struct net *net, int dp_ifindex) |
064af421 | 123 | { |
254f2dc8 BP |
124 | struct datapath *dp = NULL; |
125 | struct net_device *dev; | |
ed099e92 | 126 | |
254f2dc8 | 127 | rcu_read_lock(); |
2a4999f3 | 128 | dev = dev_get_by_index_rcu(net, dp_ifindex); |
254f2dc8 | 129 | if (dev) { |
850b6b3b | 130 | struct vport *vport = ovs_internal_dev_get_vport(dev); |
254f2dc8 BP |
131 | if (vport) |
132 | dp = vport->dp; | |
133 | } | |
134 | rcu_read_unlock(); | |
135 | ||
136 | return dp; | |
064af421 | 137 | } |
064af421 | 138 | |
cd2a59e9 | 139 | /* Must be called with rcu_read_lock or ovs_mutex. */ |
850b6b3b | 140 | const char *ovs_dp_name(const struct datapath *dp) |
f2459fe7 | 141 | { |
cd2a59e9 | 142 | struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL); |
16b82e84 | 143 | return vport->ops->get_name(vport); |
f2459fe7 JG |
144 | } |
145 | ||
99769a40 JG |
146 | static int get_dpifindex(struct datapath *dp) |
147 | { | |
148 | struct vport *local; | |
149 | int ifindex; | |
150 | ||
151 | rcu_read_lock(); | |
152 | ||
95b1d73a | 153 | local = ovs_vport_rcu(dp, OVSP_LOCAL); |
99769a40 | 154 | if (local) |
d5de5b0d | 155 | ifindex = netdev_vport_priv(local)->dev->ifindex; |
99769a40 JG |
156 | else |
157 | ifindex = 0; | |
158 | ||
159 | rcu_read_unlock(); | |
160 | ||
161 | return ifindex; | |
162 | } | |
163 | ||
46c6a11d JG |
164 | static void destroy_dp_rcu(struct rcu_head *rcu) |
165 | { | |
166 | struct datapath *dp = container_of(rcu, struct datapath, rcu); | |
46c6a11d | 167 | |
a1c564be | 168 | ovs_flow_tbl_destroy((__force struct flow_table *)dp->table, false); |
46c6a11d | 169 | free_percpu(dp->stats_percpu); |
2a4999f3 | 170 | release_net(ovs_dp_get_net(dp)); |
95b1d73a | 171 | kfree(dp->ports); |
5ca1ba48 | 172 | kfree(dp); |
46c6a11d JG |
173 | } |
174 | ||
95b1d73a PS |
175 | static struct hlist_head *vport_hash_bucket(const struct datapath *dp, |
176 | u16 port_no) | |
177 | { | |
178 | return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)]; | |
179 | } | |
180 | ||
181 | struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no) | |
182 | { | |
183 | struct vport *vport; | |
95b1d73a PS |
184 | struct hlist_head *head; |
185 | ||
186 | head = vport_hash_bucket(dp, port_no); | |
f8dfbcb7 | 187 | hlist_for_each_entry_rcu(vport, head, dp_hash_node) { |
95b1d73a PS |
188 | if (vport->port_no == port_no) |
189 | return vport; | |
190 | } | |
191 | return NULL; | |
192 | } | |
193 | ||
cd2a59e9 | 194 | /* Called with ovs_mutex. */ |
c19e6535 | 195 | static struct vport *new_vport(const struct vport_parms *parms) |
064af421 | 196 | { |
f2459fe7 | 197 | struct vport *vport; |
f2459fe7 | 198 | |
850b6b3b | 199 | vport = ovs_vport_add(parms); |
c19e6535 BP |
200 | if (!IS_ERR(vport)) { |
201 | struct datapath *dp = parms->dp; | |
95b1d73a | 202 | struct hlist_head *head = vport_hash_bucket(dp, vport->port_no); |
064af421 | 203 | |
95b1d73a | 204 | hlist_add_head_rcu(&vport->dp_hash_node, head); |
c19e6535 | 205 | } |
c19e6535 | 206 | return vport; |
064af421 BP |
207 | } |
208 | ||
850b6b3b | 209 | void ovs_dp_detach_port(struct vport *p) |
064af421 | 210 | { |
cd2a59e9 | 211 | ASSERT_OVSL(); |
064af421 | 212 | |
064af421 | 213 | /* First drop references to device. */ |
95b1d73a | 214 | hlist_del_rcu(&p->dp_hash_node); |
f2459fe7 | 215 | |
7237e4f4 | 216 | /* Then destroy it. */ |
850b6b3b | 217 | ovs_vport_del(p); |
064af421 BP |
218 | } |
219 | ||
8819fac7 | 220 | /* Must be called with rcu_read_lock. */ |
850b6b3b | 221 | void ovs_dp_process_received_packet(struct vport *p, struct sk_buff *skb) |
064af421 BP |
222 | { |
223 | struct datapath *dp = p->dp; | |
3544358a | 224 | struct sw_flow *flow; |
064af421 | 225 | struct dp_stats_percpu *stats; |
52a23d92 | 226 | struct sw_flow_key key; |
e9141eec | 227 | u64 *stats_counter; |
4c1ad233 | 228 | int error; |
064af421 | 229 | |
70dbc259 | 230 | stats = this_cpu_ptr(dp->stats_percpu); |
a063b0df | 231 | |
52a23d92 | 232 | /* Extract flow from 'skb' into 'key'. */ |
a1c564be | 233 | error = ovs_flow_extract(skb, p->port_no, &key); |
52a23d92 JG |
234 | if (unlikely(error)) { |
235 | kfree_skb(skb); | |
236 | return; | |
55574bb0 BP |
237 | } |
238 | ||
52a23d92 | 239 | /* Look up flow. */ |
a097c0b2 | 240 | flow = ovs_flow_tbl_lookup(rcu_dereference(dp->table), &key); |
52a23d92 JG |
241 | if (unlikely(!flow)) { |
242 | struct dp_upcall_info upcall; | |
243 | ||
244 | upcall.cmd = OVS_PACKET_CMD_MISS; | |
245 | upcall.key = &key; | |
246 | upcall.userdata = NULL; | |
247 | upcall.portid = p->upcall_portid; | |
248 | ovs_dp_upcall(dp, skb, &upcall); | |
249 | consume_skb(skb); | |
250 | stats_counter = &stats->n_missed; | |
251 | goto out; | |
252 | } | |
253 | ||
254 | OVS_CB(skb)->flow = flow; | |
d1d71a36 | 255 | OVS_CB(skb)->pkt_key = &key; |
52a23d92 | 256 | |
e9141eec | 257 | stats_counter = &stats->n_hit; |
850b6b3b JG |
258 | ovs_flow_used(OVS_CB(skb)->flow, skb); |
259 | ovs_execute_actions(dp, skb); | |
55574bb0 | 260 | |
8819fac7 | 261 | out: |
55574bb0 | 262 | /* Update datapath statistics. */ |
821cb9fa | 263 | u64_stats_update_begin(&stats->sync); |
e9141eec | 264 | (*stats_counter)++; |
821cb9fa | 265 | u64_stats_update_end(&stats->sync); |
064af421 BP |
266 | } |
267 | ||
aa5a8fdc JG |
268 | static struct genl_family dp_packet_genl_family = { |
269 | .id = GENL_ID_GENERATE, | |
df2c07f4 JP |
270 | .hdrsize = sizeof(struct ovs_header), |
271 | .name = OVS_PACKET_FAMILY, | |
69685a88 | 272 | .version = OVS_PACKET_VERSION, |
2a4999f3 | 273 | .maxattr = OVS_PACKET_ATTR_MAX, |
b3dcb73c | 274 | .netnsok = true, |
14002a59 | 275 | SET_PARALLEL_OPS |
aa5a8fdc JG |
276 | }; |
277 | ||
850b6b3b JG |
278 | int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb, |
279 | const struct dp_upcall_info *upcall_info) | |
aa5a8fdc JG |
280 | { |
281 | struct dp_stats_percpu *stats; | |
7257b535 | 282 | int dp_ifindex; |
aa5a8fdc JG |
283 | int err; |
284 | ||
28aea917 | 285 | if (upcall_info->portid == 0) { |
b063d9f0 | 286 | err = -ENOTCONN; |
b063d9f0 JG |
287 | goto err; |
288 | } | |
289 | ||
7257b535 BP |
290 | dp_ifindex = get_dpifindex(dp); |
291 | if (!dp_ifindex) { | |
292 | err = -ENODEV; | |
293 | goto err; | |
aa5a8fdc JG |
294 | } |
295 | ||
7257b535 | 296 | if (!skb_is_gso(skb)) |
2a4999f3 | 297 | err = queue_userspace_packet(ovs_dp_get_net(dp), dp_ifindex, skb, upcall_info); |
7257b535 | 298 | else |
2a4999f3 | 299 | err = queue_gso_packets(ovs_dp_get_net(dp), dp_ifindex, skb, upcall_info); |
d76195db JG |
300 | if (err) |
301 | goto err; | |
302 | ||
303 | return 0; | |
aa5a8fdc | 304 | |
aa5a8fdc | 305 | err: |
70dbc259 | 306 | stats = this_cpu_ptr(dp->stats_percpu); |
aa5a8fdc | 307 | |
821cb9fa | 308 | u64_stats_update_begin(&stats->sync); |
aa5a8fdc | 309 | stats->n_lost++; |
821cb9fa | 310 | u64_stats_update_end(&stats->sync); |
aa5a8fdc | 311 | |
aa5a8fdc | 312 | return err; |
982b8810 BP |
313 | } |
314 | ||
2a4999f3 PS |
315 | static int queue_gso_packets(struct net *net, int dp_ifindex, |
316 | struct sk_buff *skb, | |
7257b535 | 317 | const struct dp_upcall_info *upcall_info) |
cb5087ca | 318 | { |
d4cba1f8 | 319 | unsigned short gso_type = skb_shinfo(skb)->gso_type; |
7257b535 BP |
320 | struct dp_upcall_info later_info; |
321 | struct sw_flow_key later_key; | |
322 | struct sk_buff *segs, *nskb; | |
323 | int err; | |
cb5087ca | 324 | |
0aa52d88 | 325 | segs = __skb_gso_segment(skb, NETIF_F_SG | NETIF_F_HW_CSUM, false); |
79089764 PS |
326 | if (IS_ERR(segs)) |
327 | return PTR_ERR(segs); | |
99769a40 | 328 | |
7257b535 BP |
329 | /* Queue all of the segments. */ |
330 | skb = segs; | |
cb5087ca | 331 | do { |
2a4999f3 | 332 | err = queue_userspace_packet(net, dp_ifindex, skb, upcall_info); |
982b8810 | 333 | if (err) |
7257b535 | 334 | break; |
856081f6 | 335 | |
d4cba1f8 | 336 | if (skb == segs && gso_type & SKB_GSO_UDP) { |
e1cf87ff JG |
337 | /* The initial flow key extracted by ovs_flow_extract() |
338 | * in this case is for a first fragment, so we need to | |
7257b535 BP |
339 | * properly mark later fragments. |
340 | */ | |
341 | later_key = *upcall_info->key; | |
9e44d715 | 342 | later_key.ip.frag = OVS_FRAG_TYPE_LATER; |
7257b535 BP |
343 | |
344 | later_info = *upcall_info; | |
345 | later_info.key = &later_key; | |
346 | upcall_info = &later_info; | |
347 | } | |
36ce148c | 348 | } while ((skb = skb->next)); |
cb5087ca | 349 | |
7257b535 BP |
350 | /* Free all of the segments. */ |
351 | skb = segs; | |
352 | do { | |
353 | nskb = skb->next; | |
354 | if (err) | |
355 | kfree_skb(skb); | |
356 | else | |
357 | consume_skb(skb); | |
358 | } while ((skb = nskb)); | |
359 | return err; | |
360 | } | |
361 | ||
0afa2373 TG |
362 | static size_t key_attr_size(void) |
363 | { | |
364 | return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */ | |
365 | + nla_total_size(0) /* OVS_KEY_ATTR_TUNNEL */ | |
366 | + nla_total_size(8) /* OVS_TUNNEL_KEY_ATTR_ID */ | |
367 | + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_SRC */ | |
368 | + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_DST */ | |
369 | + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TOS */ | |
370 | + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TTL */ | |
371 | + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT */ | |
372 | + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_CSUM */ | |
373 | + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */ | |
374 | + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */ | |
375 | + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */ | |
376 | + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */ | |
377 | + nla_total_size(4) /* OVS_KEY_ATTR_8021Q */ | |
378 | + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */ | |
379 | + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */ | |
380 | + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */ | |
381 | + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */ | |
382 | + nla_total_size(28); /* OVS_KEY_ATTR_ND */ | |
383 | } | |
384 | ||
385 | static size_t upcall_msg_size(const struct sk_buff *skb, | |
386 | const struct nlattr *userdata) | |
387 | { | |
388 | size_t size = NLMSG_ALIGN(sizeof(struct ovs_header)) | |
389 | + nla_total_size(skb->len) /* OVS_PACKET_ATTR_PACKET */ | |
390 | + nla_total_size(key_attr_size()); /* OVS_PACKET_ATTR_KEY */ | |
391 | ||
392 | /* OVS_PACKET_ATTR_USERDATA */ | |
393 | if (userdata) | |
394 | size += NLA_ALIGN(userdata->nla_len); | |
395 | ||
396 | return size; | |
397 | } | |
398 | ||
2a4999f3 PS |
399 | static int queue_userspace_packet(struct net *net, int dp_ifindex, |
400 | struct sk_buff *skb, | |
7257b535 BP |
401 | const struct dp_upcall_info *upcall_info) |
402 | { | |
403 | struct ovs_header *upcall; | |
6161d3fd | 404 | struct sk_buff *nskb = NULL; |
7257b535 BP |
405 | struct sk_buff *user_skb; /* to be queued to userspace */ |
406 | struct nlattr *nla; | |
7257b535 BP |
407 | int err; |
408 | ||
6161d3fd JG |
409 | if (vlan_tx_tag_present(skb)) { |
410 | nskb = skb_clone(skb, GFP_ATOMIC); | |
411 | if (!nskb) | |
412 | return -ENOMEM; | |
07ac71ea PS |
413 | |
414 | nskb = __vlan_put_tag(nskb, nskb->vlan_proto, vlan_tx_tag_get(nskb)); | |
415 | if (!nskb) | |
416 | return -ENOMEM; | |
417 | ||
418 | vlan_set_tci(nskb, 0); | |
7257b535 | 419 | |
6161d3fd JG |
420 | skb = nskb; |
421 | } | |
422 | ||
423 | if (nla_attr_size(skb->len) > USHRT_MAX) { | |
424 | err = -EFBIG; | |
425 | goto out; | |
426 | } | |
7257b535 | 427 | |
0afa2373 | 428 | user_skb = genlmsg_new(upcall_msg_size(skb, upcall_info->userdata), GFP_ATOMIC); |
6161d3fd JG |
429 | if (!user_skb) { |
430 | err = -ENOMEM; | |
431 | goto out; | |
432 | } | |
7257b535 BP |
433 | |
434 | upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family, | |
435 | 0, upcall_info->cmd); | |
436 | upcall->dp_ifindex = dp_ifindex; | |
437 | ||
438 | nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY); | |
a097c0b2 | 439 | ovs_nla_put_flow(upcall_info->key, upcall_info->key, user_skb); |
7257b535 BP |
440 | nla_nest_end(user_skb, nla); |
441 | ||
442 | if (upcall_info->userdata) | |
e995e3df | 443 | __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA, |
462a988b | 444 | nla_len(upcall_info->userdata), |
e995e3df | 445 | nla_data(upcall_info->userdata)); |
7257b535 BP |
446 | |
447 | nla = __nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, skb->len); | |
bed53bd1 PS |
448 | |
449 | skb_copy_and_csum_dev(skb, nla_data(nla)); | |
7257b535 | 450 | |
c39b1a5c | 451 | genlmsg_end(user_skb, upcall); |
28aea917 | 452 | err = genlmsg_unicast(net, user_skb, upcall_info->portid); |
6161d3fd JG |
453 | |
454 | out: | |
455 | kfree_skb(nskb); | |
456 | return err; | |
cb5087ca BP |
457 | } |
458 | ||
cd2a59e9 | 459 | /* Called with ovs_mutex. */ |
2a4999f3 | 460 | static int flush_flows(struct datapath *dp) |
064af421 | 461 | { |
3544358a PS |
462 | struct flow_table *old_table; |
463 | struct flow_table *new_table; | |
8d5ebd83 | 464 | |
cd2a59e9 | 465 | old_table = ovsl_dereference(dp->table); |
850b6b3b | 466 | new_table = ovs_flow_tbl_alloc(TBL_MIN_BUCKETS); |
8d5ebd83 | 467 | if (!new_table) |
ed099e92 | 468 | return -ENOMEM; |
8d5ebd83 JG |
469 | |
470 | rcu_assign_pointer(dp->table, new_table); | |
471 | ||
a1c564be | 472 | ovs_flow_tbl_destroy(old_table, true); |
ed099e92 | 473 | return 0; |
064af421 BP |
474 | } |
475 | ||
064af421 BP |
476 | static void clear_stats(struct sw_flow *flow) |
477 | { | |
6bfafa55 | 478 | flow->used = 0; |
064af421 | 479 | flow->tcp_flags = 0; |
064af421 BP |
480 | flow->packet_count = 0; |
481 | flow->byte_count = 0; | |
482 | } | |
483 | ||
df2c07f4 | 484 | static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info) |
064af421 | 485 | { |
df2c07f4 | 486 | struct ovs_header *ovs_header = info->userhdr; |
982b8810 | 487 | struct nlattr **a = info->attrs; |
e0e57990 | 488 | struct sw_flow_actions *acts; |
982b8810 | 489 | struct sk_buff *packet; |
e0e57990 | 490 | struct sw_flow *flow; |
f7cd0081 | 491 | struct datapath *dp; |
d6569377 | 492 | struct ethhdr *eth; |
3f19d399 | 493 | int len; |
d6569377 | 494 | int err; |
064af421 | 495 | |
f7cd0081 | 496 | err = -EINVAL; |
df2c07f4 | 497 | if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] || |
7c3072cc | 498 | !a[OVS_PACKET_ATTR_ACTIONS]) |
e5cad958 | 499 | goto err; |
064af421 | 500 | |
df2c07f4 | 501 | len = nla_len(a[OVS_PACKET_ATTR_PACKET]); |
3f19d399 | 502 | packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL); |
f7cd0081 BP |
503 | err = -ENOMEM; |
504 | if (!packet) | |
e5cad958 | 505 | goto err; |
3f19d399 BP |
506 | skb_reserve(packet, NET_IP_ALIGN); |
507 | ||
bf3d6fce | 508 | nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len); |
8d5ebd83 | 509 | |
f7cd0081 BP |
510 | skb_reset_mac_header(packet); |
511 | eth = eth_hdr(packet); | |
064af421 | 512 | |
d6569377 BP |
513 | /* Normally, setting the skb 'protocol' field would be handled by a |
514 | * call to eth_type_trans(), but it assumes there's a sending | |
515 | * device, which we may not have. */ | |
7cd46155 | 516 | if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN) |
f7cd0081 | 517 | packet->protocol = eth->h_proto; |
d6569377 | 518 | else |
f7cd0081 | 519 | packet->protocol = htons(ETH_P_802_2); |
d3c54451 | 520 | |
e0e57990 | 521 | /* Build an sw_flow for sending this packet. */ |
850b6b3b | 522 | flow = ovs_flow_alloc(); |
e0e57990 BP |
523 | err = PTR_ERR(flow); |
524 | if (IS_ERR(flow)) | |
e5cad958 | 525 | goto err_kfree_skb; |
064af421 | 526 | |
a1c564be | 527 | err = ovs_flow_extract(packet, -1, &flow->key); |
e0e57990 | 528 | if (err) |
9321954a | 529 | goto err_flow_free; |
e0e57990 | 530 | |
a097c0b2 | 531 | err = ovs_nla_get_flow_metadata(flow, a[OVS_PACKET_ATTR_KEY]); |
80e5eed9 | 532 | if (err) |
9321954a | 533 | goto err_flow_free; |
a097c0b2 | 534 | acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_PACKET_ATTR_ACTIONS])); |
e0e57990 BP |
535 | err = PTR_ERR(acts); |
536 | if (IS_ERR(acts)) | |
9321954a | 537 | goto err_flow_free; |
9b405f1a | 538 | |
a097c0b2 PS |
539 | err = ovs_nla_copy_actions(a[OVS_PACKET_ATTR_ACTIONS], |
540 | &flow->key, 0, &acts); | |
e0e57990 | 541 | rcu_assign_pointer(flow->sf_acts, acts); |
9b405f1a PS |
542 | if (err) |
543 | goto err_flow_free; | |
e0e57990 BP |
544 | |
545 | OVS_CB(packet)->flow = flow; | |
d1d71a36 | 546 | OVS_CB(packet)->pkt_key = &flow->key; |
abff858b | 547 | packet->priority = flow->key.phy.priority; |
3025a772 | 548 | packet->mark = flow->key.phy.skb_mark; |
e0e57990 | 549 | |
d6569377 | 550 | rcu_read_lock(); |
2a4999f3 | 551 | dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex); |
f7cd0081 | 552 | err = -ENODEV; |
e5cad958 BP |
553 | if (!dp) |
554 | goto err_unlock; | |
cc4015df | 555 | |
e9141eec | 556 | local_bh_disable(); |
850b6b3b | 557 | err = ovs_execute_actions(dp, packet); |
e9141eec | 558 | local_bh_enable(); |
d6569377 | 559 | rcu_read_unlock(); |
e0e57990 | 560 | |
a1c564be | 561 | ovs_flow_free(flow, false); |
e5cad958 | 562 | return err; |
064af421 | 563 | |
e5cad958 BP |
564 | err_unlock: |
565 | rcu_read_unlock(); | |
9321954a | 566 | err_flow_free: |
a1c564be | 567 | ovs_flow_free(flow, false); |
e5cad958 BP |
568 | err_kfree_skb: |
569 | kfree_skb(packet); | |
570 | err: | |
d6569377 | 571 | return err; |
064af421 BP |
572 | } |
573 | ||
df2c07f4 | 574 | static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = { |
7c3072cc | 575 | [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN }, |
df2c07f4 JP |
576 | [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED }, |
577 | [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED }, | |
982b8810 BP |
578 | }; |
579 | ||
580 | static struct genl_ops dp_packet_genl_ops[] = { | |
df2c07f4 | 581 | { .cmd = OVS_PACKET_CMD_EXECUTE, |
982b8810 BP |
582 | .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ |
583 | .policy = packet_policy, | |
df2c07f4 | 584 | .doit = ovs_packet_cmd_execute |
982b8810 BP |
585 | } |
586 | }; | |
587 | ||
df2c07f4 | 588 | static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats) |
064af421 | 589 | { |
fb93e9aa | 590 | struct flow_table *table; |
d6569377 | 591 | int i; |
f180c2e2 | 592 | |
fb93e9aa | 593 | table = rcu_dereference_check(dp->table, lockdep_ovsl_is_held()); |
850b6b3b | 594 | stats->n_flows = ovs_flow_tbl_count(table); |
064af421 | 595 | |
7257b535 | 596 | stats->n_hit = stats->n_missed = stats->n_lost = 0; |
d6569377 BP |
597 | for_each_possible_cpu(i) { |
598 | const struct dp_stats_percpu *percpu_stats; | |
599 | struct dp_stats_percpu local_stats; | |
821cb9fa | 600 | unsigned int start; |
44e05eca | 601 | |
d6569377 | 602 | percpu_stats = per_cpu_ptr(dp->stats_percpu, i); |
064af421 | 603 | |
d6569377 | 604 | do { |
821cb9fa | 605 | start = u64_stats_fetch_begin_bh(&percpu_stats->sync); |
d6569377 | 606 | local_stats = *percpu_stats; |
821cb9fa | 607 | } while (u64_stats_fetch_retry_bh(&percpu_stats->sync, start)); |
064af421 | 608 | |
d6569377 BP |
609 | stats->n_hit += local_stats.n_hit; |
610 | stats->n_missed += local_stats.n_missed; | |
611 | stats->n_lost += local_stats.n_lost; | |
612 | } | |
613 | } | |
064af421 | 614 | |
df2c07f4 JP |
615 | static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = { |
616 | [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED }, | |
617 | [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED }, | |
618 | [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG }, | |
d6569377 | 619 | }; |
36956a7d | 620 | |
37a1300c BP |
621 | static struct genl_family dp_flow_genl_family = { |
622 | .id = GENL_ID_GENERATE, | |
df2c07f4 JP |
623 | .hdrsize = sizeof(struct ovs_header), |
624 | .name = OVS_FLOW_FAMILY, | |
69685a88 | 625 | .version = OVS_FLOW_VERSION, |
2a4999f3 | 626 | .maxattr = OVS_FLOW_ATTR_MAX, |
b3dcb73c | 627 | .netnsok = true, |
14002a59 | 628 | SET_PARALLEL_OPS |
37a1300c | 629 | }; |
ed099e92 | 630 | |
850b6b3b | 631 | static struct genl_multicast_group ovs_dp_flow_multicast_group = { |
df2c07f4 | 632 | .name = OVS_FLOW_MCGROUP |
37a1300c BP |
633 | }; |
634 | ||
0afa2373 TG |
635 | static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts) |
636 | { | |
637 | return NLMSG_ALIGN(sizeof(struct ovs_header)) | |
638 | + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_KEY */ | |
a1c564be | 639 | + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_MASK */ |
0afa2373 TG |
640 | + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */ |
641 | + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */ | |
642 | + nla_total_size(8) /* OVS_FLOW_ATTR_USED */ | |
643 | + nla_total_size(acts->actions_len); /* OVS_FLOW_ATTR_ACTIONS */ | |
644 | } | |
645 | ||
cd2a59e9 | 646 | /* Called with ovs_mutex. */ |
df2c07f4 | 647 | static int ovs_flow_cmd_fill_info(struct sw_flow *flow, struct datapath *dp, |
28aea917 | 648 | struct sk_buff *skb, u32 portid, |
6455100f | 649 | u32 seq, u32 flags, u8 cmd) |
d6569377 | 650 | { |
37a1300c | 651 | const int skb_orig_len = skb->len; |
9b405f1a | 652 | struct nlattr *start; |
df2c07f4 JP |
653 | struct ovs_flow_stats stats; |
654 | struct ovs_header *ovs_header; | |
d6569377 BP |
655 | struct nlattr *nla; |
656 | unsigned long used; | |
657 | u8 tcp_flags; | |
658 | int err; | |
064af421 | 659 | |
28aea917 | 660 | ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd); |
df2c07f4 | 661 | if (!ovs_header) |
37a1300c | 662 | return -EMSGSIZE; |
d6569377 | 663 | |
99769a40 | 664 | ovs_header->dp_ifindex = get_dpifindex(dp); |
d6569377 | 665 | |
a1c564be | 666 | /* Fill flow key. */ |
df2c07f4 | 667 | nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY); |
d6569377 BP |
668 | if (!nla) |
669 | goto nla_put_failure; | |
a1c564be | 670 | |
a097c0b2 | 671 | err = ovs_nla_put_flow(&flow->unmasked_key, &flow->unmasked_key, skb); |
d6569377 | 672 | if (err) |
37a1300c | 673 | goto error; |
d6569377 BP |
674 | nla_nest_end(skb, nla); |
675 | ||
a1c564be AZ |
676 | nla = nla_nest_start(skb, OVS_FLOW_ATTR_MASK); |
677 | if (!nla) | |
678 | goto nla_put_failure; | |
679 | ||
a097c0b2 | 680 | err = ovs_nla_put_flow(&flow->key, &flow->mask->key, skb); |
a1c564be AZ |
681 | if (err) |
682 | goto error; | |
683 | ||
684 | nla_nest_end(skb, nla); | |
685 | ||
d6569377 BP |
686 | spin_lock_bh(&flow->lock); |
687 | used = flow->used; | |
688 | stats.n_packets = flow->packet_count; | |
689 | stats.n_bytes = flow->byte_count; | |
690 | tcp_flags = flow->tcp_flags; | |
691 | spin_unlock_bh(&flow->lock); | |
692 | ||
c3cc8c03 DM |
693 | if (used && |
694 | nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used))) | |
695 | goto nla_put_failure; | |
d6569377 | 696 | |
c3cc8c03 DM |
697 | if (stats.n_packets && |
698 | nla_put(skb, OVS_FLOW_ATTR_STATS, | |
699 | sizeof(struct ovs_flow_stats), &stats)) | |
700 | goto nla_put_failure; | |
d6569377 | 701 | |
c3cc8c03 DM |
702 | if (tcp_flags && |
703 | nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, tcp_flags)) | |
704 | goto nla_put_failure; | |
d6569377 | 705 | |
df2c07f4 | 706 | /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if |
30053024 BP |
707 | * this is the first flow to be dumped into 'skb'. This is unusual for |
708 | * Netlink but individual action lists can be longer than | |
709 | * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this. | |
710 | * The userspace caller can always fetch the actions separately if it | |
711 | * really wants them. (Most userspace callers in fact don't care.) | |
712 | * | |
713 | * This can only fail for dump operations because the skb is always | |
714 | * properly sized for single flows. | |
715 | */ | |
9b405f1a | 716 | start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS); |
f6f481ee | 717 | if (start) { |
f44ccce1 PS |
718 | const struct sw_flow_actions *sf_acts; |
719 | ||
720 | sf_acts = rcu_dereference_check(flow->sf_acts, | |
721 | lockdep_ovsl_is_held()); | |
722 | ||
a097c0b2 PS |
723 | err = ovs_nla_put_actions(sf_acts->actions, |
724 | sf_acts->actions_len, skb); | |
0a25b039 BP |
725 | if (!err) |
726 | nla_nest_end(skb, start); | |
727 | else { | |
728 | if (skb_orig_len) | |
729 | goto error; | |
730 | ||
731 | nla_nest_cancel(skb, start); | |
732 | } | |
7aac03bd JG |
733 | } else if (skb_orig_len) |
734 | goto nla_put_failure; | |
37a1300c | 735 | |
df2c07f4 | 736 | return genlmsg_end(skb, ovs_header); |
d6569377 BP |
737 | |
738 | nla_put_failure: | |
739 | err = -EMSGSIZE; | |
37a1300c | 740 | error: |
df2c07f4 | 741 | genlmsg_cancel(skb, ovs_header); |
d6569377 | 742 | return err; |
44e05eca BP |
743 | } |
744 | ||
df2c07f4 | 745 | static struct sk_buff *ovs_flow_cmd_alloc_info(struct sw_flow *flow) |
44e05eca | 746 | { |
37a1300c | 747 | const struct sw_flow_actions *sf_acts; |
d6569377 | 748 | |
cd2a59e9 | 749 | sf_acts = ovsl_dereference(flow->sf_acts); |
d6569377 | 750 | |
0afa2373 | 751 | return genlmsg_new(ovs_flow_cmd_msg_size(sf_acts), GFP_KERNEL); |
37a1300c | 752 | } |
8d5ebd83 | 753 | |
6455100f PS |
754 | static struct sk_buff *ovs_flow_cmd_build_info(struct sw_flow *flow, |
755 | struct datapath *dp, | |
28aea917 | 756 | u32 portid, u32 seq, u8 cmd) |
37a1300c BP |
757 | { |
758 | struct sk_buff *skb; | |
759 | int retval; | |
d6569377 | 760 | |
df2c07f4 | 761 | skb = ovs_flow_cmd_alloc_info(flow); |
37a1300c BP |
762 | if (!skb) |
763 | return ERR_PTR(-ENOMEM); | |
d6569377 | 764 | |
28aea917 | 765 | retval = ovs_flow_cmd_fill_info(flow, dp, skb, portid, seq, 0, cmd); |
37a1300c | 766 | BUG_ON(retval < 0); |
d6569377 | 767 | return skb; |
064af421 BP |
768 | } |
769 | ||
df2c07f4 | 770 | static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info) |
064af421 | 771 | { |
37a1300c | 772 | struct nlattr **a = info->attrs; |
df2c07f4 | 773 | struct ovs_header *ovs_header = info->userhdr; |
529db635 | 774 | struct sw_flow_key key, masked_key; |
a1c564be AZ |
775 | struct sw_flow *flow = NULL; |
776 | struct sw_flow_mask mask; | |
37a1300c | 777 | struct sk_buff *reply; |
9c52546b | 778 | struct datapath *dp; |
3544358a | 779 | struct flow_table *table; |
9b405f1a | 780 | struct sw_flow_actions *acts = NULL; |
a1c564be | 781 | struct sw_flow_match match; |
bc4a05c6 | 782 | int error; |
064af421 | 783 | |
37a1300c BP |
784 | /* Extract key. */ |
785 | error = -EINVAL; | |
df2c07f4 | 786 | if (!a[OVS_FLOW_ATTR_KEY]) |
37a1300c | 787 | goto error; |
a1c564be AZ |
788 | |
789 | ovs_match_init(&match, &key, &mask); | |
a097c0b2 PS |
790 | error = ovs_nla_get_match(&match, |
791 | a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]); | |
37a1300c BP |
792 | if (error) |
793 | goto error; | |
064af421 | 794 | |
37a1300c | 795 | /* Validate actions. */ |
df2c07f4 | 796 | if (a[OVS_FLOW_ATTR_ACTIONS]) { |
a097c0b2 | 797 | acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_FLOW_ATTR_ACTIONS])); |
9b405f1a PS |
798 | error = PTR_ERR(acts); |
799 | if (IS_ERR(acts)) | |
37a1300c | 800 | goto error; |
9b405f1a | 801 | |
a097c0b2 PS |
802 | ovs_flow_mask_key(&masked_key, &key, &mask); |
803 | error = ovs_nla_copy_actions(a[OVS_FLOW_ATTR_ACTIONS], | |
804 | &masked_key, 0, &acts); | |
529db635 JG |
805 | if (error) { |
806 | OVS_NLERR("Flow actions may not be safe on all matching packets.\n"); | |
9b405f1a | 807 | goto err_kfree; |
529db635 | 808 | } |
df2c07f4 | 809 | } else if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW) { |
37a1300c BP |
810 | error = -EINVAL; |
811 | goto error; | |
812 | } | |
813 | ||
cd2a59e9 | 814 | ovs_lock(); |
2a4999f3 | 815 | dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex); |
d6569377 | 816 | error = -ENODEV; |
9c52546b | 817 | if (!dp) |
cd2a59e9 | 818 | goto err_unlock_ovs; |
704a1e09 | 819 | |
cd2a59e9 | 820 | table = ovsl_dereference(dp->table); |
a1c564be AZ |
821 | |
822 | /* Check if this is a duplicate flow */ | |
a097c0b2 | 823 | flow = ovs_flow_tbl_lookup(table, &key); |
3544358a | 824 | if (!flow) { |
fb98bcac | 825 | struct flow_table *new_table = NULL; |
a1c564be | 826 | struct sw_flow_mask *mask_p; |
fb98bcac | 827 | |
d6569377 BP |
828 | /* Bail out if we're not allowed to create a new flow. */ |
829 | error = -ENOENT; | |
df2c07f4 | 830 | if (info->genlhdr->cmd == OVS_FLOW_CMD_SET) |
cd2a59e9 | 831 | goto err_unlock_ovs; |
d6569377 BP |
832 | |
833 | /* Expand table, if necessary, to make room. */ | |
fb98bcac | 834 | if (ovs_flow_tbl_need_to_expand(table)) |
850b6b3b | 835 | new_table = ovs_flow_tbl_expand(table); |
fb98bcac PS |
836 | else if (time_after(jiffies, dp->last_rehash + REHASH_FLOW_INTERVAL)) |
837 | new_table = ovs_flow_tbl_rehash(table); | |
838 | ||
839 | if (new_table && !IS_ERR(new_table)) { | |
840 | rcu_assign_pointer(dp->table, new_table); | |
841 | ovs_flow_tbl_destroy(table, true); | |
842 | table = ovsl_dereference(dp->table); | |
843 | dp->last_rehash = jiffies; | |
d6569377 BP |
844 | } |
845 | ||
846 | /* Allocate flow. */ | |
850b6b3b | 847 | flow = ovs_flow_alloc(); |
d6569377 BP |
848 | if (IS_ERR(flow)) { |
849 | error = PTR_ERR(flow); | |
cd2a59e9 | 850 | goto err_unlock_ovs; |
d6569377 | 851 | } |
d6569377 BP |
852 | clear_stats(flow); |
853 | ||
529db635 JG |
854 | flow->key = masked_key; |
855 | flow->unmasked_key = key; | |
856 | ||
a1c564be AZ |
857 | /* Make sure mask is unique in the system */ |
858 | mask_p = ovs_sw_flow_mask_find(table, &mask); | |
859 | if (!mask_p) { | |
860 | /* Allocate a new mask if none exsits. */ | |
861 | mask_p = ovs_sw_flow_mask_alloc(); | |
862 | if (!mask_p) | |
863 | goto err_flow_free; | |
864 | mask_p->key = mask.key; | |
865 | mask_p->range = mask.range; | |
866 | ovs_sw_flow_mask_insert(table, mask_p); | |
867 | } | |
868 | ||
869 | ovs_sw_flow_mask_add_ref(mask_p); | |
3d097064 | 870 | flow->mask = mask_p; |
d6569377 BP |
871 | rcu_assign_pointer(flow->sf_acts, acts); |
872 | ||
d6569377 | 873 | /* Put flow in bucket. */ |
a097c0b2 | 874 | ovs_flow_tbl_insert(table, flow); |
37a1300c | 875 | |
28aea917 | 876 | reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid, |
a1c564be | 877 | info->snd_seq, OVS_FLOW_CMD_NEW); |
d6569377 BP |
878 | } else { |
879 | /* We found a matching flow. */ | |
880 | struct sw_flow_actions *old_acts; | |
881 | ||
882 | /* Bail out if we're not allowed to modify an existing flow. | |
883 | * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL | |
884 | * because Generic Netlink treats the latter as a dump | |
885 | * request. We also accept NLM_F_EXCL in case that bug ever | |
886 | * gets fixed. | |
887 | */ | |
888 | error = -EEXIST; | |
df2c07f4 | 889 | if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW && |
37a1300c | 890 | info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL)) |
cd2a59e9 | 891 | goto err_unlock_ovs; |
d6569377 | 892 | |
b21e5b6a AZ |
893 | /* The unmasked key has to be the same for flow updates. */ |
894 | error = -EINVAL; | |
a097c0b2 | 895 | if (!ovs_flow_cmp_unmasked_key(flow, &match)) { |
1b936472 | 896 | OVS_NLERR("Flow modification message rejected, unmasked key does not match.\n"); |
b21e5b6a | 897 | goto err_unlock_ovs; |
1b936472 | 898 | } |
b21e5b6a | 899 | |
d6569377 | 900 | /* Update actions. */ |
cd2a59e9 | 901 | old_acts = ovsl_dereference(flow->sf_acts); |
9b405f1a | 902 | rcu_assign_pointer(flow->sf_acts, acts); |
a097c0b2 | 903 | ovs_nla_free_flow_actions(old_acts); |
d6569377 | 904 | |
28aea917 | 905 | reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid, |
6455100f | 906 | info->snd_seq, OVS_FLOW_CMD_NEW); |
d6569377 BP |
907 | |
908 | /* Clear stats. */ | |
df2c07f4 | 909 | if (a[OVS_FLOW_ATTR_CLEAR]) { |
d6569377 BP |
910 | spin_lock_bh(&flow->lock); |
911 | clear_stats(flow); | |
912 | spin_unlock_bh(&flow->lock); | |
913 | } | |
9c52546b | 914 | } |
cd2a59e9 | 915 | ovs_unlock(); |
37a1300c BP |
916 | |
917 | if (!IS_ERR(reply)) | |
e297c6b7 | 918 | ovs_notify(reply, info, &ovs_dp_flow_multicast_group); |
37a1300c | 919 | else |
b3dcb73c | 920 | netlink_set_err(sock_net(skb->sk)->genl_sock, 0, |
2a4999f3 | 921 | ovs_dp_flow_multicast_group.id, PTR_ERR(reply)); |
d6569377 | 922 | return 0; |
704a1e09 | 923 | |
a1c564be AZ |
924 | err_flow_free: |
925 | ovs_flow_free(flow, false); | |
cd2a59e9 PS |
926 | err_unlock_ovs: |
927 | ovs_unlock(); | |
9b405f1a | 928 | err_kfree: |
ba400435 | 929 | kfree(acts); |
37a1300c | 930 | error: |
9c52546b | 931 | return error; |
704a1e09 BP |
932 | } |
933 | ||
df2c07f4 | 934 | static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info) |
704a1e09 | 935 | { |
37a1300c | 936 | struct nlattr **a = info->attrs; |
df2c07f4 | 937 | struct ovs_header *ovs_header = info->userhdr; |
37a1300c | 938 | struct sw_flow_key key; |
37a1300c | 939 | struct sk_buff *reply; |
704a1e09 | 940 | struct sw_flow *flow; |
9c52546b | 941 | struct datapath *dp; |
3544358a | 942 | struct flow_table *table; |
a1c564be | 943 | struct sw_flow_match match; |
9c52546b | 944 | int err; |
704a1e09 | 945 | |
1b936472 AZ |
946 | if (!a[OVS_FLOW_ATTR_KEY]) { |
947 | OVS_NLERR("Flow get message rejected, Key attribute missing.\n"); | |
37a1300c | 948 | return -EINVAL; |
1b936472 | 949 | } |
a1c564be AZ |
950 | |
951 | ovs_match_init(&match, &key, NULL); | |
a097c0b2 | 952 | err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL); |
37a1300c BP |
953 | if (err) |
954 | return err; | |
704a1e09 | 955 | |
cd2a59e9 | 956 | ovs_lock(); |
2a4999f3 | 957 | dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex); |
cd2a59e9 PS |
958 | if (!dp) { |
959 | err = -ENODEV; | |
960 | goto unlock; | |
961 | } | |
704a1e09 | 962 | |
cd2a59e9 | 963 | table = ovsl_dereference(dp->table); |
a097c0b2 PS |
964 | flow = ovs_flow_tbl_lookup(table, &key); |
965 | if (!flow || !ovs_flow_cmp_unmasked_key(flow, &match)) { | |
cd2a59e9 PS |
966 | err = -ENOENT; |
967 | goto unlock; | |
968 | } | |
d6569377 | 969 | |
28aea917 | 970 | reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid, |
6455100f | 971 | info->snd_seq, OVS_FLOW_CMD_NEW); |
cd2a59e9 PS |
972 | if (IS_ERR(reply)) { |
973 | err = PTR_ERR(reply); | |
974 | goto unlock; | |
975 | } | |
36956a7d | 976 | |
cd2a59e9 | 977 | ovs_unlock(); |
37a1300c | 978 | return genlmsg_reply(reply, info); |
cd2a59e9 PS |
979 | unlock: |
980 | ovs_unlock(); | |
981 | return err; | |
d6569377 | 982 | } |
9c52546b | 983 | |
df2c07f4 | 984 | static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info) |
d6569377 | 985 | { |
37a1300c | 986 | struct nlattr **a = info->attrs; |
df2c07f4 | 987 | struct ovs_header *ovs_header = info->userhdr; |
37a1300c | 988 | struct sw_flow_key key; |
37a1300c | 989 | struct sk_buff *reply; |
d6569377 | 990 | struct sw_flow *flow; |
d6569377 | 991 | struct datapath *dp; |
3544358a | 992 | struct flow_table *table; |
a1c564be | 993 | struct sw_flow_match match; |
d6569377 | 994 | int err; |
36956a7d | 995 | |
cd2a59e9 | 996 | ovs_lock(); |
2a4999f3 | 997 | dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex); |
cd2a59e9 PS |
998 | if (!dp) { |
999 | err = -ENODEV; | |
1000 | goto unlock; | |
1001 | } | |
2a4999f3 | 1002 | |
cd2a59e9 PS |
1003 | if (!a[OVS_FLOW_ATTR_KEY]) { |
1004 | err = flush_flows(dp); | |
1005 | goto unlock; | |
1006 | } | |
a1c564be AZ |
1007 | |
1008 | ovs_match_init(&match, &key, NULL); | |
a097c0b2 | 1009 | err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL); |
37a1300c | 1010 | if (err) |
cd2a59e9 | 1011 | goto unlock; |
d6569377 | 1012 | |
cd2a59e9 | 1013 | table = ovsl_dereference(dp->table); |
a097c0b2 PS |
1014 | flow = ovs_flow_tbl_lookup(table, &key); |
1015 | if (!flow || !ovs_flow_cmp_unmasked_key(flow, &match)) { | |
cd2a59e9 PS |
1016 | err = -ENOENT; |
1017 | goto unlock; | |
1018 | } | |
d6569377 | 1019 | |
df2c07f4 | 1020 | reply = ovs_flow_cmd_alloc_info(flow); |
cd2a59e9 PS |
1021 | if (!reply) { |
1022 | err = -ENOMEM; | |
1023 | goto unlock; | |
1024 | } | |
37a1300c | 1025 | |
a097c0b2 | 1026 | ovs_flow_tbl_remove(table, flow); |
37a1300c | 1027 | |
28aea917 | 1028 | err = ovs_flow_cmd_fill_info(flow, dp, reply, info->snd_portid, |
df2c07f4 | 1029 | info->snd_seq, 0, OVS_FLOW_CMD_DEL); |
37a1300c BP |
1030 | BUG_ON(err < 0); |
1031 | ||
a1c564be | 1032 | ovs_flow_free(flow, true); |
cd2a59e9 | 1033 | ovs_unlock(); |
37a1300c | 1034 | |
e297c6b7 | 1035 | ovs_notify(reply, info, &ovs_dp_flow_multicast_group); |
37a1300c | 1036 | return 0; |
cd2a59e9 PS |
1037 | unlock: |
1038 | ovs_unlock(); | |
1039 | return err; | |
37a1300c BP |
1040 | } |
1041 | ||
df2c07f4 | 1042 | static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb) |
37a1300c | 1043 | { |
df2c07f4 | 1044 | struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh)); |
37a1300c | 1045 | struct datapath *dp; |
20d035b2 | 1046 | struct flow_table *table; |
37a1300c | 1047 | |
f44ccce1 | 1048 | rcu_read_lock(); |
2a4999f3 | 1049 | dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex); |
cd2a59e9 | 1050 | if (!dp) { |
f44ccce1 | 1051 | rcu_read_unlock(); |
37a1300c | 1052 | return -ENODEV; |
cd2a59e9 | 1053 | } |
37a1300c | 1054 | |
f44ccce1 | 1055 | table = rcu_dereference(dp->table); |
37a1300c | 1056 | for (;;) { |
37a1300c BP |
1057 | struct sw_flow *flow; |
1058 | u32 bucket, obj; | |
1059 | ||
1060 | bucket = cb->args[0]; | |
1061 | obj = cb->args[1]; | |
a097c0b2 | 1062 | flow = ovs_flow_tbl_dump_next(table, &bucket, &obj); |
3544358a | 1063 | if (!flow) |
37a1300c BP |
1064 | break; |
1065 | ||
6455100f | 1066 | if (ovs_flow_cmd_fill_info(flow, dp, skb, |
28aea917 | 1067 | NETLINK_CB(cb->skb).portid, |
37a1300c | 1068 | cb->nlh->nlmsg_seq, NLM_F_MULTI, |
df2c07f4 | 1069 | OVS_FLOW_CMD_NEW) < 0) |
37a1300c BP |
1070 | break; |
1071 | ||
1072 | cb->args[0] = bucket; | |
1073 | cb->args[1] = obj; | |
1074 | } | |
f44ccce1 | 1075 | rcu_read_unlock(); |
37a1300c | 1076 | return skb->len; |
704a1e09 BP |
1077 | } |
1078 | ||
37a1300c | 1079 | static struct genl_ops dp_flow_genl_ops[] = { |
df2c07f4 | 1080 | { .cmd = OVS_FLOW_CMD_NEW, |
37a1300c BP |
1081 | .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ |
1082 | .policy = flow_policy, | |
df2c07f4 | 1083 | .doit = ovs_flow_cmd_new_or_set |
37a1300c | 1084 | }, |
df2c07f4 | 1085 | { .cmd = OVS_FLOW_CMD_DEL, |
37a1300c BP |
1086 | .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ |
1087 | .policy = flow_policy, | |
df2c07f4 | 1088 | .doit = ovs_flow_cmd_del |
37a1300c | 1089 | }, |
df2c07f4 | 1090 | { .cmd = OVS_FLOW_CMD_GET, |
37a1300c BP |
1091 | .flags = 0, /* OK for unprivileged users. */ |
1092 | .policy = flow_policy, | |
df2c07f4 JP |
1093 | .doit = ovs_flow_cmd_get, |
1094 | .dumpit = ovs_flow_cmd_dump | |
37a1300c | 1095 | }, |
df2c07f4 | 1096 | { .cmd = OVS_FLOW_CMD_SET, |
37a1300c BP |
1097 | .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ |
1098 | .policy = flow_policy, | |
df2c07f4 | 1099 | .doit = ovs_flow_cmd_new_or_set, |
37a1300c BP |
1100 | }, |
1101 | }; | |
1102 | ||
df2c07f4 | 1103 | static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = { |
df2c07f4 | 1104 | [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 }, |
b063d9f0 | 1105 | [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 }, |
d6569377 BP |
1106 | }; |
1107 | ||
aaff4b55 BP |
1108 | static struct genl_family dp_datapath_genl_family = { |
1109 | .id = GENL_ID_GENERATE, | |
df2c07f4 JP |
1110 | .hdrsize = sizeof(struct ovs_header), |
1111 | .name = OVS_DATAPATH_FAMILY, | |
69685a88 | 1112 | .version = OVS_DATAPATH_VERSION, |
2a4999f3 | 1113 | .maxattr = OVS_DP_ATTR_MAX, |
b3dcb73c | 1114 | .netnsok = true, |
14002a59 | 1115 | SET_PARALLEL_OPS |
aaff4b55 BP |
1116 | }; |
1117 | ||
850b6b3b | 1118 | static struct genl_multicast_group ovs_dp_datapath_multicast_group = { |
df2c07f4 | 1119 | .name = OVS_DATAPATH_MCGROUP |
aaff4b55 BP |
1120 | }; |
1121 | ||
0afa2373 TG |
1122 | static size_t ovs_dp_cmd_msg_size(void) |
1123 | { | |
1124 | size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header)); | |
1125 | ||
1126 | msgsize += nla_total_size(IFNAMSIZ); | |
1127 | msgsize += nla_total_size(sizeof(struct ovs_dp_stats)); | |
1128 | ||
1129 | return msgsize; | |
1130 | } | |
1131 | ||
df2c07f4 | 1132 | static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb, |
28aea917 | 1133 | u32 portid, u32 seq, u32 flags, u8 cmd) |
064af421 | 1134 | { |
df2c07f4 | 1135 | struct ovs_header *ovs_header; |
e926dfe3 | 1136 | struct ovs_dp_stats dp_stats; |
064af421 BP |
1137 | int err; |
1138 | ||
28aea917 | 1139 | ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family, |
aaff4b55 | 1140 | flags, cmd); |
df2c07f4 | 1141 | if (!ovs_header) |
aaff4b55 | 1142 | goto error; |
064af421 | 1143 | |
b063d9f0 | 1144 | ovs_header->dp_ifindex = get_dpifindex(dp); |
064af421 | 1145 | |
d6569377 | 1146 | rcu_read_lock(); |
850b6b3b | 1147 | err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp)); |
d6569377 | 1148 | rcu_read_unlock(); |
064af421 | 1149 | if (err) |
d6569377 | 1150 | goto nla_put_failure; |
064af421 | 1151 | |
e926dfe3 | 1152 | get_dp_stats(dp, &dp_stats); |
c3cc8c03 DM |
1153 | if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats), &dp_stats)) |
1154 | goto nla_put_failure; | |
d6569377 | 1155 | |
df2c07f4 | 1156 | return genlmsg_end(skb, ovs_header); |
d6569377 BP |
1157 | |
1158 | nla_put_failure: | |
df2c07f4 | 1159 | genlmsg_cancel(skb, ovs_header); |
aaff4b55 BP |
1160 | error: |
1161 | return -EMSGSIZE; | |
d6569377 BP |
1162 | } |
1163 | ||
28aea917 | 1164 | static struct sk_buff *ovs_dp_cmd_build_info(struct datapath *dp, u32 portid, |
aaff4b55 | 1165 | u32 seq, u8 cmd) |
d6569377 | 1166 | { |
d6569377 | 1167 | struct sk_buff *skb; |
aaff4b55 | 1168 | int retval; |
d6569377 | 1169 | |
0afa2373 | 1170 | skb = genlmsg_new(ovs_dp_cmd_msg_size(), GFP_KERNEL); |
064af421 | 1171 | if (!skb) |
d6569377 | 1172 | return ERR_PTR(-ENOMEM); |
659586ef | 1173 | |
28aea917 | 1174 | retval = ovs_dp_cmd_fill_info(dp, skb, portid, seq, 0, cmd); |
aaff4b55 BP |
1175 | if (retval < 0) { |
1176 | kfree_skb(skb); | |
1177 | return ERR_PTR(retval); | |
1178 | } | |
1179 | return skb; | |
1180 | } | |
9dca7bd5 | 1181 | |
cd2a59e9 | 1182 | /* Called with ovs_mutex. */ |
2a4999f3 PS |
1183 | static struct datapath *lookup_datapath(struct net *net, |
1184 | struct ovs_header *ovs_header, | |
6455100f | 1185 | struct nlattr *a[OVS_DP_ATTR_MAX + 1]) |
d6569377 | 1186 | { |
254f2dc8 BP |
1187 | struct datapath *dp; |
1188 | ||
df2c07f4 | 1189 | if (!a[OVS_DP_ATTR_NAME]) |
2a4999f3 | 1190 | dp = get_dp(net, ovs_header->dp_ifindex); |
254f2dc8 | 1191 | else { |
d6569377 | 1192 | struct vport *vport; |
d6569377 | 1193 | |
057dd6d2 | 1194 | rcu_read_lock(); |
2a4999f3 | 1195 | vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME])); |
df2c07f4 | 1196 | dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL; |
057dd6d2 | 1197 | rcu_read_unlock(); |
d6569377 | 1198 | } |
254f2dc8 | 1199 | return dp ? dp : ERR_PTR(-ENODEV); |
d6569377 BP |
1200 | } |
1201 | ||
df2c07f4 | 1202 | static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info) |
d6569377 | 1203 | { |
aaff4b55 | 1204 | struct nlattr **a = info->attrs; |
d6569377 | 1205 | struct vport_parms parms; |
aaff4b55 | 1206 | struct sk_buff *reply; |
d6569377 BP |
1207 | struct datapath *dp; |
1208 | struct vport *vport; | |
2a4999f3 | 1209 | struct ovs_net *ovs_net; |
95b1d73a | 1210 | int err, i; |
d6569377 | 1211 | |
d6569377 | 1212 | err = -EINVAL; |
ea36840f | 1213 | if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID]) |
aaff4b55 BP |
1214 | goto err; |
1215 | ||
cd2a59e9 | 1216 | ovs_lock(); |
d6569377 | 1217 | |
d6569377 BP |
1218 | err = -ENOMEM; |
1219 | dp = kzalloc(sizeof(*dp), GFP_KERNEL); | |
1220 | if (dp == NULL) | |
cd2a59e9 | 1221 | goto err_unlock_ovs; |
2a4999f3 | 1222 | |
0ceaa66c JG |
1223 | ovs_dp_set_net(dp, hold_net(sock_net(skb->sk))); |
1224 | ||
d6569377 BP |
1225 | /* Allocate table. */ |
1226 | err = -ENOMEM; | |
850b6b3b | 1227 | rcu_assign_pointer(dp->table, ovs_flow_tbl_alloc(TBL_MIN_BUCKETS)); |
d6569377 BP |
1228 | if (!dp->table) |
1229 | goto err_free_dp; | |
1230 | ||
99769a40 JG |
1231 | dp->stats_percpu = alloc_percpu(struct dp_stats_percpu); |
1232 | if (!dp->stats_percpu) { | |
1233 | err = -ENOMEM; | |
1234 | goto err_destroy_table; | |
1235 | } | |
1236 | ||
95b1d73a PS |
1237 | dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head), |
1238 | GFP_KERNEL); | |
1239 | if (!dp->ports) { | |
1240 | err = -ENOMEM; | |
1241 | goto err_destroy_percpu; | |
1242 | } | |
1243 | ||
1244 | for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) | |
1245 | INIT_HLIST_HEAD(&dp->ports[i]); | |
1246 | ||
d6569377 | 1247 | /* Set up our datapath device. */ |
df2c07f4 JP |
1248 | parms.name = nla_data(a[OVS_DP_ATTR_NAME]); |
1249 | parms.type = OVS_VPORT_TYPE_INTERNAL; | |
d6569377 BP |
1250 | parms.options = NULL; |
1251 | parms.dp = dp; | |
df2c07f4 | 1252 | parms.port_no = OVSP_LOCAL; |
28aea917 | 1253 | parms.upcall_portid = nla_get_u32(a[OVS_DP_ATTR_UPCALL_PID]); |
b063d9f0 | 1254 | |
d6569377 BP |
1255 | vport = new_vport(&parms); |
1256 | if (IS_ERR(vport)) { | |
1257 | err = PTR_ERR(vport); | |
1258 | if (err == -EBUSY) | |
1259 | err = -EEXIST; | |
1260 | ||
95b1d73a | 1261 | goto err_destroy_ports_array; |
d6569377 | 1262 | } |
d6569377 | 1263 | |
28aea917 | 1264 | reply = ovs_dp_cmd_build_info(dp, info->snd_portid, |
6455100f | 1265 | info->snd_seq, OVS_DP_CMD_NEW); |
aaff4b55 BP |
1266 | err = PTR_ERR(reply); |
1267 | if (IS_ERR(reply)) | |
1268 | goto err_destroy_local_port; | |
1269 | ||
2a4999f3 | 1270 | ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id); |
fb93e9aa | 1271 | list_add_tail_rcu(&dp->list_node, &ovs_net->dps); |
d6569377 | 1272 | |
cd2a59e9 | 1273 | ovs_unlock(); |
d6569377 | 1274 | |
e297c6b7 | 1275 | ovs_notify(reply, info, &ovs_dp_datapath_multicast_group); |
d6569377 BP |
1276 | return 0; |
1277 | ||
1278 | err_destroy_local_port: | |
cd2a59e9 | 1279 | ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL)); |
95b1d73a PS |
1280 | err_destroy_ports_array: |
1281 | kfree(dp->ports); | |
99769a40 JG |
1282 | err_destroy_percpu: |
1283 | free_percpu(dp->stats_percpu); | |
d6569377 | 1284 | err_destroy_table: |
a1c564be | 1285 | ovs_flow_tbl_destroy(ovsl_dereference(dp->table), false); |
d6569377 | 1286 | err_free_dp: |
0ceaa66c | 1287 | release_net(ovs_dp_get_net(dp)); |
d6569377 | 1288 | kfree(dp); |
cd2a59e9 PS |
1289 | err_unlock_ovs: |
1290 | ovs_unlock(); | |
d6569377 | 1291 | err: |
064af421 BP |
1292 | return err; |
1293 | } | |
1294 | ||
cd2a59e9 | 1295 | /* Called with ovs_mutex. */ |
2a4999f3 | 1296 | static void __dp_destroy(struct datapath *dp) |
44e05eca | 1297 | { |
95b1d73a | 1298 | int i; |
44e05eca | 1299 | |
95b1d73a PS |
1300 | for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) { |
1301 | struct vport *vport; | |
f8dfbcb7 | 1302 | struct hlist_node *n; |
95b1d73a | 1303 | |
f8dfbcb7 | 1304 | hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node) |
95b1d73a PS |
1305 | if (vport->port_no != OVSP_LOCAL) |
1306 | ovs_dp_detach_port(vport); | |
1307 | } | |
ed099e92 | 1308 | |
fb93e9aa | 1309 | list_del_rcu(&dp->list_node); |
ed099e92 | 1310 | |
cd2a59e9 PS |
1311 | /* OVSP_LOCAL is datapath internal port. We need to make sure that |
1312 | * all port in datapath are destroyed first before freeing datapath. | |
1313 | */ | |
1314 | ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL)); | |
99620d2c | 1315 | |
ed099e92 | 1316 | call_rcu(&dp->rcu, destroy_dp_rcu); |
2a4999f3 PS |
1317 | } |
1318 | ||
1319 | static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info) | |
1320 | { | |
1321 | struct sk_buff *reply; | |
1322 | struct datapath *dp; | |
1323 | int err; | |
1324 | ||
cd2a59e9 | 1325 | ovs_lock(); |
2a4999f3 PS |
1326 | dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs); |
1327 | err = PTR_ERR(dp); | |
1328 | if (IS_ERR(dp)) | |
cd2a59e9 | 1329 | goto unlock; |
2a4999f3 | 1330 | |
28aea917 | 1331 | reply = ovs_dp_cmd_build_info(dp, info->snd_portid, |
2a4999f3 PS |
1332 | info->snd_seq, OVS_DP_CMD_DEL); |
1333 | err = PTR_ERR(reply); | |
1334 | if (IS_ERR(reply)) | |
cd2a59e9 | 1335 | goto unlock; |
2a4999f3 PS |
1336 | |
1337 | __dp_destroy(dp); | |
cd2a59e9 | 1338 | ovs_unlock(); |
ed099e92 | 1339 | |
e297c6b7 | 1340 | ovs_notify(reply, info, &ovs_dp_datapath_multicast_group); |
99620d2c JG |
1341 | |
1342 | return 0; | |
cd2a59e9 PS |
1343 | unlock: |
1344 | ovs_unlock(); | |
1345 | return err; | |
44e05eca BP |
1346 | } |
1347 | ||
df2c07f4 | 1348 | static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info) |
064af421 | 1349 | { |
aaff4b55 | 1350 | struct sk_buff *reply; |
d6569377 | 1351 | struct datapath *dp; |
d6569377 | 1352 | int err; |
064af421 | 1353 | |
cd2a59e9 | 1354 | ovs_lock(); |
2a4999f3 | 1355 | dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs); |
cd2a59e9 | 1356 | err = PTR_ERR(dp); |
d6569377 | 1357 | if (IS_ERR(dp)) |
cd2a59e9 | 1358 | goto unlock; |
38c6ecbc | 1359 | |
28aea917 | 1360 | reply = ovs_dp_cmd_build_info(dp, info->snd_portid, |
6455100f | 1361 | info->snd_seq, OVS_DP_CMD_NEW); |
aaff4b55 BP |
1362 | if (IS_ERR(reply)) { |
1363 | err = PTR_ERR(reply); | |
b3dcb73c | 1364 | netlink_set_err(sock_net(skb->sk)->genl_sock, 0, |
850b6b3b | 1365 | ovs_dp_datapath_multicast_group.id, err); |
cd2a59e9 PS |
1366 | err = 0; |
1367 | goto unlock; | |
aaff4b55 BP |
1368 | } |
1369 | ||
cd2a59e9 | 1370 | ovs_unlock(); |
e297c6b7 | 1371 | ovs_notify(reply, info, &ovs_dp_datapath_multicast_group); |
850b6b3b | 1372 | |
aaff4b55 | 1373 | return 0; |
cd2a59e9 PS |
1374 | unlock: |
1375 | ovs_unlock(); | |
1376 | return err; | |
064af421 BP |
1377 | } |
1378 | ||
df2c07f4 | 1379 | static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info) |
1dcf111b | 1380 | { |
aaff4b55 | 1381 | struct sk_buff *reply; |
d6569377 | 1382 | struct datapath *dp; |
d6569377 | 1383 | int err; |
1dcf111b | 1384 | |
cd2a59e9 | 1385 | ovs_lock(); |
2a4999f3 | 1386 | dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs); |
cd2a59e9 PS |
1387 | if (IS_ERR(dp)) { |
1388 | err = PTR_ERR(dp); | |
1389 | goto unlock; | |
1390 | } | |
1dcf111b | 1391 | |
28aea917 | 1392 | reply = ovs_dp_cmd_build_info(dp, info->snd_portid, |
6455100f | 1393 | info->snd_seq, OVS_DP_CMD_NEW); |
cd2a59e9 PS |
1394 | if (IS_ERR(reply)) { |
1395 | err = PTR_ERR(reply); | |
1396 | goto unlock; | |
1397 | } | |
aaff4b55 | 1398 | |
cd2a59e9 | 1399 | ovs_unlock(); |
aaff4b55 | 1400 | return genlmsg_reply(reply, info); |
cd2a59e9 PS |
1401 | |
1402 | unlock: | |
1403 | ovs_unlock(); | |
1404 | return err; | |
1dcf111b JP |
1405 | } |
1406 | ||
df2c07f4 | 1407 | static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb) |
a7786963 | 1408 | { |
2a4999f3 | 1409 | struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id); |
254f2dc8 BP |
1410 | struct datapath *dp; |
1411 | int skip = cb->args[0]; | |
1412 | int i = 0; | |
a7786963 | 1413 | |
fb93e9aa PS |
1414 | rcu_read_lock(); |
1415 | list_for_each_entry_rcu(dp, &ovs_net->dps, list_node) { | |
a2bab2f0 | 1416 | if (i >= skip && |
28aea917 | 1417 | ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid, |
aaff4b55 | 1418 | cb->nlh->nlmsg_seq, NLM_F_MULTI, |
df2c07f4 | 1419 | OVS_DP_CMD_NEW) < 0) |
aaff4b55 | 1420 | break; |
254f2dc8 | 1421 | i++; |
a7786963 | 1422 | } |
fb93e9aa | 1423 | rcu_read_unlock(); |
aaff4b55 | 1424 | |
254f2dc8 BP |
1425 | cb->args[0] = i; |
1426 | ||
aaff4b55 | 1427 | return skb->len; |
c19e6535 BP |
1428 | } |
1429 | ||
aaff4b55 | 1430 | static struct genl_ops dp_datapath_genl_ops[] = { |
df2c07f4 | 1431 | { .cmd = OVS_DP_CMD_NEW, |
aaff4b55 BP |
1432 | .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ |
1433 | .policy = datapath_policy, | |
df2c07f4 | 1434 | .doit = ovs_dp_cmd_new |
aaff4b55 | 1435 | }, |
df2c07f4 | 1436 | { .cmd = OVS_DP_CMD_DEL, |
aaff4b55 BP |
1437 | .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ |
1438 | .policy = datapath_policy, | |
df2c07f4 | 1439 | .doit = ovs_dp_cmd_del |
aaff4b55 | 1440 | }, |
df2c07f4 | 1441 | { .cmd = OVS_DP_CMD_GET, |
aaff4b55 BP |
1442 | .flags = 0, /* OK for unprivileged users. */ |
1443 | .policy = datapath_policy, | |
df2c07f4 JP |
1444 | .doit = ovs_dp_cmd_get, |
1445 | .dumpit = ovs_dp_cmd_dump | |
aaff4b55 | 1446 | }, |
df2c07f4 | 1447 | { .cmd = OVS_DP_CMD_SET, |
aaff4b55 BP |
1448 | .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ |
1449 | .policy = datapath_policy, | |
df2c07f4 | 1450 | .doit = ovs_dp_cmd_set, |
aaff4b55 BP |
1451 | }, |
1452 | }; | |
1453 | ||
df2c07f4 | 1454 | static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = { |
df2c07f4 | 1455 | [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 }, |
f613a0d7 | 1456 | [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) }, |
d48c88ec JG |
1457 | [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 }, |
1458 | [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 }, | |
b063d9f0 | 1459 | [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 }, |
df2c07f4 | 1460 | [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED }, |
c19e6535 BP |
1461 | }; |
1462 | ||
f0fef760 BP |
1463 | static struct genl_family dp_vport_genl_family = { |
1464 | .id = GENL_ID_GENERATE, | |
df2c07f4 JP |
1465 | .hdrsize = sizeof(struct ovs_header), |
1466 | .name = OVS_VPORT_FAMILY, | |
69685a88 | 1467 | .version = OVS_VPORT_VERSION, |
2a4999f3 | 1468 | .maxattr = OVS_VPORT_ATTR_MAX, |
b3dcb73c | 1469 | .netnsok = true, |
14002a59 | 1470 | SET_PARALLEL_OPS |
f0fef760 BP |
1471 | }; |
1472 | ||
850b6b3b | 1473 | struct genl_multicast_group ovs_dp_vport_multicast_group = { |
df2c07f4 | 1474 | .name = OVS_VPORT_MCGROUP |
f0fef760 BP |
1475 | }; |
1476 | ||
cd2a59e9 | 1477 | /* Called with ovs_mutex or RCU read lock. */ |
df2c07f4 | 1478 | static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb, |
28aea917 | 1479 | u32 portid, u32 seq, u32 flags, u8 cmd) |
064af421 | 1480 | { |
df2c07f4 | 1481 | struct ovs_header *ovs_header; |
e926dfe3 | 1482 | struct ovs_vport_stats vport_stats; |
c19e6535 BP |
1483 | int err; |
1484 | ||
28aea917 | 1485 | ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family, |
f0fef760 | 1486 | flags, cmd); |
df2c07f4 | 1487 | if (!ovs_header) |
f0fef760 | 1488 | return -EMSGSIZE; |
c19e6535 | 1489 | |
99769a40 | 1490 | ovs_header->dp_ifindex = get_dpifindex(vport->dp); |
c19e6535 | 1491 | |
c3cc8c03 DM |
1492 | if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) || |
1493 | nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) || | |
1494 | nla_put_string(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport)) || | |
28aea917 | 1495 | nla_put_u32(skb, OVS_VPORT_ATTR_UPCALL_PID, vport->upcall_portid)) |
c3cc8c03 | 1496 | goto nla_put_failure; |
c19e6535 | 1497 | |
850b6b3b | 1498 | ovs_vport_get_stats(vport, &vport_stats); |
c3cc8c03 DM |
1499 | if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats), |
1500 | &vport_stats)) | |
1501 | goto nla_put_failure; | |
c19e6535 | 1502 | |
850b6b3b | 1503 | err = ovs_vport_get_options(vport, skb); |
f0fef760 BP |
1504 | if (err == -EMSGSIZE) |
1505 | goto error; | |
c19e6535 | 1506 | |
df2c07f4 | 1507 | return genlmsg_end(skb, ovs_header); |
c19e6535 BP |
1508 | |
1509 | nla_put_failure: | |
1510 | err = -EMSGSIZE; | |
f0fef760 | 1511 | error: |
df2c07f4 | 1512 | genlmsg_cancel(skb, ovs_header); |
f0fef760 | 1513 | return err; |
064af421 BP |
1514 | } |
1515 | ||
cd2a59e9 | 1516 | /* Called with ovs_mutex or RCU read lock. */ |
28aea917 | 1517 | struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid, |
f14d8083 | 1518 | u32 seq, u8 cmd) |
064af421 | 1519 | { |
c19e6535 | 1520 | struct sk_buff *skb; |
f0fef760 | 1521 | int retval; |
c19e6535 | 1522 | |
f0fef760 | 1523 | skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC); |
c19e6535 BP |
1524 | if (!skb) |
1525 | return ERR_PTR(-ENOMEM); | |
1526 | ||
28aea917 | 1527 | retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd); |
c25ea534 JG |
1528 | BUG_ON(retval < 0); |
1529 | ||
c19e6535 | 1530 | return skb; |
f0fef760 | 1531 | } |
c19e6535 | 1532 | |
cd2a59e9 | 1533 | /* Called with ovs_mutex or RCU read lock. */ |
2a4999f3 PS |
1534 | static struct vport *lookup_vport(struct net *net, |
1535 | struct ovs_header *ovs_header, | |
df2c07f4 | 1536 | struct nlattr *a[OVS_VPORT_ATTR_MAX + 1]) |
c19e6535 BP |
1537 | { |
1538 | struct datapath *dp; | |
1539 | struct vport *vport; | |
1540 | ||
df2c07f4 | 1541 | if (a[OVS_VPORT_ATTR_NAME]) { |
2a4999f3 | 1542 | vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME])); |
ed099e92 | 1543 | if (!vport) |
c19e6535 | 1544 | return ERR_PTR(-ENODEV); |
24ce832d BP |
1545 | if (ovs_header->dp_ifindex && |
1546 | ovs_header->dp_ifindex != get_dpifindex(vport->dp)) | |
1547 | return ERR_PTR(-ENODEV); | |
c19e6535 | 1548 | return vport; |
df2c07f4 JP |
1549 | } else if (a[OVS_VPORT_ATTR_PORT_NO]) { |
1550 | u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]); | |
c19e6535 BP |
1551 | |
1552 | if (port_no >= DP_MAX_PORTS) | |
f0fef760 | 1553 | return ERR_PTR(-EFBIG); |
c19e6535 | 1554 | |
2a4999f3 | 1555 | dp = get_dp(net, ovs_header->dp_ifindex); |
c19e6535 BP |
1556 | if (!dp) |
1557 | return ERR_PTR(-ENODEV); | |
f2459fe7 | 1558 | |
cd2a59e9 | 1559 | vport = ovs_vport_ovsl_rcu(dp, port_no); |
ed099e92 | 1560 | if (!vport) |
17535c57 | 1561 | return ERR_PTR(-ENODEV); |
c19e6535 BP |
1562 | return vport; |
1563 | } else | |
1564 | return ERR_PTR(-EINVAL); | |
064af421 BP |
1565 | } |
1566 | ||
df2c07f4 | 1567 | static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info) |
c19e6535 | 1568 | { |
f0fef760 | 1569 | struct nlattr **a = info->attrs; |
df2c07f4 | 1570 | struct ovs_header *ovs_header = info->userhdr; |
c19e6535 | 1571 | struct vport_parms parms; |
ed099e92 | 1572 | struct sk_buff *reply; |
c19e6535 | 1573 | struct vport *vport; |
c19e6535 | 1574 | struct datapath *dp; |
b0ec0f27 | 1575 | u32 port_no; |
c19e6535 | 1576 | int err; |
b0ec0f27 | 1577 | |
c19e6535 | 1578 | err = -EINVAL; |
ea36840f BP |
1579 | if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] || |
1580 | !a[OVS_VPORT_ATTR_UPCALL_PID]) | |
f0fef760 BP |
1581 | goto exit; |
1582 | ||
cd2a59e9 | 1583 | ovs_lock(); |
2a4999f3 | 1584 | dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex); |
c19e6535 BP |
1585 | err = -ENODEV; |
1586 | if (!dp) | |
ed099e92 | 1587 | goto exit_unlock; |
c19e6535 | 1588 | |
df2c07f4 JP |
1589 | if (a[OVS_VPORT_ATTR_PORT_NO]) { |
1590 | port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]); | |
c19e6535 BP |
1591 | |
1592 | err = -EFBIG; | |
1593 | if (port_no >= DP_MAX_PORTS) | |
ed099e92 | 1594 | goto exit_unlock; |
c19e6535 | 1595 | |
cd2a59e9 | 1596 | vport = ovs_vport_ovsl(dp, port_no); |
c19e6535 BP |
1597 | err = -EBUSY; |
1598 | if (vport) | |
ed099e92 | 1599 | goto exit_unlock; |
c19e6535 BP |
1600 | } else { |
1601 | for (port_no = 1; ; port_no++) { | |
1602 | if (port_no >= DP_MAX_PORTS) { | |
1603 | err = -EFBIG; | |
ed099e92 | 1604 | goto exit_unlock; |
c19e6535 | 1605 | } |
cd2a59e9 | 1606 | vport = ovs_vport_ovsl(dp, port_no); |
c19e6535 BP |
1607 | if (!vport) |
1608 | break; | |
51d4d598 | 1609 | } |
064af421 | 1610 | } |
b0ec0f27 | 1611 | |
df2c07f4 JP |
1612 | parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]); |
1613 | parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]); | |
1614 | parms.options = a[OVS_VPORT_ATTR_OPTIONS]; | |
c19e6535 BP |
1615 | parms.dp = dp; |
1616 | parms.port_no = port_no; | |
28aea917 | 1617 | parms.upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]); |
c19e6535 BP |
1618 | |
1619 | vport = new_vport(&parms); | |
1620 | err = PTR_ERR(vport); | |
1621 | if (IS_ERR(vport)) | |
ed099e92 | 1622 | goto exit_unlock; |
c19e6535 | 1623 | |
faef6d2d | 1624 | err = 0; |
1fc7083d JG |
1625 | if (a[OVS_VPORT_ATTR_STATS]) |
1626 | ovs_vport_set_stats(vport, nla_data(a[OVS_VPORT_ATTR_STATS])); | |
1627 | ||
1628 | reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq, | |
1629 | OVS_VPORT_CMD_NEW); | |
1630 | if (IS_ERR(reply)) { | |
1631 | err = PTR_ERR(reply); | |
850b6b3b | 1632 | ovs_dp_detach_port(vport); |
ed099e92 | 1633 | goto exit_unlock; |
c19e6535 | 1634 | } |
e297c6b7 TG |
1635 | |
1636 | ovs_notify(reply, info, &ovs_dp_vport_multicast_group); | |
c19e6535 | 1637 | |
ed099e92 | 1638 | exit_unlock: |
cd2a59e9 | 1639 | ovs_unlock(); |
c19e6535 BP |
1640 | exit: |
1641 | return err; | |
44e05eca BP |
1642 | } |
1643 | ||
df2c07f4 | 1644 | static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info) |
44e05eca | 1645 | { |
f0fef760 BP |
1646 | struct nlattr **a = info->attrs; |
1647 | struct sk_buff *reply; | |
c19e6535 | 1648 | struct vport *vport; |
c19e6535 | 1649 | int err; |
44e05eca | 1650 | |
cd2a59e9 | 1651 | ovs_lock(); |
2a4999f3 | 1652 | vport = lookup_vport(sock_net(skb->sk), info->userhdr, a); |
c19e6535 BP |
1653 | err = PTR_ERR(vport); |
1654 | if (IS_ERR(vport)) | |
f0fef760 | 1655 | goto exit_unlock; |
44e05eca | 1656 | |
6455100f | 1657 | if (a[OVS_VPORT_ATTR_TYPE] && |
17ec1d04 | 1658 | nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) { |
4879d4c7 | 1659 | err = -EINVAL; |
17ec1d04 JG |
1660 | goto exit_unlock; |
1661 | } | |
6455100f | 1662 | |
c25ea534 JG |
1663 | reply = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
1664 | if (!reply) { | |
1665 | err = -ENOMEM; | |
1666 | goto exit_unlock; | |
1667 | } | |
1668 | ||
17ec1d04 | 1669 | if (a[OVS_VPORT_ATTR_OPTIONS]) { |
850b6b3b | 1670 | err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]); |
17ec1d04 JG |
1671 | if (err) |
1672 | goto exit_free; | |
1673 | } | |
1fc7083d JG |
1674 | |
1675 | if (a[OVS_VPORT_ATTR_STATS]) | |
1676 | ovs_vport_set_stats(vport, nla_data(a[OVS_VPORT_ATTR_STATS])); | |
1677 | ||
1678 | if (a[OVS_VPORT_ATTR_UPCALL_PID]) | |
28aea917 | 1679 | vport->upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]); |
c19e6535 | 1680 | |
c25ea534 JG |
1681 | err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid, |
1682 | info->snd_seq, 0, OVS_VPORT_CMD_NEW); | |
1683 | BUG_ON(err < 0); | |
f0fef760 | 1684 | |
cd2a59e9 | 1685 | ovs_unlock(); |
8680ae4d | 1686 | ovs_notify(reply, info, &ovs_dp_vport_multicast_group); |
c25ea534 JG |
1687 | return 0; |
1688 | ||
1689 | exit_free: | |
1690 | kfree_skb(reply); | |
f0fef760 | 1691 | exit_unlock: |
cd2a59e9 | 1692 | ovs_unlock(); |
c19e6535 | 1693 | return err; |
064af421 BP |
1694 | } |
1695 | ||
df2c07f4 | 1696 | static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info) |
7c40efc9 | 1697 | { |
f0fef760 BP |
1698 | struct nlattr **a = info->attrs; |
1699 | struct sk_buff *reply; | |
c19e6535 | 1700 | struct vport *vport; |
c19e6535 BP |
1701 | int err; |
1702 | ||
cd2a59e9 | 1703 | ovs_lock(); |
2a4999f3 | 1704 | vport = lookup_vport(sock_net(skb->sk), info->userhdr, a); |
c19e6535 | 1705 | err = PTR_ERR(vport); |
f0fef760 BP |
1706 | if (IS_ERR(vport)) |
1707 | goto exit_unlock; | |
c19e6535 | 1708 | |
df2c07f4 | 1709 | if (vport->port_no == OVSP_LOCAL) { |
f0fef760 BP |
1710 | err = -EINVAL; |
1711 | goto exit_unlock; | |
1712 | } | |
1713 | ||
28aea917 IY |
1714 | reply = ovs_vport_cmd_build_info(vport, info->snd_portid, |
1715 | info->snd_seq, OVS_VPORT_CMD_DEL); | |
f0fef760 BP |
1716 | err = PTR_ERR(reply); |
1717 | if (IS_ERR(reply)) | |
1718 | goto exit_unlock; | |
1719 | ||
b57d5819 | 1720 | err = 0; |
850b6b3b | 1721 | ovs_dp_detach_port(vport); |
f0fef760 | 1722 | |
e297c6b7 | 1723 | ovs_notify(reply, info, &ovs_dp_vport_multicast_group); |
f0fef760 BP |
1724 | |
1725 | exit_unlock: | |
cd2a59e9 | 1726 | ovs_unlock(); |
c19e6535 | 1727 | return err; |
7c40efc9 BP |
1728 | } |
1729 | ||
df2c07f4 | 1730 | static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info) |
7c40efc9 | 1731 | { |
f0fef760 | 1732 | struct nlattr **a = info->attrs; |
df2c07f4 | 1733 | struct ovs_header *ovs_header = info->userhdr; |
ed099e92 | 1734 | struct sk_buff *reply; |
c19e6535 | 1735 | struct vport *vport; |
c19e6535 BP |
1736 | int err; |
1737 | ||
ed099e92 | 1738 | rcu_read_lock(); |
2a4999f3 | 1739 | vport = lookup_vport(sock_net(skb->sk), ovs_header, a); |
c19e6535 BP |
1740 | err = PTR_ERR(vport); |
1741 | if (IS_ERR(vport)) | |
f0fef760 | 1742 | goto exit_unlock; |
c19e6535 | 1743 | |
28aea917 IY |
1744 | reply = ovs_vport_cmd_build_info(vport, info->snd_portid, |
1745 | info->snd_seq, OVS_VPORT_CMD_NEW); | |
ed099e92 BP |
1746 | err = PTR_ERR(reply); |
1747 | if (IS_ERR(reply)) | |
f0fef760 | 1748 | goto exit_unlock; |
ed099e92 | 1749 | |
df2fa9b5 JG |
1750 | rcu_read_unlock(); |
1751 | ||
1752 | return genlmsg_reply(reply, info); | |
ed099e92 | 1753 | |
f0fef760 | 1754 | exit_unlock: |
ed099e92 | 1755 | rcu_read_unlock(); |
c19e6535 BP |
1756 | return err; |
1757 | } | |
1758 | ||
df2c07f4 | 1759 | static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb) |
c19e6535 | 1760 | { |
df2c07f4 | 1761 | struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh)); |
c19e6535 | 1762 | struct datapath *dp; |
95b1d73a PS |
1763 | int bucket = cb->args[0], skip = cb->args[1]; |
1764 | int i, j = 0; | |
c19e6535 | 1765 | |
2a4999f3 | 1766 | dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex); |
c19e6535 | 1767 | if (!dp) |
f0fef760 | 1768 | return -ENODEV; |
ed099e92 BP |
1769 | |
1770 | rcu_read_lock(); | |
95b1d73a | 1771 | for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) { |
ed099e92 | 1772 | struct vport *vport; |
95b1d73a PS |
1773 | |
1774 | j = 0; | |
f8dfbcb7 | 1775 | hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) { |
95b1d73a PS |
1776 | if (j >= skip && |
1777 | ovs_vport_cmd_fill_info(vport, skb, | |
28aea917 | 1778 | NETLINK_CB(cb->skb).portid, |
95b1d73a PS |
1779 | cb->nlh->nlmsg_seq, |
1780 | NLM_F_MULTI, | |
1781 | OVS_VPORT_CMD_NEW) < 0) | |
1782 | goto out; | |
1783 | ||
1784 | j++; | |
1785 | } | |
1786 | skip = 0; | |
c19e6535 | 1787 | } |
95b1d73a | 1788 | out: |
ed099e92 | 1789 | rcu_read_unlock(); |
c19e6535 | 1790 | |
95b1d73a PS |
1791 | cb->args[0] = i; |
1792 | cb->args[1] = j; | |
f0fef760 | 1793 | |
95b1d73a | 1794 | return skb->len; |
7c40efc9 BP |
1795 | } |
1796 | ||
f0fef760 | 1797 | static struct genl_ops dp_vport_genl_ops[] = { |
df2c07f4 | 1798 | { .cmd = OVS_VPORT_CMD_NEW, |
f0fef760 BP |
1799 | .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ |
1800 | .policy = vport_policy, | |
df2c07f4 | 1801 | .doit = ovs_vport_cmd_new |
f0fef760 | 1802 | }, |
df2c07f4 | 1803 | { .cmd = OVS_VPORT_CMD_DEL, |
f0fef760 BP |
1804 | .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ |
1805 | .policy = vport_policy, | |
df2c07f4 | 1806 | .doit = ovs_vport_cmd_del |
f0fef760 | 1807 | }, |
df2c07f4 | 1808 | { .cmd = OVS_VPORT_CMD_GET, |
f0fef760 BP |
1809 | .flags = 0, /* OK for unprivileged users. */ |
1810 | .policy = vport_policy, | |
df2c07f4 JP |
1811 | .doit = ovs_vport_cmd_get, |
1812 | .dumpit = ovs_vport_cmd_dump | |
f0fef760 | 1813 | }, |
df2c07f4 | 1814 | { .cmd = OVS_VPORT_CMD_SET, |
f0fef760 BP |
1815 | .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ |
1816 | .policy = vport_policy, | |
df2c07f4 | 1817 | .doit = ovs_vport_cmd_set, |
f0fef760 BP |
1818 | }, |
1819 | }; | |
1820 | ||
982b8810 BP |
1821 | struct genl_family_and_ops { |
1822 | struct genl_family *family; | |
1823 | struct genl_ops *ops; | |
1824 | int n_ops; | |
1825 | struct genl_multicast_group *group; | |
1826 | }; | |
ed099e92 | 1827 | |
982b8810 | 1828 | static const struct genl_family_and_ops dp_genl_families[] = { |
aaff4b55 BP |
1829 | { &dp_datapath_genl_family, |
1830 | dp_datapath_genl_ops, ARRAY_SIZE(dp_datapath_genl_ops), | |
850b6b3b | 1831 | &ovs_dp_datapath_multicast_group }, |
f0fef760 BP |
1832 | { &dp_vport_genl_family, |
1833 | dp_vport_genl_ops, ARRAY_SIZE(dp_vport_genl_ops), | |
850b6b3b | 1834 | &ovs_dp_vport_multicast_group }, |
37a1300c BP |
1835 | { &dp_flow_genl_family, |
1836 | dp_flow_genl_ops, ARRAY_SIZE(dp_flow_genl_ops), | |
850b6b3b | 1837 | &ovs_dp_flow_multicast_group }, |
982b8810 BP |
1838 | { &dp_packet_genl_family, |
1839 | dp_packet_genl_ops, ARRAY_SIZE(dp_packet_genl_ops), | |
1840 | NULL }, | |
1841 | }; | |
ed099e92 | 1842 | |
982b8810 BP |
1843 | static void dp_unregister_genl(int n_families) |
1844 | { | |
1845 | int i; | |
ed099e92 | 1846 | |
b867ca75 | 1847 | for (i = 0; i < n_families; i++) |
982b8810 | 1848 | genl_unregister_family(dp_genl_families[i].family); |
ed099e92 BP |
1849 | } |
1850 | ||
982b8810 | 1851 | static int dp_register_genl(void) |
064af421 | 1852 | { |
982b8810 BP |
1853 | int n_registered; |
1854 | int err; | |
1855 | int i; | |
064af421 | 1856 | |
982b8810 BP |
1857 | n_registered = 0; |
1858 | for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) { | |
1859 | const struct genl_family_and_ops *f = &dp_genl_families[i]; | |
064af421 | 1860 | |
982b8810 BP |
1861 | err = genl_register_family_with_ops(f->family, f->ops, |
1862 | f->n_ops); | |
1863 | if (err) | |
1864 | goto error; | |
1865 | n_registered++; | |
e22d4953 | 1866 | |
982b8810 BP |
1867 | if (f->group) { |
1868 | err = genl_register_mc_group(f->family, f->group); | |
1869 | if (err) | |
1870 | goto error; | |
1871 | } | |
1872 | } | |
9cc8b4e4 | 1873 | |
982b8810 | 1874 | return 0; |
064af421 BP |
1875 | |
1876 | error: | |
982b8810 BP |
1877 | dp_unregister_genl(n_registered); |
1878 | return err; | |
064af421 BP |
1879 | } |
1880 | ||
2a4999f3 PS |
1881 | static int __net_init ovs_init_net(struct net *net) |
1882 | { | |
1883 | struct ovs_net *ovs_net = net_generic(net, ovs_net_id); | |
1884 | ||
1885 | INIT_LIST_HEAD(&ovs_net->dps); | |
cd2a59e9 | 1886 | INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq); |
2a4999f3 PS |
1887 | return 0; |
1888 | } | |
1889 | ||
1890 | static void __net_exit ovs_exit_net(struct net *net) | |
1891 | { | |
cd2a59e9 | 1892 | struct datapath *dp, *dp_next; |
2a4999f3 PS |
1893 | struct ovs_net *ovs_net = net_generic(net, ovs_net_id); |
1894 | ||
cd2a59e9 PS |
1895 | ovs_lock(); |
1896 | list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node) | |
1897 | __dp_destroy(dp); | |
1898 | ovs_unlock(); | |
1899 | ||
1900 | cancel_work_sync(&ovs_net->dp_notify_work); | |
2a4999f3 PS |
1901 | } |
1902 | ||
1903 | static struct pernet_operations ovs_net_ops = { | |
1904 | .init = ovs_init_net, | |
1905 | .exit = ovs_exit_net, | |
1906 | .id = &ovs_net_id, | |
1907 | .size = sizeof(struct ovs_net), | |
1908 | }; | |
1909 | ||
637c8268 PS |
1910 | DEFINE_COMPAT_PNET_REG_FUNC(device); |
1911 | ||
22d24ebf BP |
1912 | static int __init dp_init(void) |
1913 | { | |
1914 | int err; | |
1915 | ||
f3d85db3 | 1916 | BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb)); |
22d24ebf | 1917 | |
dc5f3fef | 1918 | pr_info("Open vSwitch switching datapath %s, built "__DATE__" "__TIME__"\n", |
8a07709c | 1919 | VERSION); |
064af421 | 1920 | |
850b6b3b | 1921 | err = ovs_flow_init(); |
3544358a | 1922 | if (err) |
533e96e7 | 1923 | goto error; |
3544358a | 1924 | |
850b6b3b | 1925 | err = ovs_vport_init(); |
064af421 BP |
1926 | if (err) |
1927 | goto error_flow_exit; | |
1928 | ||
2a4999f3 | 1929 | err = register_pernet_device(&ovs_net_ops); |
f2459fe7 JG |
1930 | if (err) |
1931 | goto error_vport_exit; | |
1932 | ||
2a4999f3 PS |
1933 | err = register_netdevice_notifier(&ovs_dp_device_notifier); |
1934 | if (err) | |
1935 | goto error_netns_exit; | |
1936 | ||
982b8810 BP |
1937 | err = dp_register_genl(); |
1938 | if (err < 0) | |
37a1300c | 1939 | goto error_unreg_notifier; |
982b8810 | 1940 | |
064af421 BP |
1941 | return 0; |
1942 | ||
1943 | error_unreg_notifier: | |
850b6b3b | 1944 | unregister_netdevice_notifier(&ovs_dp_device_notifier); |
2a4999f3 PS |
1945 | error_netns_exit: |
1946 | unregister_pernet_device(&ovs_net_ops); | |
f2459fe7 | 1947 | error_vport_exit: |
850b6b3b | 1948 | ovs_vport_exit(); |
064af421 | 1949 | error_flow_exit: |
850b6b3b | 1950 | ovs_flow_exit(); |
064af421 BP |
1951 | error: |
1952 | return err; | |
1953 | } | |
1954 | ||
1955 | static void dp_cleanup(void) | |
1956 | { | |
982b8810 | 1957 | dp_unregister_genl(ARRAY_SIZE(dp_genl_families)); |
850b6b3b | 1958 | unregister_netdevice_notifier(&ovs_dp_device_notifier); |
2a4999f3 PS |
1959 | unregister_pernet_device(&ovs_net_ops); |
1960 | rcu_barrier(); | |
850b6b3b JG |
1961 | ovs_vport_exit(); |
1962 | ovs_flow_exit(); | |
064af421 BP |
1963 | } |
1964 | ||
1965 | module_init(dp_init); | |
1966 | module_exit(dp_cleanup); | |
1967 | ||
1968 | MODULE_DESCRIPTION("Open vSwitch switching datapath"); | |
1969 | MODULE_LICENSE("GPL"); | |
3d0666d2 | 1970 | MODULE_VERSION(VERSION); |