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