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