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