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