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