]> git.proxmox.com Git - mirror_ovs.git/blame - datapath/datapath.c
FAQ: Describe how to use ovs-ofctl with OpenFlow 1.1+.
[mirror_ovs.git] / datapath / datapath.c
CommitLineData
064af421 1/*
a1c564be 2 * Copyright (c) 2007-2013 Nicira, Inc.
a14bc59f 3 *
a9a29d22
JG
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of version 2 of the GNU General Public
6 * License as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 * 02110-1301, USA
064af421
BP
17 */
18
dfffaef1
JP
19#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20
064af421
BP
21#include <linux/init.h>
22#include <linux/module.h>
064af421 23#include <linux/if_arp.h>
064af421
BP
24#include <linux/if_vlan.h>
25#include <linux/in.h>
26#include <linux/ip.h>
982b8810 27#include <linux/jhash.h>
064af421
BP
28#include <linux/delay.h>
29#include <linux/time.h>
30#include <linux/etherdevice.h>
ed099e92 31#include <linux/genetlink.h>
064af421
BP
32#include <linux/kernel.h>
33#include <linux/kthread.h>
064af421
BP
34#include <linux/mutex.h>
35#include <linux/percpu.h>
36#include <linux/rcupdate.h>
37#include <linux/tcp.h>
38#include <linux/udp.h>
39#include <linux/version.h>
40#include <linux/ethtool.h>
064af421 41#include <linux/wait.h>
064af421 42#include <asm/div64.h>
656a0e37 43#include <linux/highmem.h>
064af421
BP
44#include <linux/netfilter_bridge.h>
45#include <linux/netfilter_ipv4.h>
46#include <linux/inetdevice.h>
47#include <linux/list.h>
077257b8 48#include <linux/openvswitch.h>
064af421 49#include <linux/rculist.h>
064af421 50#include <linux/dmi.h>
cd2a59e9
PS
51#include <linux/genetlink.h>
52#include <net/genetlink.h>
36956a7d 53#include <net/genetlink.h>
2a4999f3
PS
54#include <net/net_namespace.h>
55#include <net/netns/generic.h>
064af421 56
dd8d6b8c 57#include "checksum.h"
064af421 58#include "datapath.h"
064af421 59#include "flow.h"
303708cc 60#include "vlan.h"
3544358a 61#include "tunnel.h"
f2459fe7 62#include "vport-internal_dev.h"
d5de5b0d 63#include "vport-netdev.h"
064af421 64
4cf41591 65#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18) || \
64807dfb
JP
66 LINUX_VERSION_CODE >= KERNEL_VERSION(3,9,0)
67#error Kernels before 2.6.18 or after 3.8 are not supported by this version of Open vSwitch.
4cf41591
JG
68#endif
69
acd051f1
PS
70#define REHASH_FLOW_INTERVAL (10 * 60 * HZ)
71static void rehash_flow_table(struct work_struct *work);
72static DECLARE_DELAYED_WORK(rehash_flow_wq, rehash_flow_table);
73
2a4999f3
PS
74int ovs_net_id __read_mostly;
75
e297c6b7
TG
76static void ovs_notify(struct sk_buff *skb, struct genl_info *info,
77 struct genl_multicast_group *grp)
78{
79 genl_notify(skb, genl_info_net(info), info->snd_portid,
80 grp->id, info->nlhdr, GFP_KERNEL);
81}
82
ed099e92
BP
83/**
84 * DOC: Locking:
064af421 85 *
cd2a59e9
PS
86 * All writes e.g. Writes to device state (add/remove datapath, port, set
87 * operations on vports, etc.), Writes to other state (flow table
88 * modifications, set miscellaneous datapath parameters, etc.) are protected
89 * by ovs_lock.
ed099e92
BP
90 *
91 * Reads are protected by RCU.
92 *
93 * There are a few special cases (mostly stats) that have their own
94 * synchronization but they nest under all of above and don't interact with
95 * each other.
cd2a59e9
PS
96 *
97 * The RTNL lock nests inside ovs_mutex.
064af421 98 */
ed099e92 99
cd2a59e9
PS
100static DEFINE_MUTEX(ovs_mutex);
101
102void ovs_lock(void)
103{
104 mutex_lock(&ovs_mutex);
105}
106
107void ovs_unlock(void)
108{
109 mutex_unlock(&ovs_mutex);
110}
111
112#ifdef CONFIG_LOCKDEP
113int lockdep_ovsl_is_held(void)
114{
115 if (debug_locks)
116 return lockdep_is_held(&ovs_mutex);
117 else
118 return 1;
119}
120#endif
121
c19e6535 122static struct vport *new_vport(const struct vport_parms *);
2a4999f3 123static int queue_gso_packets(struct net *, int dp_ifindex, struct sk_buff *,
7257b535 124 const struct dp_upcall_info *);
2a4999f3
PS
125static int queue_userspace_packet(struct net *, int dp_ifindex,
126 struct sk_buff *,
7257b535 127 const struct dp_upcall_info *);
064af421 128
cd2a59e9 129/* Must be called with rcu_read_lock or ovs_mutex. */
2a4999f3 130static struct datapath *get_dp(struct net *net, int dp_ifindex)
064af421 131{
254f2dc8
BP
132 struct datapath *dp = NULL;
133 struct net_device *dev;
ed099e92 134
254f2dc8 135 rcu_read_lock();
2a4999f3 136 dev = dev_get_by_index_rcu(net, dp_ifindex);
254f2dc8 137 if (dev) {
850b6b3b 138 struct vport *vport = ovs_internal_dev_get_vport(dev);
254f2dc8
BP
139 if (vport)
140 dp = vport->dp;
141 }
142 rcu_read_unlock();
143
144 return dp;
064af421 145}
064af421 146
cd2a59e9 147/* Must be called with rcu_read_lock or ovs_mutex. */
850b6b3b 148const char *ovs_dp_name(const struct datapath *dp)
f2459fe7 149{
cd2a59e9 150 struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL);
16b82e84 151 return vport->ops->get_name(vport);
f2459fe7
JG
152}
153
99769a40
JG
154static int get_dpifindex(struct datapath *dp)
155{
156 struct vport *local;
157 int ifindex;
158
159 rcu_read_lock();
160
95b1d73a 161 local = ovs_vport_rcu(dp, OVSP_LOCAL);
99769a40 162 if (local)
d5de5b0d 163 ifindex = netdev_vport_priv(local)->dev->ifindex;
99769a40
JG
164 else
165 ifindex = 0;
166
167 rcu_read_unlock();
168
169 return ifindex;
170}
171
46c6a11d
JG
172static void destroy_dp_rcu(struct rcu_head *rcu)
173{
174 struct datapath *dp = container_of(rcu, struct datapath, rcu);
46c6a11d 175
a1c564be 176 ovs_flow_tbl_destroy((__force struct flow_table *)dp->table, false);
46c6a11d 177 free_percpu(dp->stats_percpu);
2a4999f3 178 release_net(ovs_dp_get_net(dp));
95b1d73a 179 kfree(dp->ports);
5ca1ba48 180 kfree(dp);
46c6a11d
JG
181}
182
95b1d73a
PS
183static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
184 u16 port_no)
185{
186 return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
187}
188
189struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
190{
191 struct vport *vport;
95b1d73a
PS
192 struct hlist_head *head;
193
194 head = vport_hash_bucket(dp, port_no);
f8dfbcb7 195 hlist_for_each_entry_rcu(vport, head, dp_hash_node) {
95b1d73a
PS
196 if (vport->port_no == port_no)
197 return vport;
198 }
199 return NULL;
200}
201
cd2a59e9 202/* Called with ovs_mutex. */
c19e6535 203static struct vport *new_vport(const struct vport_parms *parms)
064af421 204{
f2459fe7 205 struct vport *vport;
f2459fe7 206
850b6b3b 207 vport = ovs_vport_add(parms);
c19e6535
BP
208 if (!IS_ERR(vport)) {
209 struct datapath *dp = parms->dp;
95b1d73a 210 struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
064af421 211
95b1d73a 212 hlist_add_head_rcu(&vport->dp_hash_node, head);
c19e6535 213 }
c19e6535 214 return vport;
064af421
BP
215}
216
850b6b3b 217void ovs_dp_detach_port(struct vport *p)
064af421 218{
cd2a59e9 219 ASSERT_OVSL();
064af421 220
064af421 221 /* First drop references to device. */
95b1d73a 222 hlist_del_rcu(&p->dp_hash_node);
f2459fe7 223
7237e4f4 224 /* Then destroy it. */
850b6b3b 225 ovs_vport_del(p);
064af421
BP
226}
227
8819fac7 228/* Must be called with rcu_read_lock. */
850b6b3b 229void ovs_dp_process_received_packet(struct vport *p, struct sk_buff *skb)
064af421
BP
230{
231 struct datapath *dp = p->dp;
3544358a 232 struct sw_flow *flow;
064af421 233 struct dp_stats_percpu *stats;
52a23d92 234 struct sw_flow_key key;
e9141eec 235 u64 *stats_counter;
4c1ad233 236 int error;
064af421 237
70dbc259 238 stats = this_cpu_ptr(dp->stats_percpu);
a063b0df 239
52a23d92 240 /* Extract flow from 'skb' into 'key'. */
a1c564be 241 error = ovs_flow_extract(skb, p->port_no, &key);
52a23d92
JG
242 if (unlikely(error)) {
243 kfree_skb(skb);
244 return;
55574bb0
BP
245 }
246
52a23d92 247 /* Look up flow. */
a1c564be 248 flow = ovs_flow_lookup(rcu_dereference(dp->table), &key);
52a23d92
JG
249 if (unlikely(!flow)) {
250 struct dp_upcall_info upcall;
251
252 upcall.cmd = OVS_PACKET_CMD_MISS;
253 upcall.key = &key;
254 upcall.userdata = NULL;
255 upcall.portid = p->upcall_portid;
256 ovs_dp_upcall(dp, skb, &upcall);
257 consume_skb(skb);
258 stats_counter = &stats->n_missed;
259 goto out;
260 }
261
262 OVS_CB(skb)->flow = flow;
263
e9141eec 264 stats_counter = &stats->n_hit;
850b6b3b
JG
265 ovs_flow_used(OVS_CB(skb)->flow, skb);
266 ovs_execute_actions(dp, skb);
55574bb0 267
8819fac7 268out:
55574bb0 269 /* Update datapath statistics. */
821cb9fa 270 u64_stats_update_begin(&stats->sync);
e9141eec 271 (*stats_counter)++;
821cb9fa 272 u64_stats_update_end(&stats->sync);
064af421
BP
273}
274
aa5a8fdc
JG
275static struct genl_family dp_packet_genl_family = {
276 .id = GENL_ID_GENERATE,
df2c07f4
JP
277 .hdrsize = sizeof(struct ovs_header),
278 .name = OVS_PACKET_FAMILY,
69685a88 279 .version = OVS_PACKET_VERSION,
2a4999f3
PS
280 .maxattr = OVS_PACKET_ATTR_MAX,
281 SET_NETNSOK
aa5a8fdc
JG
282};
283
850b6b3b
JG
284int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
285 const struct dp_upcall_info *upcall_info)
aa5a8fdc
JG
286{
287 struct dp_stats_percpu *stats;
7257b535 288 int dp_ifindex;
aa5a8fdc
JG
289 int err;
290
28aea917 291 if (upcall_info->portid == 0) {
b063d9f0 292 err = -ENOTCONN;
b063d9f0
JG
293 goto err;
294 }
295
7257b535
BP
296 dp_ifindex = get_dpifindex(dp);
297 if (!dp_ifindex) {
298 err = -ENODEV;
299 goto err;
aa5a8fdc
JG
300 }
301
7257b535 302 forward_ip_summed(skb, true);
36ce148c 303
7257b535 304 if (!skb_is_gso(skb))
2a4999f3 305 err = queue_userspace_packet(ovs_dp_get_net(dp), dp_ifindex, skb, upcall_info);
7257b535 306 else
2a4999f3 307 err = queue_gso_packets(ovs_dp_get_net(dp), dp_ifindex, skb, upcall_info);
d76195db
JG
308 if (err)
309 goto err;
310
311 return 0;
aa5a8fdc 312
aa5a8fdc 313err:
70dbc259 314 stats = this_cpu_ptr(dp->stats_percpu);
aa5a8fdc 315
821cb9fa 316 u64_stats_update_begin(&stats->sync);
aa5a8fdc 317 stats->n_lost++;
821cb9fa 318 u64_stats_update_end(&stats->sync);
aa5a8fdc 319
aa5a8fdc 320 return err;
982b8810
BP
321}
322
2a4999f3
PS
323static int queue_gso_packets(struct net *net, int dp_ifindex,
324 struct sk_buff *skb,
7257b535 325 const struct dp_upcall_info *upcall_info)
cb5087ca 326{
d4cba1f8 327 unsigned short gso_type = skb_shinfo(skb)->gso_type;
7257b535
BP
328 struct dp_upcall_info later_info;
329 struct sw_flow_key later_key;
330 struct sk_buff *segs, *nskb;
331 int err;
cb5087ca 332
0aa52d88 333 segs = __skb_gso_segment(skb, NETIF_F_SG | NETIF_F_HW_CSUM, false);
79089764
PS
334 if (IS_ERR(segs))
335 return PTR_ERR(segs);
99769a40 336
7257b535
BP
337 /* Queue all of the segments. */
338 skb = segs;
cb5087ca 339 do {
2a4999f3 340 err = queue_userspace_packet(net, dp_ifindex, skb, upcall_info);
982b8810 341 if (err)
7257b535 342 break;
856081f6 343
d4cba1f8 344 if (skb == segs && gso_type & SKB_GSO_UDP) {
e1cf87ff
JG
345 /* The initial flow key extracted by ovs_flow_extract()
346 * in this case is for a first fragment, so we need to
7257b535
BP
347 * properly mark later fragments.
348 */
349 later_key = *upcall_info->key;
9e44d715 350 later_key.ip.frag = OVS_FRAG_TYPE_LATER;
7257b535
BP
351
352 later_info = *upcall_info;
353 later_info.key = &later_key;
354 upcall_info = &later_info;
355 }
36ce148c 356 } while ((skb = skb->next));
cb5087ca 357
7257b535
BP
358 /* Free all of the segments. */
359 skb = segs;
360 do {
361 nskb = skb->next;
362 if (err)
363 kfree_skb(skb);
364 else
365 consume_skb(skb);
366 } while ((skb = nskb));
367 return err;
368}
369
0afa2373
TG
370static size_t key_attr_size(void)
371{
372 return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */
373 + nla_total_size(0) /* OVS_KEY_ATTR_TUNNEL */
374 + nla_total_size(8) /* OVS_TUNNEL_KEY_ATTR_ID */
375 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_SRC */
376 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_DST */
377 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TOS */
378 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TTL */
379 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT */
380 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_CSUM */
381 + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */
382 + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
383 + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
384 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
385 + nla_total_size(4) /* OVS_KEY_ATTR_8021Q */
386 + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
387 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
388 + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
389 + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
390 + nla_total_size(28); /* OVS_KEY_ATTR_ND */
391}
392
393static size_t upcall_msg_size(const struct sk_buff *skb,
394 const struct nlattr *userdata)
395{
396 size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
397 + nla_total_size(skb->len) /* OVS_PACKET_ATTR_PACKET */
398 + nla_total_size(key_attr_size()); /* OVS_PACKET_ATTR_KEY */
399
400 /* OVS_PACKET_ATTR_USERDATA */
401 if (userdata)
402 size += NLA_ALIGN(userdata->nla_len);
403
404 return size;
405}
406
2a4999f3
PS
407static int queue_userspace_packet(struct net *net, int dp_ifindex,
408 struct sk_buff *skb,
7257b535
BP
409 const struct dp_upcall_info *upcall_info)
410{
411 struct ovs_header *upcall;
6161d3fd 412 struct sk_buff *nskb = NULL;
7257b535
BP
413 struct sk_buff *user_skb; /* to be queued to userspace */
414 struct nlattr *nla;
7257b535
BP
415 int err;
416
6161d3fd
JG
417 if (vlan_tx_tag_present(skb)) {
418 nskb = skb_clone(skb, GFP_ATOMIC);
419 if (!nskb)
420 return -ENOMEM;
421
422 err = vlan_deaccel_tag(nskb);
423 if (err)
424 return err;
7257b535 425
6161d3fd
JG
426 skb = nskb;
427 }
428
429 if (nla_attr_size(skb->len) > USHRT_MAX) {
430 err = -EFBIG;
431 goto out;
432 }
7257b535 433
0afa2373 434 user_skb = genlmsg_new(upcall_msg_size(skb, upcall_info->userdata), GFP_ATOMIC);
6161d3fd
JG
435 if (!user_skb) {
436 err = -ENOMEM;
437 goto out;
438 }
7257b535
BP
439
440 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
441 0, upcall_info->cmd);
442 upcall->dp_ifindex = dp_ifindex;
443
444 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
a1c564be 445 ovs_flow_to_nlattrs(upcall_info->key, upcall_info->key, user_skb);
7257b535
BP
446 nla_nest_end(user_skb, nla);
447
448 if (upcall_info->userdata)
e995e3df 449 __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
462a988b 450 nla_len(upcall_info->userdata),
e995e3df 451 nla_data(upcall_info->userdata));
7257b535
BP
452
453 nla = __nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, skb->len);
bed53bd1
PS
454
455 skb_copy_and_csum_dev(skb, nla_data(nla));
7257b535 456
c39b1a5c 457 genlmsg_end(user_skb, upcall);
28aea917 458 err = genlmsg_unicast(net, user_skb, upcall_info->portid);
6161d3fd
JG
459
460out:
461 kfree_skb(nskb);
462 return err;
cb5087ca
BP
463}
464
cd2a59e9 465/* Called with ovs_mutex. */
2a4999f3 466static int flush_flows(struct datapath *dp)
064af421 467{
3544358a
PS
468 struct flow_table *old_table;
469 struct flow_table *new_table;
8d5ebd83 470
cd2a59e9 471 old_table = ovsl_dereference(dp->table);
850b6b3b 472 new_table = ovs_flow_tbl_alloc(TBL_MIN_BUCKETS);
8d5ebd83 473 if (!new_table)
ed099e92 474 return -ENOMEM;
8d5ebd83
JG
475
476 rcu_assign_pointer(dp->table, new_table);
477
a1c564be 478 ovs_flow_tbl_destroy(old_table, true);
ed099e92 479 return 0;
064af421
BP
480}
481
9b405f1a
PS
482static struct nlattr *reserve_sfa_size(struct sw_flow_actions **sfa, int attr_len)
483{
484
485 struct sw_flow_actions *acts;
486 int new_acts_size;
487 int req_size = NLA_ALIGN(attr_len);
488 int next_offset = offsetof(struct sw_flow_actions, actions) +
489 (*sfa)->actions_len;
490
ba400435 491 if (req_size <= (ksize(*sfa) - next_offset))
9b405f1a
PS
492 goto out;
493
ba400435 494 new_acts_size = ksize(*sfa) * 2;
9b405f1a
PS
495
496 if (new_acts_size > MAX_ACTIONS_BUFSIZE) {
497 if ((MAX_ACTIONS_BUFSIZE - next_offset) < req_size)
498 return ERR_PTR(-EMSGSIZE);
499 new_acts_size = MAX_ACTIONS_BUFSIZE;
500 }
501
502 acts = ovs_flow_actions_alloc(new_acts_size);
503 if (IS_ERR(acts))
504 return (void *)acts;
505
506 memcpy(acts->actions, (*sfa)->actions, (*sfa)->actions_len);
507 acts->actions_len = (*sfa)->actions_len;
ba400435 508 kfree(*sfa);
9b405f1a
PS
509 *sfa = acts;
510
511out:
512 (*sfa)->actions_len += req_size;
513 return (struct nlattr *) ((unsigned char *)(*sfa) + next_offset);
514}
515
516static int add_action(struct sw_flow_actions **sfa, int attrtype, void *data, int len)
517{
518 struct nlattr *a;
519
520 a = reserve_sfa_size(sfa, nla_attr_size(len));
521 if (IS_ERR(a))
522 return PTR_ERR(a);
523
524 a->nla_type = attrtype;
525 a->nla_len = nla_attr_size(len);
526
527 if (data)
528 memcpy(nla_data(a), data, len);
529 memset((unsigned char *) a + a->nla_len, 0, nla_padlen(len));
6ff686f2 530
9b405f1a
PS
531 return 0;
532}
533
534static inline int add_nested_action_start(struct sw_flow_actions **sfa, int attrtype)
535{
536 int used = (*sfa)->actions_len;
537 int err;
538
539 err = add_action(sfa, attrtype, NULL, 0);
540 if (err)
541 return err;
542
543 return used;
544}
545
546static inline void add_nested_action_end(struct sw_flow_actions *sfa, int st_offset)
547{
548 struct nlattr *a = (struct nlattr *) ((unsigned char *)sfa->actions + st_offset);
549
550 a->nla_len = sfa->actions_len - st_offset;
551}
552
553static int validate_and_copy_actions(const struct nlattr *attr,
554 const struct sw_flow_key *key, int depth,
555 struct sw_flow_actions **sfa);
556
557static int validate_and_copy_sample(const struct nlattr *attr,
558 const struct sw_flow_key *key, int depth,
559 struct sw_flow_actions **sfa)
6ff686f2 560{
4be00e48
BP
561 const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
562 const struct nlattr *probability, *actions;
563 const struct nlattr *a;
9b405f1a 564 int rem, start, err, st_acts;
4be00e48
BP
565
566 memset(attrs, 0, sizeof(attrs));
6455100f 567 nla_for_each_nested(a, attr, rem) {
4be00e48
BP
568 int type = nla_type(a);
569 if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
570 return -EINVAL;
571 attrs[type] = a;
572 }
573 if (rem)
6ff686f2 574 return -EINVAL;
4be00e48
BP
575
576 probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
577 if (!probability || nla_len(probability) != sizeof(u32))
6ff686f2
PS
578 return -EINVAL;
579
4be00e48
BP
580 actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
581 if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
582 return -EINVAL;
9b405f1a
PS
583
584 /* validation done, copy sample action. */
585 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SAMPLE);
586 if (start < 0)
587 return start;
588 err = add_action(sfa, OVS_SAMPLE_ATTR_PROBABILITY, nla_data(probability), sizeof(u32));
589 if (err)
590 return err;
591 st_acts = add_nested_action_start(sfa, OVS_SAMPLE_ATTR_ACTIONS);
592 if (st_acts < 0)
593 return st_acts;
594
595 err = validate_and_copy_actions(actions, key, depth + 1, sfa);
596 if (err)
597 return err;
598
599 add_nested_action_end(*sfa, st_acts);
600 add_nested_action_end(*sfa, start);
601
602 return 0;
4edb9ae9
PS
603}
604
b1323f59
PS
605static int validate_tp_port(const struct sw_flow_key *flow_key)
606{
607 if (flow_key->eth.type == htons(ETH_P_IP)) {
6e9bea4d 608 if (flow_key->ipv4.tp.src || flow_key->ipv4.tp.dst)
b1323f59
PS
609 return 0;
610 } else if (flow_key->eth.type == htons(ETH_P_IPV6)) {
6e9bea4d 611 if (flow_key->ipv6.tp.src || flow_key->ipv6.tp.dst)
b1323f59
PS
612 return 0;
613 }
614
615 return -EINVAL;
616}
617
9b405f1a
PS
618static int validate_and_copy_set_tun(const struct nlattr *attr,
619 struct sw_flow_actions **sfa)
620{
a1c564be
AZ
621 struct sw_flow_match match;
622 struct sw_flow_key key;
9b405f1a
PS
623 int err, start;
624
a1c564be
AZ
625 ovs_match_init(&match, &key, NULL);
626 err = ipv4_tun_from_nlattr(nla_data(attr), &match, false);
9b405f1a
PS
627 if (err)
628 return err;
629
630 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SET);
631 if (start < 0)
632 return start;
633
a1c564be
AZ
634 err = add_action(sfa, OVS_KEY_ATTR_IPV4_TUNNEL, &match.key->tun_key,
635 sizeof(match.key->tun_key));
9b405f1a
PS
636 add_nested_action_end(*sfa, start);
637
638 return err;
639}
640
fea393b1 641static int validate_set(const struct nlattr *a,
9b405f1a
PS
642 const struct sw_flow_key *flow_key,
643 struct sw_flow_actions **sfa,
644 bool *set_tun)
4edb9ae9 645{
4edb9ae9
PS
646 const struct nlattr *ovs_key = nla_data(a);
647 int key_type = nla_type(ovs_key);
648
649 /* There can be only one key in a action */
650 if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
651 return -EINVAL;
652
653 if (key_type > OVS_KEY_ATTR_MAX ||
9b405f1a
PS
654 (ovs_key_lens[key_type] != nla_len(ovs_key) &&
655 ovs_key_lens[key_type] != -1))
4edb9ae9
PS
656 return -EINVAL;
657
fea393b1 658 switch (key_type) {
4edb9ae9 659 const struct ovs_key_ipv4 *ipv4_key;
bc7a5acd 660 const struct ovs_key_ipv6 *ipv6_key;
9b405f1a 661 int err;
4edb9ae9 662
fea393b1 663 case OVS_KEY_ATTR_PRIORITY:
fea393b1 664 case OVS_KEY_ATTR_ETHERNET:
4edb9ae9
PS
665 break;
666
72e8bf28
AA
667 case OVS_KEY_ATTR_SKB_MARK:
668#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20) && !defined(CONFIG_NETFILTER)
669 if (nla_get_u32(ovs_key) != 0)
670 return -EINVAL;
671#endif
672 break;
673
9b405f1a
PS
674 case OVS_KEY_ATTR_TUNNEL:
675 *set_tun = true;
676 err = validate_and_copy_set_tun(a, sfa);
677 if (err)
678 return err;
356af50b
KM
679 break;
680
fea393b1 681 case OVS_KEY_ATTR_IPV4:
4edb9ae9
PS
682 if (flow_key->eth.type != htons(ETH_P_IP))
683 return -EINVAL;
684
6e9bea4d 685 if (!flow_key->ip.proto)
4edb9ae9
PS
686 return -EINVAL;
687
688 ipv4_key = nla_data(ovs_key);
689 if (ipv4_key->ipv4_proto != flow_key->ip.proto)
690 return -EINVAL;
691
9e44d715 692 if (ipv4_key->ipv4_frag != flow_key->ip.frag)
7257b535
BP
693 return -EINVAL;
694
4edb9ae9
PS
695 break;
696
bc7a5acd
AA
697 case OVS_KEY_ATTR_IPV6:
698 if (flow_key->eth.type != htons(ETH_P_IPV6))
699 return -EINVAL;
700
701 if (!flow_key->ip.proto)
702 return -EINVAL;
703
704 ipv6_key = nla_data(ovs_key);
705 if (ipv6_key->ipv6_proto != flow_key->ip.proto)
706 return -EINVAL;
707
708 if (ipv6_key->ipv6_frag != flow_key->ip.frag)
709 return -EINVAL;
710
711 if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
712 return -EINVAL;
713
714 break;
715
fea393b1 716 case OVS_KEY_ATTR_TCP:
4edb9ae9
PS
717 if (flow_key->ip.proto != IPPROTO_TCP)
718 return -EINVAL;
719
b1323f59 720 return validate_tp_port(flow_key);
4edb9ae9 721
fea393b1 722 case OVS_KEY_ATTR_UDP:
4edb9ae9
PS
723 if (flow_key->ip.proto != IPPROTO_UDP)
724 return -EINVAL;
725
b1323f59 726 return validate_tp_port(flow_key);
4edb9ae9
PS
727
728 default:
729 return -EINVAL;
730 }
fea393b1 731
4edb9ae9 732 return 0;
6ff686f2
PS
733}
734
98403001
BP
735static int validate_userspace(const struct nlattr *attr)
736{
6455100f 737 static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
98403001 738 [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
e995e3df 739 [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_UNSPEC },
98403001
BP
740 };
741 struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
742 int error;
743
6455100f
PS
744 error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
745 attr, userspace_policy);
98403001
BP
746 if (error)
747 return error;
748
6455100f
PS
749 if (!a[OVS_USERSPACE_ATTR_PID] ||
750 !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
98403001
BP
751 return -EINVAL;
752
753 return 0;
754}
755
9b405f1a
PS
756static int copy_action(const struct nlattr *from,
757 struct sw_flow_actions **sfa)
758{
759 int totlen = NLA_ALIGN(from->nla_len);
760 struct nlattr *to;
761
762 to = reserve_sfa_size(sfa, from->nla_len);
763 if (IS_ERR(to))
764 return PTR_ERR(to);
765
766 memcpy(to, from, totlen);
767 return 0;
768}
769
770static int validate_and_copy_actions(const struct nlattr *attr,
771 const struct sw_flow_key *key,
772 int depth,
773 struct sw_flow_actions **sfa)
064af421 774{
23cad98c 775 const struct nlattr *a;
6ff686f2
PS
776 int rem, err;
777
778 if (depth >= SAMPLE_ACTION_DEPTH)
779 return -EOVERFLOW;
23cad98c 780
37a1300c 781 nla_for_each_nested(a, attr, rem) {
98403001 782 /* Expected argument lengths, (u32)-1 for variable length. */
df2c07f4 783 static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
fea393b1 784 [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
98403001 785 [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
fea393b1
BP
786 [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
787 [OVS_ACTION_ATTR_POP_VLAN] = 0,
4edb9ae9 788 [OVS_ACTION_ATTR_SET] = (u32)-1,
98403001 789 [OVS_ACTION_ATTR_SAMPLE] = (u32)-1
23cad98c 790 };
fea393b1 791 const struct ovs_action_push_vlan *vlan;
23cad98c 792 int type = nla_type(a);
9b405f1a 793 bool skip_copy;
23cad98c 794
6ff686f2 795 if (type > OVS_ACTION_ATTR_MAX ||
98403001
BP
796 (action_lens[type] != nla_len(a) &&
797 action_lens[type] != (u32)-1))
23cad98c
BP
798 return -EINVAL;
799
9b405f1a 800 skip_copy = false;
23cad98c 801 switch (type) {
df2c07f4 802 case OVS_ACTION_ATTR_UNSPEC:
cdee00fd 803 return -EINVAL;
064af421 804
98403001
BP
805 case OVS_ACTION_ATTR_USERSPACE:
806 err = validate_userspace(a);
807 if (err)
808 return err;
809 break;
810
df2c07f4 811 case OVS_ACTION_ATTR_OUTPUT:
23cad98c
BP
812 if (nla_get_u32(a) >= DP_MAX_PORTS)
813 return -EINVAL;
3b1fc5f3 814 break;
cdee00fd 815
4edb9ae9 816
fea393b1
BP
817 case OVS_ACTION_ATTR_POP_VLAN:
818 break;
819
820 case OVS_ACTION_ATTR_PUSH_VLAN:
821 vlan = nla_data(a);
822 if (vlan->vlan_tpid != htons(ETH_P_8021Q))
823 return -EINVAL;
8ddc056d 824 if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
064af421 825 return -EINVAL;
23cad98c 826 break;
064af421 827
4edb9ae9 828 case OVS_ACTION_ATTR_SET:
9b405f1a 829 err = validate_set(a, key, sfa, &skip_copy);
4edb9ae9
PS
830 if (err)
831 return err;
23cad98c 832 break;
064af421 833
6ff686f2 834 case OVS_ACTION_ATTR_SAMPLE:
9b405f1a 835 err = validate_and_copy_sample(a, key, depth, sfa);
6ff686f2
PS
836 if (err)
837 return err;
9b405f1a 838 skip_copy = true;
6ff686f2
PS
839 break;
840
23cad98c 841 default:
4edb9ae9 842 return -EINVAL;
23cad98c 843 }
9b405f1a
PS
844 if (!skip_copy) {
845 err = copy_action(a, sfa);
846 if (err)
847 return err;
848 }
23cad98c 849 }
3c5f6de3 850
23cad98c
BP
851 if (rem > 0)
852 return -EINVAL;
064af421 853
23cad98c 854 return 0;
064af421 855}
4edb9ae9 856
064af421
BP
857static void clear_stats(struct sw_flow *flow)
858{
6bfafa55 859 flow->used = 0;
064af421 860 flow->tcp_flags = 0;
064af421
BP
861 flow->packet_count = 0;
862 flow->byte_count = 0;
863}
864
df2c07f4 865static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
064af421 866{
df2c07f4 867 struct ovs_header *ovs_header = info->userhdr;
982b8810 868 struct nlattr **a = info->attrs;
e0e57990 869 struct sw_flow_actions *acts;
982b8810 870 struct sk_buff *packet;
e0e57990 871 struct sw_flow *flow;
f7cd0081 872 struct datapath *dp;
d6569377 873 struct ethhdr *eth;
3f19d399 874 int len;
d6569377 875 int err;
064af421 876
f7cd0081 877 err = -EINVAL;
df2c07f4 878 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
7c3072cc 879 !a[OVS_PACKET_ATTR_ACTIONS])
e5cad958 880 goto err;
064af421 881
df2c07f4 882 len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
3f19d399 883 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
f7cd0081
BP
884 err = -ENOMEM;
885 if (!packet)
e5cad958 886 goto err;
3f19d399
BP
887 skb_reserve(packet, NET_IP_ALIGN);
888
bf3d6fce 889 nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
8d5ebd83 890
f7cd0081
BP
891 skb_reset_mac_header(packet);
892 eth = eth_hdr(packet);
064af421 893
d6569377
BP
894 /* Normally, setting the skb 'protocol' field would be handled by a
895 * call to eth_type_trans(), but it assumes there's a sending
896 * device, which we may not have. */
7cd46155 897 if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN)
f7cd0081 898 packet->protocol = eth->h_proto;
d6569377 899 else
f7cd0081 900 packet->protocol = htons(ETH_P_802_2);
d3c54451 901
e0e57990 902 /* Build an sw_flow for sending this packet. */
850b6b3b 903 flow = ovs_flow_alloc();
e0e57990
BP
904 err = PTR_ERR(flow);
905 if (IS_ERR(flow))
e5cad958 906 goto err_kfree_skb;
064af421 907
a1c564be 908 err = ovs_flow_extract(packet, -1, &flow->key);
e0e57990 909 if (err)
9321954a 910 goto err_flow_free;
e0e57990 911
a1c564be 912 err = ovs_flow_metadata_from_nlattrs(flow, a[OVS_PACKET_ATTR_KEY]);
80e5eed9 913 if (err)
9321954a 914 goto err_flow_free;
9b405f1a 915 acts = ovs_flow_actions_alloc(nla_len(a[OVS_PACKET_ATTR_ACTIONS]));
e0e57990
BP
916 err = PTR_ERR(acts);
917 if (IS_ERR(acts))
9321954a 918 goto err_flow_free;
9b405f1a
PS
919
920 err = validate_and_copy_actions(a[OVS_PACKET_ATTR_ACTIONS], &flow->key, 0, &acts);
e0e57990 921 rcu_assign_pointer(flow->sf_acts, acts);
9b405f1a
PS
922 if (err)
923 goto err_flow_free;
e0e57990
BP
924
925 OVS_CB(packet)->flow = flow;
abff858b 926 packet->priority = flow->key.phy.priority;
72e8bf28 927 skb_set_mark(packet, flow->key.phy.skb_mark);
e0e57990 928
d6569377 929 rcu_read_lock();
2a4999f3 930 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
f7cd0081 931 err = -ENODEV;
e5cad958
BP
932 if (!dp)
933 goto err_unlock;
cc4015df 934
e9141eec 935 local_bh_disable();
850b6b3b 936 err = ovs_execute_actions(dp, packet);
e9141eec 937 local_bh_enable();
d6569377 938 rcu_read_unlock();
e0e57990 939
a1c564be 940 ovs_flow_free(flow, false);
e5cad958 941 return err;
064af421 942
e5cad958
BP
943err_unlock:
944 rcu_read_unlock();
9321954a 945err_flow_free:
a1c564be 946 ovs_flow_free(flow, false);
e5cad958
BP
947err_kfree_skb:
948 kfree_skb(packet);
949err:
d6569377 950 return err;
064af421
BP
951}
952
df2c07f4 953static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
7c3072cc
TG
954#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18)
955 [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
956#else
957 [OVS_PACKET_ATTR_PACKET] = { .minlen = ETH_HLEN },
958#endif
df2c07f4
JP
959 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
960 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
982b8810
BP
961};
962
963static struct genl_ops dp_packet_genl_ops[] = {
df2c07f4 964 { .cmd = OVS_PACKET_CMD_EXECUTE,
982b8810
BP
965 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
966 .policy = packet_policy,
df2c07f4 967 .doit = ovs_packet_cmd_execute
982b8810
BP
968 }
969};
970
df2c07f4 971static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats)
064af421 972{
d6569377 973 int i;
cd2a59e9 974 struct flow_table *table = ovsl_dereference(dp->table);
f180c2e2 975
850b6b3b 976 stats->n_flows = ovs_flow_tbl_count(table);
064af421 977
7257b535 978 stats->n_hit = stats->n_missed = stats->n_lost = 0;
d6569377
BP
979 for_each_possible_cpu(i) {
980 const struct dp_stats_percpu *percpu_stats;
981 struct dp_stats_percpu local_stats;
821cb9fa 982 unsigned int start;
44e05eca 983
d6569377 984 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
064af421 985
d6569377 986 do {
821cb9fa 987 start = u64_stats_fetch_begin_bh(&percpu_stats->sync);
d6569377 988 local_stats = *percpu_stats;
821cb9fa 989 } while (u64_stats_fetch_retry_bh(&percpu_stats->sync, start));
064af421 990
d6569377
BP
991 stats->n_hit += local_stats.n_hit;
992 stats->n_missed += local_stats.n_missed;
993 stats->n_lost += local_stats.n_lost;
994 }
995}
064af421 996
df2c07f4
JP
997static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
998 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
999 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
1000 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
d6569377 1001};
36956a7d 1002
37a1300c
BP
1003static struct genl_family dp_flow_genl_family = {
1004 .id = GENL_ID_GENERATE,
df2c07f4
JP
1005 .hdrsize = sizeof(struct ovs_header),
1006 .name = OVS_FLOW_FAMILY,
69685a88 1007 .version = OVS_FLOW_VERSION,
2a4999f3
PS
1008 .maxattr = OVS_FLOW_ATTR_MAX,
1009 SET_NETNSOK
37a1300c 1010};
ed099e92 1011
850b6b3b 1012static struct genl_multicast_group ovs_dp_flow_multicast_group = {
df2c07f4 1013 .name = OVS_FLOW_MCGROUP
37a1300c
BP
1014};
1015
9b405f1a
PS
1016static int actions_to_attr(const struct nlattr *attr, int len, struct sk_buff *skb);
1017static int sample_action_to_attr(const struct nlattr *attr, struct sk_buff *skb)
1018{
1019 const struct nlattr *a;
1020 struct nlattr *start;
1021 int err = 0, rem;
1022
1023 start = nla_nest_start(skb, OVS_ACTION_ATTR_SAMPLE);
1024 if (!start)
1025 return -EMSGSIZE;
1026
1027 nla_for_each_nested(a, attr, rem) {
1028 int type = nla_type(a);
1029 struct nlattr *st_sample;
1030
1031 switch (type) {
1032 case OVS_SAMPLE_ATTR_PROBABILITY:
1033 if (nla_put(skb, OVS_SAMPLE_ATTR_PROBABILITY, sizeof(u32), nla_data(a)))
1034 return -EMSGSIZE;
1035 break;
1036 case OVS_SAMPLE_ATTR_ACTIONS:
1037 st_sample = nla_nest_start(skb, OVS_SAMPLE_ATTR_ACTIONS);
1038 if (!st_sample)
1039 return -EMSGSIZE;
1040 err = actions_to_attr(nla_data(a), nla_len(a), skb);
1041 if (err)
1042 return err;
1043 nla_nest_end(skb, st_sample);
1044 break;
1045 }
1046 }
1047
1048 nla_nest_end(skb, start);
1049 return err;
1050}
1051
1052static int set_action_to_attr(const struct nlattr *a, struct sk_buff *skb)
1053{
1054 const struct nlattr *ovs_key = nla_data(a);
1055 int key_type = nla_type(ovs_key);
1056 struct nlattr *start;
1057 int err;
1058
1059 switch (key_type) {
1060 case OVS_KEY_ATTR_IPV4_TUNNEL:
1061 start = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
1062 if (!start)
1063 return -EMSGSIZE;
1064
a1c564be
AZ
1065 err = ipv4_tun_to_nlattr(skb,
1066 nla_data(ovs_key), nla_data(ovs_key));
9b405f1a
PS
1067 if (err)
1068 return err;
1069 nla_nest_end(skb, start);
1070 break;
1071 default:
1072 if (nla_put(skb, OVS_ACTION_ATTR_SET, nla_len(a), ovs_key))
1073 return -EMSGSIZE;
1074 break;
1075 }
1076
1077 return 0;
1078}
1079
1080static int actions_to_attr(const struct nlattr *attr, int len, struct sk_buff *skb)
1081{
1082 const struct nlattr *a;
1083 int rem, err;
1084
1085 nla_for_each_attr(a, attr, len, rem) {
1086 int type = nla_type(a);
1087
1088 switch (type) {
1089 case OVS_ACTION_ATTR_SET:
1090 err = set_action_to_attr(a, skb);
1091 if (err)
1092 return err;
1093 break;
1094
1095 case OVS_ACTION_ATTR_SAMPLE:
1096 err = sample_action_to_attr(a, skb);
1097 if (err)
1098 return err;
1099 break;
1100 default:
1101 if (nla_put(skb, type, nla_len(a), nla_data(a)))
1102 return -EMSGSIZE;
1103 break;
1104 }
1105 }
1106
1107 return 0;
1108}
1109
0afa2373
TG
1110static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts)
1111{
1112 return NLMSG_ALIGN(sizeof(struct ovs_header))
1113 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_KEY */
a1c564be 1114 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_MASK */
0afa2373
TG
1115 + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
1116 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
1117 + nla_total_size(8) /* OVS_FLOW_ATTR_USED */
1118 + nla_total_size(acts->actions_len); /* OVS_FLOW_ATTR_ACTIONS */
1119}
1120
cd2a59e9 1121/* Called with ovs_mutex. */
df2c07f4 1122static int ovs_flow_cmd_fill_info(struct sw_flow *flow, struct datapath *dp,
28aea917 1123 struct sk_buff *skb, u32 portid,
6455100f 1124 u32 seq, u32 flags, u8 cmd)
d6569377 1125{
37a1300c 1126 const int skb_orig_len = skb->len;
d6569377 1127 const struct sw_flow_actions *sf_acts;
9b405f1a 1128 struct nlattr *start;
df2c07f4
JP
1129 struct ovs_flow_stats stats;
1130 struct ovs_header *ovs_header;
d6569377
BP
1131 struct nlattr *nla;
1132 unsigned long used;
1133 u8 tcp_flags;
1134 int err;
064af421 1135
cd2a59e9 1136 sf_acts = ovsl_dereference(flow->sf_acts);
064af421 1137
28aea917 1138 ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd);
df2c07f4 1139 if (!ovs_header)
37a1300c 1140 return -EMSGSIZE;
d6569377 1141
99769a40 1142 ovs_header->dp_ifindex = get_dpifindex(dp);
d6569377 1143
a1c564be 1144 /* Fill flow key. */
df2c07f4 1145 nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
d6569377
BP
1146 if (!nla)
1147 goto nla_put_failure;
a1c564be
AZ
1148
1149 err = ovs_flow_to_nlattrs(&flow->unmasked_key,
1150 &flow->unmasked_key, skb);
d6569377 1151 if (err)
37a1300c 1152 goto error;
d6569377
BP
1153 nla_nest_end(skb, nla);
1154
a1c564be
AZ
1155 nla = nla_nest_start(skb, OVS_FLOW_ATTR_MASK);
1156 if (!nla)
1157 goto nla_put_failure;
1158
1159 err = ovs_flow_to_nlattrs(&flow->key,
1160 &ovsl_dereference(flow->mask)->key, skb);
1161 if (err)
1162 goto error;
1163
1164 nla_nest_end(skb, nla);
1165
d6569377
BP
1166 spin_lock_bh(&flow->lock);
1167 used = flow->used;
1168 stats.n_packets = flow->packet_count;
1169 stats.n_bytes = flow->byte_count;
1170 tcp_flags = flow->tcp_flags;
1171 spin_unlock_bh(&flow->lock);
1172
c3cc8c03
DM
1173 if (used &&
1174 nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
1175 goto nla_put_failure;
d6569377 1176
c3cc8c03
DM
1177 if (stats.n_packets &&
1178 nla_put(skb, OVS_FLOW_ATTR_STATS,
1179 sizeof(struct ovs_flow_stats), &stats))
1180 goto nla_put_failure;
d6569377 1181
c3cc8c03
DM
1182 if (tcp_flags &&
1183 nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, tcp_flags))
1184 goto nla_put_failure;
d6569377 1185
df2c07f4 1186 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
30053024
BP
1187 * this is the first flow to be dumped into 'skb'. This is unusual for
1188 * Netlink but individual action lists can be longer than
1189 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
1190 * The userspace caller can always fetch the actions separately if it
1191 * really wants them. (Most userspace callers in fact don't care.)
1192 *
1193 * This can only fail for dump operations because the skb is always
1194 * properly sized for single flows.
1195 */
9b405f1a 1196 start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
f6f481ee
PS
1197 if (start) {
1198 err = actions_to_attr(sf_acts->actions, sf_acts->actions_len, skb);
0a25b039
BP
1199 if (!err)
1200 nla_nest_end(skb, start);
1201 else {
1202 if (skb_orig_len)
1203 goto error;
1204
1205 nla_nest_cancel(skb, start);
1206 }
7aac03bd
JG
1207 } else if (skb_orig_len)
1208 goto nla_put_failure;
37a1300c 1209
df2c07f4 1210 return genlmsg_end(skb, ovs_header);
d6569377
BP
1211
1212nla_put_failure:
1213 err = -EMSGSIZE;
37a1300c 1214error:
df2c07f4 1215 genlmsg_cancel(skb, ovs_header);
d6569377 1216 return err;
44e05eca
BP
1217}
1218
df2c07f4 1219static struct sk_buff *ovs_flow_cmd_alloc_info(struct sw_flow *flow)
44e05eca 1220{
37a1300c 1221 const struct sw_flow_actions *sf_acts;
d6569377 1222
cd2a59e9 1223 sf_acts = ovsl_dereference(flow->sf_acts);
d6569377 1224
0afa2373 1225 return genlmsg_new(ovs_flow_cmd_msg_size(sf_acts), GFP_KERNEL);
37a1300c 1226}
8d5ebd83 1227
6455100f
PS
1228static struct sk_buff *ovs_flow_cmd_build_info(struct sw_flow *flow,
1229 struct datapath *dp,
28aea917 1230 u32 portid, u32 seq, u8 cmd)
37a1300c
BP
1231{
1232 struct sk_buff *skb;
1233 int retval;
d6569377 1234
df2c07f4 1235 skb = ovs_flow_cmd_alloc_info(flow);
37a1300c
BP
1236 if (!skb)
1237 return ERR_PTR(-ENOMEM);
d6569377 1238
28aea917 1239 retval = ovs_flow_cmd_fill_info(flow, dp, skb, portid, seq, 0, cmd);
37a1300c 1240 BUG_ON(retval < 0);
d6569377 1241 return skb;
064af421
BP
1242}
1243
df2c07f4 1244static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info)
064af421 1245{
37a1300c 1246 struct nlattr **a = info->attrs;
df2c07f4 1247 struct ovs_header *ovs_header = info->userhdr;
37a1300c 1248 struct sw_flow_key key;
a1c564be
AZ
1249 struct sw_flow *flow = NULL;
1250 struct sw_flow_mask mask;
37a1300c 1251 struct sk_buff *reply;
9c52546b 1252 struct datapath *dp;
3544358a 1253 struct flow_table *table;
9b405f1a 1254 struct sw_flow_actions *acts = NULL;
a1c564be 1255 struct sw_flow_match match;
bc4a05c6 1256 int error;
064af421 1257
37a1300c
BP
1258 /* Extract key. */
1259 error = -EINVAL;
df2c07f4 1260 if (!a[OVS_FLOW_ATTR_KEY])
37a1300c 1261 goto error;
a1c564be
AZ
1262
1263 ovs_match_init(&match, &key, &mask);
1264 error = ovs_match_from_nlattrs(&match,
1265 a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
37a1300c
BP
1266 if (error)
1267 goto error;
064af421 1268
37a1300c 1269 /* Validate actions. */
df2c07f4 1270 if (a[OVS_FLOW_ATTR_ACTIONS]) {
9b405f1a
PS
1271 acts = ovs_flow_actions_alloc(nla_len(a[OVS_FLOW_ATTR_ACTIONS]));
1272 error = PTR_ERR(acts);
1273 if (IS_ERR(acts))
37a1300c 1274 goto error;
9b405f1a
PS
1275
1276 error = validate_and_copy_actions(a[OVS_FLOW_ATTR_ACTIONS], &key, 0, &acts);
1277 if (error)
1278 goto err_kfree;
df2c07f4 1279 } else if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW) {
37a1300c
BP
1280 error = -EINVAL;
1281 goto error;
1282 }
1283
cd2a59e9 1284 ovs_lock();
2a4999f3 1285 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
d6569377 1286 error = -ENODEV;
9c52546b 1287 if (!dp)
cd2a59e9 1288 goto err_unlock_ovs;
704a1e09 1289
cd2a59e9 1290 table = ovsl_dereference(dp->table);
a1c564be
AZ
1291
1292 /* Check if this is a duplicate flow */
1293 flow = ovs_flow_lookup(table, &key);
3544358a 1294 if (!flow) {
a1c564be 1295 struct sw_flow_mask *mask_p;
d6569377
BP
1296 /* Bail out if we're not allowed to create a new flow. */
1297 error = -ENOENT;
df2c07f4 1298 if (info->genlhdr->cmd == OVS_FLOW_CMD_SET)
cd2a59e9 1299 goto err_unlock_ovs;
d6569377
BP
1300
1301 /* Expand table, if necessary, to make room. */
850b6b3b 1302 if (ovs_flow_tbl_need_to_expand(table)) {
3544358a
PS
1303 struct flow_table *new_table;
1304
850b6b3b 1305 new_table = ovs_flow_tbl_expand(table);
3544358a
PS
1306 if (!IS_ERR(new_table)) {
1307 rcu_assign_pointer(dp->table, new_table);
a1c564be 1308 ovs_flow_tbl_destroy(table, true);
cd2a59e9 1309 table = ovsl_dereference(dp->table);
3544358a 1310 }
d6569377
BP
1311 }
1312
1313 /* Allocate flow. */
850b6b3b 1314 flow = ovs_flow_alloc();
d6569377
BP
1315 if (IS_ERR(flow)) {
1316 error = PTR_ERR(flow);
cd2a59e9 1317 goto err_unlock_ovs;
d6569377 1318 }
d6569377
BP
1319 clear_stats(flow);
1320
a1c564be
AZ
1321 /* Make sure mask is unique in the system */
1322 mask_p = ovs_sw_flow_mask_find(table, &mask);
1323 if (!mask_p) {
1324 /* Allocate a new mask if none exsits. */
1325 mask_p = ovs_sw_flow_mask_alloc();
1326 if (!mask_p)
1327 goto err_flow_free;
1328 mask_p->key = mask.key;
1329 mask_p->range = mask.range;
1330 ovs_sw_flow_mask_insert(table, mask_p);
1331 }
1332
1333 ovs_sw_flow_mask_add_ref(mask_p);
1334 rcu_assign_pointer(flow->mask, mask_p);
d6569377
BP
1335 rcu_assign_pointer(flow->sf_acts, acts);
1336
d6569377 1337 /* Put flow in bucket. */
a1c564be 1338 ovs_flow_insert(table, flow, &key, match.range.end);
37a1300c 1339
28aea917 1340 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
a1c564be 1341 info->snd_seq, OVS_FLOW_CMD_NEW);
d6569377
BP
1342 } else {
1343 /* We found a matching flow. */
1344 struct sw_flow_actions *old_acts;
1345
a1c564be
AZ
1346 /* Make sure the it has the same unmasked key. */
1347 if (!ovs_flow_cmp_unmasked_key(flow, &key, match.range.end)) {
1348 error = -EINVAL;
1349 goto err_unlock_ovs;
1350 }
1351
d6569377
BP
1352 /* Bail out if we're not allowed to modify an existing flow.
1353 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
1354 * because Generic Netlink treats the latter as a dump
1355 * request. We also accept NLM_F_EXCL in case that bug ever
1356 * gets fixed.
1357 */
1358 error = -EEXIST;
df2c07f4 1359 if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW &&
37a1300c 1360 info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL))
cd2a59e9 1361 goto err_unlock_ovs;
d6569377
BP
1362
1363 /* Update actions. */
cd2a59e9 1364 old_acts = ovsl_dereference(flow->sf_acts);
9b405f1a
PS
1365 rcu_assign_pointer(flow->sf_acts, acts);
1366 ovs_flow_deferred_free_acts(old_acts);
d6569377 1367
28aea917 1368 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
6455100f 1369 info->snd_seq, OVS_FLOW_CMD_NEW);
d6569377
BP
1370
1371 /* Clear stats. */
df2c07f4 1372 if (a[OVS_FLOW_ATTR_CLEAR]) {
d6569377
BP
1373 spin_lock_bh(&flow->lock);
1374 clear_stats(flow);
1375 spin_unlock_bh(&flow->lock);
1376 }
9c52546b 1377 }
cd2a59e9 1378 ovs_unlock();
37a1300c
BP
1379
1380 if (!IS_ERR(reply))
e297c6b7 1381 ovs_notify(reply, info, &ovs_dp_flow_multicast_group);
37a1300c 1382 else
2a4999f3
PS
1383 netlink_set_err(GENL_SOCK(sock_net(skb->sk)), 0,
1384 ovs_dp_flow_multicast_group.id, PTR_ERR(reply));
d6569377 1385 return 0;
704a1e09 1386
a1c564be
AZ
1387err_flow_free:
1388 ovs_flow_free(flow, false);
cd2a59e9
PS
1389err_unlock_ovs:
1390 ovs_unlock();
9b405f1a 1391err_kfree:
ba400435 1392 kfree(acts);
37a1300c 1393error:
9c52546b 1394 return error;
704a1e09
BP
1395}
1396
df2c07f4 1397static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
704a1e09 1398{
37a1300c 1399 struct nlattr **a = info->attrs;
df2c07f4 1400 struct ovs_header *ovs_header = info->userhdr;
37a1300c 1401 struct sw_flow_key key;
37a1300c 1402 struct sk_buff *reply;
704a1e09 1403 struct sw_flow *flow;
9c52546b 1404 struct datapath *dp;
3544358a 1405 struct flow_table *table;
a1c564be 1406 struct sw_flow_match match;
9c52546b 1407 int err;
704a1e09 1408
df2c07f4 1409 if (!a[OVS_FLOW_ATTR_KEY])
37a1300c 1410 return -EINVAL;
a1c564be
AZ
1411
1412 ovs_match_init(&match, &key, NULL);
1413 err = ovs_match_from_nlattrs(&match, a[OVS_FLOW_ATTR_KEY], NULL);
37a1300c
BP
1414 if (err)
1415 return err;
704a1e09 1416
cd2a59e9 1417 ovs_lock();
2a4999f3 1418 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
cd2a59e9
PS
1419 if (!dp) {
1420 err = -ENODEV;
1421 goto unlock;
1422 }
704a1e09 1423
cd2a59e9 1424 table = ovsl_dereference(dp->table);
a1c564be 1425 flow = ovs_flow_lookup_unmasked_key(table, &match);
cd2a59e9
PS
1426 if (!flow) {
1427 err = -ENOENT;
1428 goto unlock;
1429 }
d6569377 1430
28aea917 1431 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
6455100f 1432 info->snd_seq, OVS_FLOW_CMD_NEW);
cd2a59e9
PS
1433 if (IS_ERR(reply)) {
1434 err = PTR_ERR(reply);
1435 goto unlock;
1436 }
36956a7d 1437
cd2a59e9 1438 ovs_unlock();
37a1300c 1439 return genlmsg_reply(reply, info);
cd2a59e9
PS
1440unlock:
1441 ovs_unlock();
1442 return err;
d6569377 1443}
9c52546b 1444
df2c07f4 1445static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
d6569377 1446{
37a1300c 1447 struct nlattr **a = info->attrs;
df2c07f4 1448 struct ovs_header *ovs_header = info->userhdr;
37a1300c 1449 struct sw_flow_key key;
37a1300c 1450 struct sk_buff *reply;
d6569377 1451 struct sw_flow *flow;
d6569377 1452 struct datapath *dp;
3544358a 1453 struct flow_table *table;
a1c564be 1454 struct sw_flow_match match;
d6569377 1455 int err;
36956a7d 1456
cd2a59e9 1457 ovs_lock();
2a4999f3 1458 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
cd2a59e9
PS
1459 if (!dp) {
1460 err = -ENODEV;
1461 goto unlock;
1462 }
2a4999f3 1463
cd2a59e9
PS
1464 if (!a[OVS_FLOW_ATTR_KEY]) {
1465 err = flush_flows(dp);
1466 goto unlock;
1467 }
a1c564be
AZ
1468
1469 ovs_match_init(&match, &key, NULL);
1470 err = ovs_match_from_nlattrs(&match, a[OVS_FLOW_ATTR_KEY], NULL);
37a1300c 1471 if (err)
cd2a59e9 1472 goto unlock;
d6569377 1473
cd2a59e9 1474 table = ovsl_dereference(dp->table);
a1c564be 1475 flow = ovs_flow_lookup_unmasked_key(table, &match);
cd2a59e9
PS
1476 if (!flow) {
1477 err = -ENOENT;
1478 goto unlock;
1479 }
d6569377 1480
df2c07f4 1481 reply = ovs_flow_cmd_alloc_info(flow);
cd2a59e9
PS
1482 if (!reply) {
1483 err = -ENOMEM;
1484 goto unlock;
1485 }
37a1300c 1486
a1c564be 1487 ovs_flow_remove(table, flow);
37a1300c 1488
28aea917 1489 err = ovs_flow_cmd_fill_info(flow, dp, reply, info->snd_portid,
df2c07f4 1490 info->snd_seq, 0, OVS_FLOW_CMD_DEL);
37a1300c
BP
1491 BUG_ON(err < 0);
1492
a1c564be 1493 ovs_flow_free(flow, true);
cd2a59e9 1494 ovs_unlock();
37a1300c 1495
e297c6b7 1496 ovs_notify(reply, info, &ovs_dp_flow_multicast_group);
37a1300c 1497 return 0;
cd2a59e9
PS
1498unlock:
1499 ovs_unlock();
1500 return err;
37a1300c
BP
1501}
1502
df2c07f4 1503static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
37a1300c 1504{
df2c07f4 1505 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
37a1300c 1506 struct datapath *dp;
20d035b2 1507 struct flow_table *table;
37a1300c 1508
cd2a59e9 1509 ovs_lock();
2a4999f3 1510 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
cd2a59e9
PS
1511 if (!dp) {
1512 ovs_unlock();
37a1300c 1513 return -ENODEV;
cd2a59e9 1514 }
37a1300c 1515
cd2a59e9 1516 table = ovsl_dereference(dp->table);
20d035b2 1517
37a1300c 1518 for (;;) {
37a1300c
BP
1519 struct sw_flow *flow;
1520 u32 bucket, obj;
1521
1522 bucket = cb->args[0];
1523 obj = cb->args[1];
a1c564be 1524 flow = ovs_flow_dump_next(table, &bucket, &obj);
3544358a 1525 if (!flow)
37a1300c
BP
1526 break;
1527
6455100f 1528 if (ovs_flow_cmd_fill_info(flow, dp, skb,
28aea917 1529 NETLINK_CB(cb->skb).portid,
37a1300c 1530 cb->nlh->nlmsg_seq, NLM_F_MULTI,
df2c07f4 1531 OVS_FLOW_CMD_NEW) < 0)
37a1300c
BP
1532 break;
1533
1534 cb->args[0] = bucket;
1535 cb->args[1] = obj;
1536 }
cd2a59e9 1537 ovs_unlock();
37a1300c 1538 return skb->len;
704a1e09
BP
1539}
1540
37a1300c 1541static struct genl_ops dp_flow_genl_ops[] = {
df2c07f4 1542 { .cmd = OVS_FLOW_CMD_NEW,
37a1300c
BP
1543 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1544 .policy = flow_policy,
df2c07f4 1545 .doit = ovs_flow_cmd_new_or_set
37a1300c 1546 },
df2c07f4 1547 { .cmd = OVS_FLOW_CMD_DEL,
37a1300c
BP
1548 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1549 .policy = flow_policy,
df2c07f4 1550 .doit = ovs_flow_cmd_del
37a1300c 1551 },
df2c07f4 1552 { .cmd = OVS_FLOW_CMD_GET,
37a1300c
BP
1553 .flags = 0, /* OK for unprivileged users. */
1554 .policy = flow_policy,
df2c07f4
JP
1555 .doit = ovs_flow_cmd_get,
1556 .dumpit = ovs_flow_cmd_dump
37a1300c 1557 },
df2c07f4 1558 { .cmd = OVS_FLOW_CMD_SET,
37a1300c
BP
1559 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1560 .policy = flow_policy,
df2c07f4 1561 .doit = ovs_flow_cmd_new_or_set,
37a1300c
BP
1562 },
1563};
1564
df2c07f4 1565static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
aaff4b55 1566#ifdef HAVE_NLA_NUL_STRING
df2c07f4 1567 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
aaff4b55 1568#endif
b063d9f0 1569 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
d6569377
BP
1570};
1571
aaff4b55
BP
1572static struct genl_family dp_datapath_genl_family = {
1573 .id = GENL_ID_GENERATE,
df2c07f4
JP
1574 .hdrsize = sizeof(struct ovs_header),
1575 .name = OVS_DATAPATH_FAMILY,
69685a88 1576 .version = OVS_DATAPATH_VERSION,
2a4999f3
PS
1577 .maxattr = OVS_DP_ATTR_MAX,
1578 SET_NETNSOK
aaff4b55
BP
1579};
1580
850b6b3b 1581static struct genl_multicast_group ovs_dp_datapath_multicast_group = {
df2c07f4 1582 .name = OVS_DATAPATH_MCGROUP
aaff4b55
BP
1583};
1584
0afa2373
TG
1585static size_t ovs_dp_cmd_msg_size(void)
1586{
1587 size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
1588
1589 msgsize += nla_total_size(IFNAMSIZ);
1590 msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
1591
1592 return msgsize;
1593}
1594
df2c07f4 1595static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
28aea917 1596 u32 portid, u32 seq, u32 flags, u8 cmd)
064af421 1597{
df2c07f4 1598 struct ovs_header *ovs_header;
e926dfe3 1599 struct ovs_dp_stats dp_stats;
064af421
BP
1600 int err;
1601
28aea917 1602 ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
aaff4b55 1603 flags, cmd);
df2c07f4 1604 if (!ovs_header)
aaff4b55 1605 goto error;
064af421 1606
b063d9f0 1607 ovs_header->dp_ifindex = get_dpifindex(dp);
064af421 1608
d6569377 1609 rcu_read_lock();
850b6b3b 1610 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
d6569377 1611 rcu_read_unlock();
064af421 1612 if (err)
d6569377 1613 goto nla_put_failure;
064af421 1614
e926dfe3 1615 get_dp_stats(dp, &dp_stats);
c3cc8c03
DM
1616 if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats), &dp_stats))
1617 goto nla_put_failure;
d6569377 1618
df2c07f4 1619 return genlmsg_end(skb, ovs_header);
d6569377
BP
1620
1621nla_put_failure:
df2c07f4 1622 genlmsg_cancel(skb, ovs_header);
aaff4b55
BP
1623error:
1624 return -EMSGSIZE;
d6569377
BP
1625}
1626
28aea917 1627static struct sk_buff *ovs_dp_cmd_build_info(struct datapath *dp, u32 portid,
aaff4b55 1628 u32 seq, u8 cmd)
d6569377 1629{
d6569377 1630 struct sk_buff *skb;
aaff4b55 1631 int retval;
d6569377 1632
0afa2373 1633 skb = genlmsg_new(ovs_dp_cmd_msg_size(), GFP_KERNEL);
064af421 1634 if (!skb)
d6569377 1635 return ERR_PTR(-ENOMEM);
659586ef 1636
28aea917 1637 retval = ovs_dp_cmd_fill_info(dp, skb, portid, seq, 0, cmd);
aaff4b55
BP
1638 if (retval < 0) {
1639 kfree_skb(skb);
1640 return ERR_PTR(retval);
1641 }
1642 return skb;
1643}
9dca7bd5 1644
df2c07f4 1645static int ovs_dp_cmd_validate(struct nlattr *a[OVS_DP_ATTR_MAX + 1])
aaff4b55 1646{
df2c07f4 1647 return CHECK_NUL_STRING(a[OVS_DP_ATTR_NAME], IFNAMSIZ - 1);
d6569377
BP
1648}
1649
cd2a59e9 1650/* Called with ovs_mutex. */
2a4999f3
PS
1651static struct datapath *lookup_datapath(struct net *net,
1652 struct ovs_header *ovs_header,
6455100f 1653 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
d6569377 1654{
254f2dc8
BP
1655 struct datapath *dp;
1656
df2c07f4 1657 if (!a[OVS_DP_ATTR_NAME])
2a4999f3 1658 dp = get_dp(net, ovs_header->dp_ifindex);
254f2dc8 1659 else {
d6569377 1660 struct vport *vport;
d6569377 1661
057dd6d2 1662 rcu_read_lock();
2a4999f3 1663 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
df2c07f4 1664 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
057dd6d2 1665 rcu_read_unlock();
d6569377 1666 }
254f2dc8 1667 return dp ? dp : ERR_PTR(-ENODEV);
d6569377
BP
1668}
1669
df2c07f4 1670static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
d6569377 1671{
aaff4b55 1672 struct nlattr **a = info->attrs;
d6569377 1673 struct vport_parms parms;
aaff4b55 1674 struct sk_buff *reply;
d6569377
BP
1675 struct datapath *dp;
1676 struct vport *vport;
2a4999f3 1677 struct ovs_net *ovs_net;
95b1d73a 1678 int err, i;
d6569377 1679
d6569377 1680 err = -EINVAL;
ea36840f 1681 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
aaff4b55
BP
1682 goto err;
1683
df2c07f4 1684 err = ovs_dp_cmd_validate(a);
aaff4b55
BP
1685 if (err)
1686 goto err;
d6569377 1687
cd2a59e9 1688 ovs_lock();
d6569377 1689
d6569377
BP
1690 err = -ENOMEM;
1691 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1692 if (dp == NULL)
cd2a59e9 1693 goto err_unlock_ovs;
2a4999f3 1694
0ceaa66c
JG
1695 ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
1696
d6569377
BP
1697 /* Allocate table. */
1698 err = -ENOMEM;
850b6b3b 1699 rcu_assign_pointer(dp->table, ovs_flow_tbl_alloc(TBL_MIN_BUCKETS));
d6569377
BP
1700 if (!dp->table)
1701 goto err_free_dp;
1702
99769a40
JG
1703 dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
1704 if (!dp->stats_percpu) {
1705 err = -ENOMEM;
1706 goto err_destroy_table;
1707 }
1708
95b1d73a
PS
1709 dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
1710 GFP_KERNEL);
1711 if (!dp->ports) {
1712 err = -ENOMEM;
1713 goto err_destroy_percpu;
1714 }
1715
1716 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1717 INIT_HLIST_HEAD(&dp->ports[i]);
1718
d6569377 1719 /* Set up our datapath device. */
df2c07f4
JP
1720 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1721 parms.type = OVS_VPORT_TYPE_INTERNAL;
d6569377
BP
1722 parms.options = NULL;
1723 parms.dp = dp;
df2c07f4 1724 parms.port_no = OVSP_LOCAL;
28aea917 1725 parms.upcall_portid = nla_get_u32(a[OVS_DP_ATTR_UPCALL_PID]);
b063d9f0 1726
d6569377
BP
1727 vport = new_vport(&parms);
1728 if (IS_ERR(vport)) {
1729 err = PTR_ERR(vport);
1730 if (err == -EBUSY)
1731 err = -EEXIST;
1732
95b1d73a 1733 goto err_destroy_ports_array;
d6569377 1734 }
d6569377 1735
28aea917 1736 reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
6455100f 1737 info->snd_seq, OVS_DP_CMD_NEW);
aaff4b55
BP
1738 err = PTR_ERR(reply);
1739 if (IS_ERR(reply))
1740 goto err_destroy_local_port;
1741
2a4999f3
PS
1742 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
1743 list_add_tail(&dp->list_node, &ovs_net->dps);
d6569377 1744
cd2a59e9 1745 ovs_unlock();
d6569377 1746
e297c6b7 1747 ovs_notify(reply, info, &ovs_dp_datapath_multicast_group);
d6569377
BP
1748 return 0;
1749
1750err_destroy_local_port:
cd2a59e9 1751 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
95b1d73a
PS
1752err_destroy_ports_array:
1753 kfree(dp->ports);
99769a40
JG
1754err_destroy_percpu:
1755 free_percpu(dp->stats_percpu);
d6569377 1756err_destroy_table:
a1c564be 1757 ovs_flow_tbl_destroy(ovsl_dereference(dp->table), false);
d6569377 1758err_free_dp:
0ceaa66c 1759 release_net(ovs_dp_get_net(dp));
d6569377 1760 kfree(dp);
cd2a59e9
PS
1761err_unlock_ovs:
1762 ovs_unlock();
d6569377 1763err:
064af421
BP
1764 return err;
1765}
1766
cd2a59e9 1767/* Called with ovs_mutex. */
2a4999f3 1768static void __dp_destroy(struct datapath *dp)
44e05eca 1769{
95b1d73a 1770 int i;
44e05eca 1771
95b1d73a
PS
1772 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1773 struct vport *vport;
f8dfbcb7 1774 struct hlist_node *n;
95b1d73a 1775
f8dfbcb7 1776 hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
95b1d73a
PS
1777 if (vport->port_no != OVSP_LOCAL)
1778 ovs_dp_detach_port(vport);
1779 }
ed099e92 1780
254f2dc8 1781 list_del(&dp->list_node);
ed099e92 1782
cd2a59e9
PS
1783 /* OVSP_LOCAL is datapath internal port. We need to make sure that
1784 * all port in datapath are destroyed first before freeing datapath.
1785 */
1786 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
99620d2c 1787
ed099e92 1788 call_rcu(&dp->rcu, destroy_dp_rcu);
2a4999f3
PS
1789}
1790
1791static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1792{
1793 struct sk_buff *reply;
1794 struct datapath *dp;
1795 int err;
1796
1797 err = ovs_dp_cmd_validate(info->attrs);
1798 if (err)
1799 return err;
1800
cd2a59e9 1801 ovs_lock();
2a4999f3
PS
1802 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1803 err = PTR_ERR(dp);
1804 if (IS_ERR(dp))
cd2a59e9 1805 goto unlock;
2a4999f3 1806
28aea917 1807 reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
2a4999f3
PS
1808 info->snd_seq, OVS_DP_CMD_DEL);
1809 err = PTR_ERR(reply);
1810 if (IS_ERR(reply))
cd2a59e9 1811 goto unlock;
2a4999f3
PS
1812
1813 __dp_destroy(dp);
cd2a59e9 1814 ovs_unlock();
ed099e92 1815
e297c6b7 1816 ovs_notify(reply, info, &ovs_dp_datapath_multicast_group);
99620d2c
JG
1817
1818 return 0;
cd2a59e9
PS
1819unlock:
1820 ovs_unlock();
1821 return err;
44e05eca
BP
1822}
1823
df2c07f4 1824static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
064af421 1825{
aaff4b55 1826 struct sk_buff *reply;
d6569377 1827 struct datapath *dp;
d6569377 1828 int err;
064af421 1829
df2c07f4 1830 err = ovs_dp_cmd_validate(info->attrs);
aaff4b55
BP
1831 if (err)
1832 return err;
38c6ecbc 1833
cd2a59e9 1834 ovs_lock();
2a4999f3 1835 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
cd2a59e9 1836 err = PTR_ERR(dp);
d6569377 1837 if (IS_ERR(dp))
cd2a59e9 1838 goto unlock;
38c6ecbc 1839
28aea917 1840 reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
6455100f 1841 info->snd_seq, OVS_DP_CMD_NEW);
aaff4b55
BP
1842 if (IS_ERR(reply)) {
1843 err = PTR_ERR(reply);
2a4999f3 1844 netlink_set_err(GENL_SOCK(sock_net(skb->sk)), 0,
850b6b3b 1845 ovs_dp_datapath_multicast_group.id, err);
cd2a59e9
PS
1846 err = 0;
1847 goto unlock;
aaff4b55
BP
1848 }
1849
cd2a59e9 1850 ovs_unlock();
e297c6b7 1851 ovs_notify(reply, info, &ovs_dp_datapath_multicast_group);
850b6b3b 1852
aaff4b55 1853 return 0;
cd2a59e9
PS
1854unlock:
1855 ovs_unlock();
1856 return err;
064af421
BP
1857}
1858
df2c07f4 1859static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1dcf111b 1860{
aaff4b55 1861 struct sk_buff *reply;
d6569377 1862 struct datapath *dp;
d6569377 1863 int err;
1dcf111b 1864
df2c07f4 1865 err = ovs_dp_cmd_validate(info->attrs);
aaff4b55
BP
1866 if (err)
1867 return err;
1dcf111b 1868
cd2a59e9 1869 ovs_lock();
2a4999f3 1870 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
cd2a59e9
PS
1871 if (IS_ERR(dp)) {
1872 err = PTR_ERR(dp);
1873 goto unlock;
1874 }
1dcf111b 1875
28aea917 1876 reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
6455100f 1877 info->snd_seq, OVS_DP_CMD_NEW);
cd2a59e9
PS
1878 if (IS_ERR(reply)) {
1879 err = PTR_ERR(reply);
1880 goto unlock;
1881 }
aaff4b55 1882
cd2a59e9 1883 ovs_unlock();
aaff4b55 1884 return genlmsg_reply(reply, info);
cd2a59e9
PS
1885
1886unlock:
1887 ovs_unlock();
1888 return err;
1dcf111b
JP
1889}
1890
df2c07f4 1891static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
a7786963 1892{
2a4999f3 1893 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
254f2dc8
BP
1894 struct datapath *dp;
1895 int skip = cb->args[0];
1896 int i = 0;
a7786963 1897
cd2a59e9 1898 ovs_lock();
2a4999f3 1899 list_for_each_entry(dp, &ovs_net->dps, list_node) {
a2bab2f0 1900 if (i >= skip &&
28aea917 1901 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
aaff4b55 1902 cb->nlh->nlmsg_seq, NLM_F_MULTI,
df2c07f4 1903 OVS_DP_CMD_NEW) < 0)
aaff4b55 1904 break;
254f2dc8 1905 i++;
a7786963 1906 }
cd2a59e9 1907 ovs_unlock();
aaff4b55 1908
254f2dc8
BP
1909 cb->args[0] = i;
1910
aaff4b55 1911 return skb->len;
c19e6535
BP
1912}
1913
aaff4b55 1914static struct genl_ops dp_datapath_genl_ops[] = {
df2c07f4 1915 { .cmd = OVS_DP_CMD_NEW,
aaff4b55
BP
1916 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1917 .policy = datapath_policy,
df2c07f4 1918 .doit = ovs_dp_cmd_new
aaff4b55 1919 },
df2c07f4 1920 { .cmd = OVS_DP_CMD_DEL,
aaff4b55
BP
1921 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1922 .policy = datapath_policy,
df2c07f4 1923 .doit = ovs_dp_cmd_del
aaff4b55 1924 },
df2c07f4 1925 { .cmd = OVS_DP_CMD_GET,
aaff4b55
BP
1926 .flags = 0, /* OK for unprivileged users. */
1927 .policy = datapath_policy,
df2c07f4
JP
1928 .doit = ovs_dp_cmd_get,
1929 .dumpit = ovs_dp_cmd_dump
aaff4b55 1930 },
df2c07f4 1931 { .cmd = OVS_DP_CMD_SET,
aaff4b55
BP
1932 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1933 .policy = datapath_policy,
df2c07f4 1934 .doit = ovs_dp_cmd_set,
aaff4b55
BP
1935 },
1936};
1937
df2c07f4 1938static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
f0fef760 1939#ifdef HAVE_NLA_NUL_STRING
df2c07f4 1940 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
f613a0d7 1941 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
f0fef760 1942#else
f613a0d7 1943 [OVS_VPORT_ATTR_STATS] = { .minlen = sizeof(struct ovs_vport_stats) },
f0fef760 1944#endif
d48c88ec
JG
1945 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
1946 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
b063d9f0 1947 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
df2c07f4 1948 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
c19e6535
BP
1949};
1950
f0fef760
BP
1951static struct genl_family dp_vport_genl_family = {
1952 .id = GENL_ID_GENERATE,
df2c07f4
JP
1953 .hdrsize = sizeof(struct ovs_header),
1954 .name = OVS_VPORT_FAMILY,
69685a88 1955 .version = OVS_VPORT_VERSION,
2a4999f3
PS
1956 .maxattr = OVS_VPORT_ATTR_MAX,
1957 SET_NETNSOK
f0fef760
BP
1958};
1959
850b6b3b 1960struct genl_multicast_group ovs_dp_vport_multicast_group = {
df2c07f4 1961 .name = OVS_VPORT_MCGROUP
f0fef760
BP
1962};
1963
cd2a59e9 1964/* Called with ovs_mutex or RCU read lock. */
df2c07f4 1965static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
28aea917 1966 u32 portid, u32 seq, u32 flags, u8 cmd)
064af421 1967{
df2c07f4 1968 struct ovs_header *ovs_header;
e926dfe3 1969 struct ovs_vport_stats vport_stats;
c19e6535
BP
1970 int err;
1971
28aea917 1972 ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
f0fef760 1973 flags, cmd);
df2c07f4 1974 if (!ovs_header)
f0fef760 1975 return -EMSGSIZE;
c19e6535 1976
99769a40 1977 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
c19e6535 1978
c3cc8c03
DM
1979 if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1980 nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
1981 nla_put_string(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport)) ||
28aea917 1982 nla_put_u32(skb, OVS_VPORT_ATTR_UPCALL_PID, vport->upcall_portid))
c3cc8c03 1983 goto nla_put_failure;
c19e6535 1984
850b6b3b 1985 ovs_vport_get_stats(vport, &vport_stats);
c3cc8c03
DM
1986 if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
1987 &vport_stats))
1988 goto nla_put_failure;
c19e6535 1989
850b6b3b 1990 err = ovs_vport_get_options(vport, skb);
f0fef760
BP
1991 if (err == -EMSGSIZE)
1992 goto error;
c19e6535 1993
df2c07f4 1994 return genlmsg_end(skb, ovs_header);
c19e6535
BP
1995
1996nla_put_failure:
1997 err = -EMSGSIZE;
f0fef760 1998error:
df2c07f4 1999 genlmsg_cancel(skb, ovs_header);
f0fef760 2000 return err;
064af421
BP
2001}
2002
cd2a59e9 2003/* Called with ovs_mutex or RCU read lock. */
28aea917 2004struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
f14d8083 2005 u32 seq, u8 cmd)
064af421 2006{
c19e6535 2007 struct sk_buff *skb;
f0fef760 2008 int retval;
c19e6535 2009
f0fef760 2010 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
c19e6535
BP
2011 if (!skb)
2012 return ERR_PTR(-ENOMEM);
2013
28aea917 2014 retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
c25ea534
JG
2015 BUG_ON(retval < 0);
2016
c19e6535 2017 return skb;
f0fef760 2018}
c19e6535 2019
df2c07f4 2020static int ovs_vport_cmd_validate(struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
f0fef760 2021{
df2c07f4 2022 return CHECK_NUL_STRING(a[OVS_VPORT_ATTR_NAME], IFNAMSIZ - 1);
c19e6535 2023}
51d4d598 2024
cd2a59e9 2025/* Called with ovs_mutex or RCU read lock. */
2a4999f3
PS
2026static struct vport *lookup_vport(struct net *net,
2027 struct ovs_header *ovs_header,
df2c07f4 2028 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
c19e6535
BP
2029{
2030 struct datapath *dp;
2031 struct vport *vport;
2032
df2c07f4 2033 if (a[OVS_VPORT_ATTR_NAME]) {
2a4999f3 2034 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
ed099e92 2035 if (!vport)
c19e6535 2036 return ERR_PTR(-ENODEV);
24ce832d
BP
2037 if (ovs_header->dp_ifindex &&
2038 ovs_header->dp_ifindex != get_dpifindex(vport->dp))
2039 return ERR_PTR(-ENODEV);
c19e6535 2040 return vport;
df2c07f4
JP
2041 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
2042 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
c19e6535
BP
2043
2044 if (port_no >= DP_MAX_PORTS)
f0fef760 2045 return ERR_PTR(-EFBIG);
c19e6535 2046
2a4999f3 2047 dp = get_dp(net, ovs_header->dp_ifindex);
c19e6535
BP
2048 if (!dp)
2049 return ERR_PTR(-ENODEV);
f2459fe7 2050
cd2a59e9 2051 vport = ovs_vport_ovsl_rcu(dp, port_no);
ed099e92 2052 if (!vport)
17535c57 2053 return ERR_PTR(-ENODEV);
c19e6535
BP
2054 return vport;
2055 } else
2056 return ERR_PTR(-EINVAL);
064af421
BP
2057}
2058
df2c07f4 2059static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
c19e6535 2060{
f0fef760 2061 struct nlattr **a = info->attrs;
df2c07f4 2062 struct ovs_header *ovs_header = info->userhdr;
c19e6535 2063 struct vport_parms parms;
ed099e92 2064 struct sk_buff *reply;
c19e6535 2065 struct vport *vport;
c19e6535 2066 struct datapath *dp;
b0ec0f27 2067 u32 port_no;
c19e6535 2068 int err;
b0ec0f27 2069
c19e6535 2070 err = -EINVAL;
ea36840f
BP
2071 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
2072 !a[OVS_VPORT_ATTR_UPCALL_PID])
f0fef760
BP
2073 goto exit;
2074
df2c07f4 2075 err = ovs_vport_cmd_validate(a);
f0fef760
BP
2076 if (err)
2077 goto exit;
51d4d598 2078
cd2a59e9 2079 ovs_lock();
2a4999f3 2080 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
c19e6535
BP
2081 err = -ENODEV;
2082 if (!dp)
ed099e92 2083 goto exit_unlock;
c19e6535 2084
df2c07f4
JP
2085 if (a[OVS_VPORT_ATTR_PORT_NO]) {
2086 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
c19e6535
BP
2087
2088 err = -EFBIG;
2089 if (port_no >= DP_MAX_PORTS)
ed099e92 2090 goto exit_unlock;
c19e6535 2091
cd2a59e9 2092 vport = ovs_vport_ovsl(dp, port_no);
c19e6535
BP
2093 err = -EBUSY;
2094 if (vport)
ed099e92 2095 goto exit_unlock;
c19e6535
BP
2096 } else {
2097 for (port_no = 1; ; port_no++) {
2098 if (port_no >= DP_MAX_PORTS) {
2099 err = -EFBIG;
ed099e92 2100 goto exit_unlock;
c19e6535 2101 }
cd2a59e9 2102 vport = ovs_vport_ovsl(dp, port_no);
c19e6535
BP
2103 if (!vport)
2104 break;
51d4d598 2105 }
064af421 2106 }
b0ec0f27 2107
df2c07f4
JP
2108 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
2109 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
2110 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
c19e6535
BP
2111 parms.dp = dp;
2112 parms.port_no = port_no;
28aea917 2113 parms.upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
c19e6535
BP
2114
2115 vport = new_vport(&parms);
2116 err = PTR_ERR(vport);
2117 if (IS_ERR(vport))
ed099e92 2118 goto exit_unlock;
c19e6535 2119
faef6d2d 2120 err = 0;
1fc7083d
JG
2121 if (a[OVS_VPORT_ATTR_STATS])
2122 ovs_vport_set_stats(vport, nla_data(a[OVS_VPORT_ATTR_STATS]));
2123
2124 reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq,
2125 OVS_VPORT_CMD_NEW);
2126 if (IS_ERR(reply)) {
2127 err = PTR_ERR(reply);
850b6b3b 2128 ovs_dp_detach_port(vport);
ed099e92 2129 goto exit_unlock;
c19e6535 2130 }
e297c6b7
TG
2131
2132 ovs_notify(reply, info, &ovs_dp_vport_multicast_group);
c19e6535 2133
ed099e92 2134exit_unlock:
cd2a59e9 2135 ovs_unlock();
c19e6535
BP
2136exit:
2137 return err;
44e05eca
BP
2138}
2139
df2c07f4 2140static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
44e05eca 2141{
f0fef760
BP
2142 struct nlattr **a = info->attrs;
2143 struct sk_buff *reply;
c19e6535 2144 struct vport *vport;
c19e6535 2145 int err;
44e05eca 2146
df2c07f4 2147 err = ovs_vport_cmd_validate(a);
f0fef760 2148 if (err)
c19e6535
BP
2149 goto exit;
2150
cd2a59e9 2151 ovs_lock();
2a4999f3 2152 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
c19e6535
BP
2153 err = PTR_ERR(vport);
2154 if (IS_ERR(vport))
f0fef760 2155 goto exit_unlock;
44e05eca 2156
6455100f 2157 if (a[OVS_VPORT_ATTR_TYPE] &&
17ec1d04 2158 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
4879d4c7 2159 err = -EINVAL;
17ec1d04
JG
2160 goto exit_unlock;
2161 }
6455100f 2162
c25ea534
JG
2163 reply = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2164 if (!reply) {
2165 err = -ENOMEM;
2166 goto exit_unlock;
2167 }
2168
17ec1d04 2169 if (a[OVS_VPORT_ATTR_OPTIONS]) {
850b6b3b 2170 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
17ec1d04
JG
2171 if (err)
2172 goto exit_free;
2173 }
1fc7083d
JG
2174
2175 if (a[OVS_VPORT_ATTR_STATS])
2176 ovs_vport_set_stats(vport, nla_data(a[OVS_VPORT_ATTR_STATS]));
2177
2178 if (a[OVS_VPORT_ATTR_UPCALL_PID])
28aea917 2179 vport->upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
c19e6535 2180
c25ea534
JG
2181 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
2182 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
2183 BUG_ON(err < 0);
f0fef760 2184
cd2a59e9 2185 ovs_unlock();
8680ae4d 2186 ovs_notify(reply, info, &ovs_dp_vport_multicast_group);
c25ea534
JG
2187 return 0;
2188
2189exit_free:
2190 kfree_skb(reply);
f0fef760 2191exit_unlock:
cd2a59e9 2192 ovs_unlock();
c19e6535
BP
2193exit:
2194 return err;
064af421
BP
2195}
2196
df2c07f4 2197static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
7c40efc9 2198{
f0fef760
BP
2199 struct nlattr **a = info->attrs;
2200 struct sk_buff *reply;
c19e6535 2201 struct vport *vport;
c19e6535
BP
2202 int err;
2203
df2c07f4 2204 err = ovs_vport_cmd_validate(a);
f0fef760 2205 if (err)
c19e6535
BP
2206 goto exit;
2207
cd2a59e9 2208 ovs_lock();
2a4999f3 2209 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
c19e6535 2210 err = PTR_ERR(vport);
f0fef760
BP
2211 if (IS_ERR(vport))
2212 goto exit_unlock;
c19e6535 2213
df2c07f4 2214 if (vport->port_no == OVSP_LOCAL) {
f0fef760
BP
2215 err = -EINVAL;
2216 goto exit_unlock;
2217 }
2218
28aea917
IY
2219 reply = ovs_vport_cmd_build_info(vport, info->snd_portid,
2220 info->snd_seq, OVS_VPORT_CMD_DEL);
f0fef760
BP
2221 err = PTR_ERR(reply);
2222 if (IS_ERR(reply))
2223 goto exit_unlock;
2224
b57d5819 2225 err = 0;
850b6b3b 2226 ovs_dp_detach_port(vport);
f0fef760 2227
e297c6b7 2228 ovs_notify(reply, info, &ovs_dp_vport_multicast_group);
f0fef760
BP
2229
2230exit_unlock:
cd2a59e9 2231 ovs_unlock();
c19e6535
BP
2232exit:
2233 return err;
7c40efc9
BP
2234}
2235
df2c07f4 2236static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
7c40efc9 2237{
f0fef760 2238 struct nlattr **a = info->attrs;
df2c07f4 2239 struct ovs_header *ovs_header = info->userhdr;
ed099e92 2240 struct sk_buff *reply;
c19e6535 2241 struct vport *vport;
c19e6535
BP
2242 int err;
2243
df2c07f4 2244 err = ovs_vport_cmd_validate(a);
f0fef760
BP
2245 if (err)
2246 goto exit;
c19e6535 2247
ed099e92 2248 rcu_read_lock();
2a4999f3 2249 vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
c19e6535
BP
2250 err = PTR_ERR(vport);
2251 if (IS_ERR(vport))
f0fef760 2252 goto exit_unlock;
c19e6535 2253
28aea917
IY
2254 reply = ovs_vport_cmd_build_info(vport, info->snd_portid,
2255 info->snd_seq, OVS_VPORT_CMD_NEW);
ed099e92
BP
2256 err = PTR_ERR(reply);
2257 if (IS_ERR(reply))
f0fef760 2258 goto exit_unlock;
ed099e92 2259
df2fa9b5
JG
2260 rcu_read_unlock();
2261
2262 return genlmsg_reply(reply, info);
ed099e92 2263
f0fef760 2264exit_unlock:
ed099e92 2265 rcu_read_unlock();
f0fef760 2266exit:
c19e6535
BP
2267 return err;
2268}
2269
df2c07f4 2270static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
c19e6535 2271{
df2c07f4 2272 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
c19e6535 2273 struct datapath *dp;
95b1d73a
PS
2274 int bucket = cb->args[0], skip = cb->args[1];
2275 int i, j = 0;
c19e6535 2276
2a4999f3 2277 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
c19e6535 2278 if (!dp)
f0fef760 2279 return -ENODEV;
ed099e92
BP
2280
2281 rcu_read_lock();
95b1d73a 2282 for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
ed099e92 2283 struct vport *vport;
95b1d73a
PS
2284
2285 j = 0;
f8dfbcb7 2286 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
95b1d73a
PS
2287 if (j >= skip &&
2288 ovs_vport_cmd_fill_info(vport, skb,
28aea917 2289 NETLINK_CB(cb->skb).portid,
95b1d73a
PS
2290 cb->nlh->nlmsg_seq,
2291 NLM_F_MULTI,
2292 OVS_VPORT_CMD_NEW) < 0)
2293 goto out;
2294
2295 j++;
2296 }
2297 skip = 0;
c19e6535 2298 }
95b1d73a 2299out:
ed099e92 2300 rcu_read_unlock();
c19e6535 2301
95b1d73a
PS
2302 cb->args[0] = i;
2303 cb->args[1] = j;
f0fef760 2304
95b1d73a 2305 return skb->len;
7c40efc9
BP
2306}
2307
f0fef760 2308static struct genl_ops dp_vport_genl_ops[] = {
df2c07f4 2309 { .cmd = OVS_VPORT_CMD_NEW,
f0fef760
BP
2310 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2311 .policy = vport_policy,
df2c07f4 2312 .doit = ovs_vport_cmd_new
f0fef760 2313 },
df2c07f4 2314 { .cmd = OVS_VPORT_CMD_DEL,
f0fef760
BP
2315 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2316 .policy = vport_policy,
df2c07f4 2317 .doit = ovs_vport_cmd_del
f0fef760 2318 },
df2c07f4 2319 { .cmd = OVS_VPORT_CMD_GET,
f0fef760
BP
2320 .flags = 0, /* OK for unprivileged users. */
2321 .policy = vport_policy,
df2c07f4
JP
2322 .doit = ovs_vport_cmd_get,
2323 .dumpit = ovs_vport_cmd_dump
f0fef760 2324 },
df2c07f4 2325 { .cmd = OVS_VPORT_CMD_SET,
f0fef760
BP
2326 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2327 .policy = vport_policy,
df2c07f4 2328 .doit = ovs_vport_cmd_set,
f0fef760
BP
2329 },
2330};
2331
982b8810
BP
2332struct genl_family_and_ops {
2333 struct genl_family *family;
2334 struct genl_ops *ops;
2335 int n_ops;
2336 struct genl_multicast_group *group;
2337};
ed099e92 2338
982b8810 2339static const struct genl_family_and_ops dp_genl_families[] = {
aaff4b55
BP
2340 { &dp_datapath_genl_family,
2341 dp_datapath_genl_ops, ARRAY_SIZE(dp_datapath_genl_ops),
850b6b3b 2342 &ovs_dp_datapath_multicast_group },
f0fef760
BP
2343 { &dp_vport_genl_family,
2344 dp_vport_genl_ops, ARRAY_SIZE(dp_vport_genl_ops),
850b6b3b 2345 &ovs_dp_vport_multicast_group },
37a1300c
BP
2346 { &dp_flow_genl_family,
2347 dp_flow_genl_ops, ARRAY_SIZE(dp_flow_genl_ops),
850b6b3b 2348 &ovs_dp_flow_multicast_group },
982b8810
BP
2349 { &dp_packet_genl_family,
2350 dp_packet_genl_ops, ARRAY_SIZE(dp_packet_genl_ops),
2351 NULL },
2352};
ed099e92 2353
982b8810
BP
2354static void dp_unregister_genl(int n_families)
2355{
2356 int i;
ed099e92 2357
b867ca75 2358 for (i = 0; i < n_families; i++)
982b8810 2359 genl_unregister_family(dp_genl_families[i].family);
ed099e92
BP
2360}
2361
982b8810 2362static int dp_register_genl(void)
064af421 2363{
982b8810
BP
2364 int n_registered;
2365 int err;
2366 int i;
064af421 2367
982b8810
BP
2368 n_registered = 0;
2369 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
2370 const struct genl_family_and_ops *f = &dp_genl_families[i];
064af421 2371
982b8810
BP
2372 err = genl_register_family_with_ops(f->family, f->ops,
2373 f->n_ops);
2374 if (err)
2375 goto error;
2376 n_registered++;
e22d4953 2377
982b8810
BP
2378 if (f->group) {
2379 err = genl_register_mc_group(f->family, f->group);
2380 if (err)
2381 goto error;
2382 }
2383 }
9cc8b4e4 2384
982b8810 2385 return 0;
064af421
BP
2386
2387error:
982b8810
BP
2388 dp_unregister_genl(n_registered);
2389 return err;
064af421
BP
2390}
2391
cd2a59e9 2392static void rehash_flow_table(struct work_struct *work)
acd051f1
PS
2393{
2394 struct datapath *dp;
2a4999f3
PS
2395 struct net *net;
2396
cd2a59e9 2397 ovs_lock();
2a4999f3
PS
2398 rtnl_lock();
2399 for_each_net(net) {
2400 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
acd051f1 2401
2a4999f3 2402 list_for_each_entry(dp, &ovs_net->dps, list_node) {
cd2a59e9 2403 struct flow_table *old_table = ovsl_dereference(dp->table);
2a4999f3 2404 struct flow_table *new_table;
acd051f1 2405
2a4999f3
PS
2406 new_table = ovs_flow_tbl_rehash(old_table);
2407 if (!IS_ERR(new_table)) {
2408 rcu_assign_pointer(dp->table, new_table);
a1c564be 2409 ovs_flow_tbl_destroy(old_table, true);
2a4999f3 2410 }
acd051f1
PS
2411 }
2412 }
2a4999f3 2413 rtnl_unlock();
cd2a59e9 2414 ovs_unlock();
acd051f1
PS
2415 schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
2416}
2417
2a4999f3
PS
2418static int __net_init ovs_init_net(struct net *net)
2419{
2420 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2421
2422 INIT_LIST_HEAD(&ovs_net->dps);
cd2a59e9 2423 INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
2a4999f3
PS
2424 return 0;
2425}
2426
2427static void __net_exit ovs_exit_net(struct net *net)
2428{
cd2a59e9 2429 struct datapath *dp, *dp_next;
2a4999f3
PS
2430 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2431
cd2a59e9
PS
2432 ovs_lock();
2433 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
2434 __dp_destroy(dp);
2435 ovs_unlock();
2436
2437 cancel_work_sync(&ovs_net->dp_notify_work);
2a4999f3
PS
2438}
2439
2440static struct pernet_operations ovs_net_ops = {
2441 .init = ovs_init_net,
2442 .exit = ovs_exit_net,
2443 .id = &ovs_net_id,
2444 .size = sizeof(struct ovs_net),
2445};
2446
22d24ebf
BP
2447static int __init dp_init(void)
2448{
2449 int err;
2450
f3d85db3 2451 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
22d24ebf 2452
dc5f3fef 2453 pr_info("Open vSwitch switching datapath %s, built "__DATE__" "__TIME__"\n",
8a07709c 2454 VERSION);
064af421 2455
16d650e5 2456 err = ovs_workqueues_init();
b9c15df9 2457 if (err)
cd2a59e9 2458 goto error;
b9c15df9 2459
850b6b3b 2460 err = ovs_flow_init();
3544358a 2461 if (err)
85c9de19 2462 goto error_wq;
3544358a 2463
850b6b3b 2464 err = ovs_vport_init();
064af421
BP
2465 if (err)
2466 goto error_flow_exit;
2467
2a4999f3 2468 err = register_pernet_device(&ovs_net_ops);
f2459fe7
JG
2469 if (err)
2470 goto error_vport_exit;
2471
2a4999f3
PS
2472 err = register_netdevice_notifier(&ovs_dp_device_notifier);
2473 if (err)
2474 goto error_netns_exit;
2475
982b8810
BP
2476 err = dp_register_genl();
2477 if (err < 0)
37a1300c 2478 goto error_unreg_notifier;
982b8810 2479
acd051f1
PS
2480 schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
2481
064af421
BP
2482 return 0;
2483
2484error_unreg_notifier:
850b6b3b 2485 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2a4999f3
PS
2486error_netns_exit:
2487 unregister_pernet_device(&ovs_net_ops);
f2459fe7 2488error_vport_exit:
850b6b3b 2489 ovs_vport_exit();
064af421 2490error_flow_exit:
850b6b3b 2491 ovs_flow_exit();
16d650e5
PS
2492error_wq:
2493 ovs_workqueues_exit();
064af421
BP
2494error:
2495 return err;
2496}
2497
2498static void dp_cleanup(void)
2499{
acd051f1 2500 cancel_delayed_work_sync(&rehash_flow_wq);
982b8810 2501 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
850b6b3b 2502 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2a4999f3
PS
2503 unregister_pernet_device(&ovs_net_ops);
2504 rcu_barrier();
850b6b3b
JG
2505 ovs_vport_exit();
2506 ovs_flow_exit();
16d650e5 2507 ovs_workqueues_exit();
064af421
BP
2508}
2509
2510module_init(dp_init);
2511module_exit(dp_cleanup);
2512
2513MODULE_DESCRIPTION("Open vSwitch switching datapath");
2514MODULE_LICENSE("GPL");
3d0666d2 2515MODULE_VERSION(VERSION);