~ubuntu-branches/ubuntu/raring/openvswitch/raring

« back to all changes in this revision

Viewing changes to .pc/0006-Bump-up-the-supported-kernel-versions-to-include-3.5.patch/datapath/datapath.c

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2012-10-23 17:24:17 UTC
  • Revision ID: package-import@ubuntu.com-20121023172417-26590u7zfr4r795j
Tags: 1.4.3-0ubuntu2
* Re-enable the openvswitch-datapath-dkms package to enable support
  for gre tunnels between virtual switches which is not supported
  in the kernel provided openvswitch module (LP: #1068365). 
  - d/patches/0001->0008*.patch: Cherry picked patches from upstream
    trunk which enable support for the 3.5 linux kernel and align
    dkms module naming with kernel module naming.
  - d/dkms.conf.in: Drop _mod postfix from dkms module names.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (c) 2007-2012 Nicira Networks.
 
3
 *
 
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
 
17
 */
 
18
 
 
19
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 
20
 
 
21
#include <linux/init.h>
 
22
#include <linux/module.h>
 
23
#include <linux/if_arp.h>
 
24
#include <linux/if_vlan.h>
 
25
#include <linux/in.h>
 
26
#include <linux/ip.h>
 
27
#include <linux/jhash.h>
 
28
#include <linux/delay.h>
 
29
#include <linux/time.h>
 
30
#include <linux/etherdevice.h>
 
31
#include <linux/genetlink.h>
 
32
#include <linux/kernel.h>
 
33
#include <linux/kthread.h>
 
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>
 
41
#include <linux/wait.h>
 
42
#include <asm/system.h>
 
43
#include <asm/div64.h>
 
44
#include <linux/highmem.h>
 
45
#include <linux/netfilter_bridge.h>
 
46
#include <linux/netfilter_ipv4.h>
 
47
#include <linux/inetdevice.h>
 
48
#include <linux/list.h>
 
49
#include <linux/openvswitch.h>
 
50
#include <linux/rculist.h>
 
51
#include <linux/dmi.h>
 
52
#include <net/genetlink.h>
 
53
 
 
54
#include "checksum.h"
 
55
#include "datapath.h"
 
56
#include "flow.h"
 
57
#include "vlan.h"
 
58
#include "tunnel.h"
 
59
#include "vport-internal_dev.h"
 
60
 
 
61
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18) || \
 
62
    LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)
 
63
#error Kernels before 2.6.18 or after 3.3 are not supported by this version of Open vSwitch.
 
64
#endif
 
65
 
 
66
int (*ovs_dp_ioctl_hook)(struct net_device *dev, struct ifreq *rq, int cmd);
 
67
EXPORT_SYMBOL(ovs_dp_ioctl_hook);
 
68
 
 
69
/**
 
70
 * DOC: Locking:
 
71
 *
 
72
 * Writes to device state (add/remove datapath, port, set operations on vports,
 
73
 * etc.) are protected by RTNL.
 
74
 *
 
75
 * Writes to other state (flow table modifications, set miscellaneous datapath
 
76
 * parameters, etc.) are protected by genl_mutex.  The RTNL lock nests inside
 
77
 * genl_mutex.
 
78
 *
 
79
 * Reads are protected by RCU.
 
80
 *
 
81
 * There are a few special cases (mostly stats) that have their own
 
82
 * synchronization but they nest under all of above and don't interact with
 
83
 * each other.
 
84
 */
 
85
 
 
86
/* Global list of datapaths to enable dumping them all out.
 
87
 * Protected by genl_mutex.
 
88
 */
 
89
static LIST_HEAD(dps);
 
90
 
 
91
static struct vport *new_vport(const struct vport_parms *);
 
92
static int queue_gso_packets(int dp_ifindex, struct sk_buff *,
 
93
                             const struct dp_upcall_info *);
 
94
static int queue_userspace_packet(int dp_ifindex, struct sk_buff *,
 
95
                                  const struct dp_upcall_info *);
 
96
 
 
97
/* Must be called with rcu_read_lock, genl_mutex, or RTNL lock. */
 
98
static struct datapath *get_dp(int dp_ifindex)
 
99
{
 
100
        struct datapath *dp = NULL;
 
101
        struct net_device *dev;
 
102
 
 
103
        rcu_read_lock();
 
104
        dev = dev_get_by_index_rcu(&init_net, dp_ifindex);
 
105
        if (dev) {
 
106
                struct vport *vport = ovs_internal_dev_get_vport(dev);
 
107
                if (vport)
 
108
                        dp = vport->dp;
 
109
        }
 
110
        rcu_read_unlock();
 
111
 
 
112
        return dp;
 
113
}
 
114
 
 
115
/* Must be called with rcu_read_lock or RTNL lock. */
 
116
const char *ovs_dp_name(const struct datapath *dp)
 
117
{
 
118
        struct vport *vport = rcu_dereference_rtnl(dp->ports[OVSP_LOCAL]);
 
119
        return vport->ops->get_name(vport);
 
120
}
 
121
 
 
122
static int get_dpifindex(struct datapath *dp)
 
123
{
 
124
        struct vport *local;
 
125
        int ifindex;
 
126
 
 
127
        rcu_read_lock();
 
128
 
 
129
        local = rcu_dereference(dp->ports[OVSP_LOCAL]);
 
130
        if (local)
 
131
                ifindex = local->ops->get_ifindex(local);
 
132
        else
 
133
                ifindex = 0;
 
134
 
 
135
        rcu_read_unlock();
 
136
 
 
137
        return ifindex;
 
138
}
 
139
 
 
140
static size_t br_nlmsg_size(void)
 
141
{
 
142
        return NLMSG_ALIGN(sizeof(struct ifinfomsg))
 
143
               + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
 
144
               + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
 
145
               + nla_total_size(4) /* IFLA_MASTER */
 
146
               + nla_total_size(4) /* IFLA_MTU */
 
147
               + nla_total_size(1); /* IFLA_OPERSTATE */
 
148
}
 
149
 
 
150
/* Caller must hold RTNL lock. */
 
151
static int dp_fill_ifinfo(struct sk_buff *skb,
 
152
                          const struct vport *port,
 
153
                          int event, unsigned int flags)
 
154
{
 
155
        struct datapath *dp = port->dp;
 
156
        struct ifinfomsg *hdr;
 
157
        struct nlmsghdr *nlh;
 
158
 
 
159
        if (!port->ops->get_ifindex)
 
160
                return -ENODEV;
 
161
 
 
162
        nlh = nlmsg_put(skb, 0, 0, event, sizeof(*hdr), flags);
 
163
        if (nlh == NULL)
 
164
                return -EMSGSIZE;
 
165
 
 
166
        hdr = nlmsg_data(nlh);
 
167
        hdr->ifi_family = AF_BRIDGE;
 
168
        hdr->__ifi_pad = 0;
 
169
        hdr->ifi_type = ARPHRD_ETHER;
 
170
        hdr->ifi_index = port->ops->get_ifindex(port);
 
171
        hdr->ifi_flags = port->ops->get_dev_flags(port);
 
172
        hdr->ifi_change = 0;
 
173
 
 
174
        if (nla_put_string(skb, IFLA_IFNAME, port->ops->get_name(port)) ||
 
175
            nla_put_u32(skb, IFLA_MASTER, get_dpifindex(dp)) ||
 
176
            nla_put_u32(skb, IFLA_MTU, port->ops->get_mtu(port)) ||
 
177
#ifdef IFLA_OPERSTATE
 
178
            nla_put_u8(skb, IFLA_OPERSTATE,
 
179
                       port->ops->is_running(port) ?
 
180
                                port->ops->get_operstate(port) :
 
181
                                IF_OPER_DOWN) ||
 
182
#endif
 
183
            nla_put(skb, IFLA_ADDRESS, ETH_ALEN, port->ops->get_addr(port)))
 
184
                goto nla_put_failure;
 
185
 
 
186
        return nlmsg_end(skb, nlh);
 
187
 
 
188
nla_put_failure:
 
189
        nlmsg_cancel(skb, nlh);
 
190
        return -EMSGSIZE;
 
191
}
 
192
 
 
193
/* Caller must hold RTNL lock. */
 
194
static void dp_ifinfo_notify(int event, struct vport *port)
 
195
{
 
196
        struct sk_buff *skb;
 
197
        int err;
 
198
 
 
199
        skb = nlmsg_new(br_nlmsg_size(), GFP_KERNEL);
 
200
        if (!skb) {
 
201
                err = -ENOBUFS;
 
202
                goto err;
 
203
        }
 
204
 
 
205
        err = dp_fill_ifinfo(skb, port, event, 0);
 
206
        if (err < 0) {
 
207
                if (err == -ENODEV) {
 
208
                        goto out;
 
209
                } else {
 
210
                        /* -EMSGSIZE implies BUG in br_nlmsg_size() */
 
211
                        WARN_ON(err == -EMSGSIZE);
 
212
                        goto err;
 
213
                }
 
214
        }
 
215
 
 
216
        rtnl_notify(skb, &init_net, 0, RTNLGRP_LINK, NULL, GFP_KERNEL);
 
217
 
 
218
        return;
 
219
err:
 
220
        rtnl_set_sk_err(&init_net, RTNLGRP_LINK, err);
 
221
out:
 
222
        kfree_skb(skb);
 
223
}
 
224
 
 
225
static void release_dp(struct kobject *kobj)
 
226
{
 
227
        struct datapath *dp = container_of(kobj, struct datapath, ifobj);
 
228
        kfree(dp);
 
229
}
 
230
 
 
231
static struct kobj_type dp_ktype = {
 
232
        .release = release_dp
 
233
};
 
234
 
 
235
static void destroy_dp_rcu(struct rcu_head *rcu)
 
236
{
 
237
        struct datapath *dp = container_of(rcu, struct datapath, rcu);
 
238
 
 
239
        ovs_flow_tbl_destroy((__force struct flow_table *)dp->table);
 
240
        free_percpu(dp->stats_percpu);
 
241
        kobject_put(&dp->ifobj);
 
242
}
 
243
 
 
244
/* Called with RTNL lock and genl_lock. */
 
245
static struct vport *new_vport(const struct vport_parms *parms)
 
246
{
 
247
        struct vport *vport;
 
248
 
 
249
        vport = ovs_vport_add(parms);
 
250
        if (!IS_ERR(vport)) {
 
251
                struct datapath *dp = parms->dp;
 
252
 
 
253
                rcu_assign_pointer(dp->ports[parms->port_no], vport);
 
254
                list_add(&vport->node, &dp->port_list);
 
255
 
 
256
                dp_ifinfo_notify(RTM_NEWLINK, vport);
 
257
        }
 
258
 
 
259
        return vport;
 
260
}
 
261
 
 
262
/* Called with RTNL lock. */
 
263
void ovs_dp_detach_port(struct vport *p)
 
264
{
 
265
        ASSERT_RTNL();
 
266
 
 
267
        if (p->port_no != OVSP_LOCAL)
 
268
                ovs_dp_sysfs_del_if(p);
 
269
        dp_ifinfo_notify(RTM_DELLINK, p);
 
270
 
 
271
        /* First drop references to device. */
 
272
        list_del(&p->node);
 
273
        rcu_assign_pointer(p->dp->ports[p->port_no], NULL);
 
274
 
 
275
        /* Then destroy it. */
 
276
        ovs_vport_del(p);
 
277
}
 
278
 
 
279
/* Must be called with rcu_read_lock. */
 
280
void ovs_dp_process_received_packet(struct vport *p, struct sk_buff *skb)
 
281
{
 
282
        struct datapath *dp = p->dp;
 
283
        struct sw_flow *flow;
 
284
        struct dp_stats_percpu *stats;
 
285
        u64 *stats_counter;
 
286
        int error;
 
287
 
 
288
        stats = per_cpu_ptr(dp->stats_percpu, smp_processor_id());
 
289
 
 
290
        if (!OVS_CB(skb)->flow) {
 
291
                struct sw_flow_key key;
 
292
                int key_len;
 
293
 
 
294
                /* Extract flow from 'skb' into 'key'. */
 
295
                error = ovs_flow_extract(skb, p->port_no, &key, &key_len);
 
296
                if (unlikely(error)) {
 
297
                        kfree_skb(skb);
 
298
                        return;
 
299
                }
 
300
 
 
301
                /* Look up flow. */
 
302
                flow = ovs_flow_tbl_lookup(rcu_dereference(dp->table),
 
303
                                           &key, key_len);
 
304
                if (unlikely(!flow)) {
 
305
                        struct dp_upcall_info upcall;
 
306
 
 
307
                        upcall.cmd = OVS_PACKET_CMD_MISS;
 
308
                        upcall.key = &key;
 
309
                        upcall.userdata = NULL;
 
310
                        upcall.pid = p->upcall_pid;
 
311
                        ovs_dp_upcall(dp, skb, &upcall);
 
312
                        consume_skb(skb);
 
313
                        stats_counter = &stats->n_missed;
 
314
                        goto out;
 
315
                }
 
316
 
 
317
                OVS_CB(skb)->flow = flow;
 
318
        }
 
319
 
 
320
        stats_counter = &stats->n_hit;
 
321
        ovs_flow_used(OVS_CB(skb)->flow, skb);
 
322
        ovs_execute_actions(dp, skb);
 
323
 
 
324
out:
 
325
        /* Update datapath statistics. */
 
326
        u64_stats_update_begin(&stats->sync);
 
327
        (*stats_counter)++;
 
328
        u64_stats_update_end(&stats->sync);
 
329
}
 
330
 
 
331
static struct genl_family dp_packet_genl_family = {
 
332
        .id = GENL_ID_GENERATE,
 
333
        .hdrsize = sizeof(struct ovs_header),
 
334
        .name = OVS_PACKET_FAMILY,
 
335
        .version = OVS_PACKET_VERSION,
 
336
        .maxattr = OVS_PACKET_ATTR_MAX
 
337
};
 
338
 
 
339
int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
 
340
                  const struct dp_upcall_info *upcall_info)
 
341
{
 
342
        struct dp_stats_percpu *stats;
 
343
        int dp_ifindex;
 
344
        int err;
 
345
 
 
346
        if (upcall_info->pid == 0) {
 
347
                err = -ENOTCONN;
 
348
                goto err;
 
349
        }
 
350
 
 
351
        dp_ifindex = get_dpifindex(dp);
 
352
        if (!dp_ifindex) {
 
353
                err = -ENODEV;
 
354
                goto err;
 
355
        }
 
356
 
 
357
        forward_ip_summed(skb, true);
 
358
 
 
359
        if (!skb_is_gso(skb))
 
360
                err = queue_userspace_packet(dp_ifindex, skb, upcall_info);
 
361
        else
 
362
                err = queue_gso_packets(dp_ifindex, skb, upcall_info);
 
363
        if (err)
 
364
                goto err;
 
365
 
 
366
        return 0;
 
367
 
 
368
err:
 
369
        stats = per_cpu_ptr(dp->stats_percpu, smp_processor_id());
 
370
 
 
371
        u64_stats_update_begin(&stats->sync);
 
372
        stats->n_lost++;
 
373
        u64_stats_update_end(&stats->sync);
 
374
 
 
375
        return err;
 
376
}
 
377
 
 
378
static int queue_gso_packets(int dp_ifindex, struct sk_buff *skb,
 
379
                             const struct dp_upcall_info *upcall_info)
 
380
{
 
381
        unsigned short gso_type = skb_shinfo(skb)->gso_type;
 
382
        struct dp_upcall_info later_info;
 
383
        struct sw_flow_key later_key;
 
384
        struct sk_buff *segs, *nskb;
 
385
        int err;
 
386
 
 
387
        segs = skb_gso_segment(skb, NETIF_F_SG | NETIF_F_HW_CSUM);
 
388
        if (IS_ERR(segs))
 
389
                return PTR_ERR(segs);
 
390
 
 
391
        /* Queue all of the segments. */
 
392
        skb = segs;
 
393
        do {
 
394
                err = queue_userspace_packet(dp_ifindex, skb, upcall_info);
 
395
                if (err)
 
396
                        break;
 
397
 
 
398
                if (skb == segs && gso_type & SKB_GSO_UDP) {
 
399
                        /* The initial flow key extracted by flow_extract() in
 
400
                         * this case is for a first fragment, so we need to
 
401
                         * properly mark later fragments.
 
402
                         */
 
403
                        later_key = *upcall_info->key;
 
404
                        later_key.ip.frag = OVS_FRAG_TYPE_LATER;
 
405
 
 
406
                        later_info = *upcall_info;
 
407
                        later_info.key = &later_key;
 
408
                        upcall_info = &later_info;
 
409
                }
 
410
        } while ((skb = skb->next));
 
411
 
 
412
        /* Free all of the segments. */
 
413
        skb = segs;
 
414
        do {
 
415
                nskb = skb->next;
 
416
                if (err)
 
417
                        kfree_skb(skb);
 
418
                else
 
419
                        consume_skb(skb);
 
420
        } while ((skb = nskb));
 
421
        return err;
 
422
}
 
423
 
 
424
static int queue_userspace_packet(int dp_ifindex, struct sk_buff *skb,
 
425
                                  const struct dp_upcall_info *upcall_info)
 
426
{
 
427
        struct ovs_header *upcall;
 
428
        struct sk_buff *nskb = NULL;
 
429
        struct sk_buff *user_skb; /* to be queued to userspace */
 
430
        struct nlattr *nla;
 
431
        unsigned int len;
 
432
        int err;
 
433
 
 
434
        if (vlan_tx_tag_present(skb)) {
 
435
                nskb = skb_clone(skb, GFP_ATOMIC);
 
436
                if (!nskb)
 
437
                        return -ENOMEM;
 
438
                
 
439
                err = vlan_deaccel_tag(nskb);
 
440
                if (err)
 
441
                        return err;
 
442
 
 
443
                skb = nskb;
 
444
        }
 
445
 
 
446
        if (nla_attr_size(skb->len) > USHRT_MAX) {
 
447
                err = -EFBIG;
 
448
                goto out;
 
449
        }
 
450
 
 
451
        len = sizeof(struct ovs_header);
 
452
        len += nla_total_size(skb->len);
 
453
        len += nla_total_size(FLOW_BUFSIZE);
 
454
        if (upcall_info->cmd == OVS_PACKET_CMD_ACTION)
 
455
                len += nla_total_size(8);
 
456
 
 
457
        user_skb = genlmsg_new(len, GFP_ATOMIC);
 
458
        if (!user_skb) {
 
459
                err = -ENOMEM;
 
460
                goto out;
 
461
        }
 
462
 
 
463
        upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
 
464
                             0, upcall_info->cmd);
 
465
        upcall->dp_ifindex = dp_ifindex;
 
466
 
 
467
        nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
 
468
        ovs_flow_to_nlattrs(upcall_info->key, user_skb);
 
469
        nla_nest_end(user_skb, nla);
 
470
 
 
471
        if (upcall_info->userdata)
 
472
                nla_put_u64(user_skb, OVS_PACKET_ATTR_USERDATA,
 
473
                            nla_get_u64(upcall_info->userdata));
 
474
 
 
475
        nla = __nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, skb->len);
 
476
 
 
477
        skb_copy_and_csum_dev(skb, nla_data(nla));
 
478
 
 
479
        err = genlmsg_unicast(&init_net, user_skb, upcall_info->pid);
 
480
 
 
481
out:
 
482
        kfree_skb(nskb);
 
483
        return err;
 
484
}
 
485
 
 
486
/* Called with genl_mutex. */
 
487
static int flush_flows(int dp_ifindex)
 
488
{
 
489
        struct flow_table *old_table;
 
490
        struct flow_table *new_table;
 
491
        struct datapath *dp;
 
492
 
 
493
        dp = get_dp(dp_ifindex);
 
494
        if (!dp)
 
495
                return -ENODEV;
 
496
 
 
497
        old_table = genl_dereference(dp->table);
 
498
        new_table = ovs_flow_tbl_alloc(TBL_MIN_BUCKETS);
 
499
        if (!new_table)
 
500
                return -ENOMEM;
 
501
 
 
502
        rcu_assign_pointer(dp->table, new_table);
 
503
 
 
504
        ovs_flow_tbl_deferred_destroy(old_table);
 
505
        return 0;
 
506
}
 
507
 
 
508
static int validate_actions(const struct nlattr *attr,
 
509
                                const struct sw_flow_key *key, int depth);
 
510
 
 
511
static int validate_sample(const struct nlattr *attr,
 
512
                                const struct sw_flow_key *key, int depth)
 
513
{
 
514
        const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
 
515
        const struct nlattr *probability, *actions;
 
516
        const struct nlattr *a;
 
517
        int rem;
 
518
 
 
519
        memset(attrs, 0, sizeof(attrs));
 
520
        nla_for_each_nested(a, attr, rem) {
 
521
                int type = nla_type(a);
 
522
                if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
 
523
                        return -EINVAL;
 
524
                attrs[type] = a;
 
525
        }
 
526
        if (rem)
 
527
                return -EINVAL;
 
528
 
 
529
        probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
 
530
        if (!probability || nla_len(probability) != sizeof(u32))
 
531
                return -EINVAL;
 
532
 
 
533
        actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
 
534
        if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
 
535
                return -EINVAL;
 
536
        return validate_actions(actions, key, depth + 1);
 
537
}
 
538
 
 
539
static int validate_tp_port(const struct sw_flow_key *flow_key)
 
540
{
 
541
        if (flow_key->eth.type == htons(ETH_P_IP)) {
 
542
                if (flow_key->ipv4.tp.src || flow_key->ipv4.tp.dst)
 
543
                        return 0;
 
544
        } else if (flow_key->eth.type == htons(ETH_P_IPV6)) {
 
545
                if (flow_key->ipv6.tp.src || flow_key->ipv6.tp.dst)
 
546
                        return 0;
 
547
        }
 
548
 
 
549
        return -EINVAL;
 
550
}
 
551
 
 
552
static int validate_set(const struct nlattr *a,
 
553
                        const struct sw_flow_key *flow_key)
 
554
{
 
555
        const struct nlattr *ovs_key = nla_data(a);
 
556
        int key_type = nla_type(ovs_key);
 
557
 
 
558
        /* There can be only one key in a action */
 
559
        if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
 
560
                return -EINVAL;
 
561
 
 
562
        if (key_type > OVS_KEY_ATTR_MAX ||
 
563
            nla_len(ovs_key) != ovs_key_lens[key_type])
 
564
                return -EINVAL;
 
565
 
 
566
        switch (key_type) {
 
567
        const struct ovs_key_ipv4 *ipv4_key;
 
568
 
 
569
        case OVS_KEY_ATTR_PRIORITY:
 
570
        case OVS_KEY_ATTR_TUN_ID:
 
571
        case OVS_KEY_ATTR_ETHERNET:
 
572
                break;
 
573
 
 
574
        case OVS_KEY_ATTR_IPV4:
 
575
                if (flow_key->eth.type != htons(ETH_P_IP))
 
576
                        return -EINVAL;
 
577
 
 
578
                if (!flow_key->ip.proto)
 
579
                        return -EINVAL;
 
580
 
 
581
                ipv4_key = nla_data(ovs_key);
 
582
                if (ipv4_key->ipv4_proto != flow_key->ip.proto)
 
583
                        return -EINVAL;
 
584
 
 
585
                if (ipv4_key->ipv4_frag != flow_key->ip.frag)
 
586
                        return -EINVAL;
 
587
 
 
588
                break;
 
589
 
 
590
        case OVS_KEY_ATTR_TCP:
 
591
                if (flow_key->ip.proto != IPPROTO_TCP)
 
592
                        return -EINVAL;
 
593
 
 
594
                return validate_tp_port(flow_key);
 
595
 
 
596
        case OVS_KEY_ATTR_UDP:
 
597
                if (flow_key->ip.proto != IPPROTO_UDP)
 
598
                        return -EINVAL;
 
599
 
 
600
                return validate_tp_port(flow_key);
 
601
 
 
602
        default:
 
603
                return -EINVAL;
 
604
        }
 
605
 
 
606
        return 0;
 
607
}
 
608
 
 
609
static int validate_userspace(const struct nlattr *attr)
 
610
{
 
611
        static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] =   {
 
612
                [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
 
613
                [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_U64 },
 
614
        };
 
615
        struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
 
616
        int error;
 
617
 
 
618
        error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
 
619
                                 attr, userspace_policy);
 
620
        if (error)
 
621
                return error;
 
622
 
 
623
        if (!a[OVS_USERSPACE_ATTR_PID] ||
 
624
            !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
 
625
                return -EINVAL;
 
626
 
 
627
        return 0;
 
628
}
 
629
 
 
630
static int validate_actions(const struct nlattr *attr,
 
631
                                const struct sw_flow_key *key,  int depth)
 
632
{
 
633
        const struct nlattr *a;
 
634
        int rem, err;
 
635
 
 
636
        if (depth >= SAMPLE_ACTION_DEPTH)
 
637
                return -EOVERFLOW;
 
638
 
 
639
        nla_for_each_nested(a, attr, rem) {
 
640
                /* Expected argument lengths, (u32)-1 for variable length. */
 
641
                static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
 
642
                        [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
 
643
                        [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
 
644
                        [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
 
645
                        [OVS_ACTION_ATTR_POP_VLAN] = 0,
 
646
                        [OVS_ACTION_ATTR_SET] = (u32)-1,
 
647
                        [OVS_ACTION_ATTR_SAMPLE] = (u32)-1
 
648
                };
 
649
                const struct ovs_action_push_vlan *vlan;
 
650
                int type = nla_type(a);
 
651
 
 
652
                if (type > OVS_ACTION_ATTR_MAX ||
 
653
                    (action_lens[type] != nla_len(a) &&
 
654
                     action_lens[type] != (u32)-1))
 
655
                        return -EINVAL;
 
656
 
 
657
                switch (type) {
 
658
                case OVS_ACTION_ATTR_UNSPEC:
 
659
                        return -EINVAL;
 
660
 
 
661
                case OVS_ACTION_ATTR_USERSPACE:
 
662
                        err = validate_userspace(a);
 
663
                        if (err)
 
664
                                return err;
 
665
                        break;
 
666
 
 
667
                case OVS_ACTION_ATTR_OUTPUT:
 
668
                        if (nla_get_u32(a) >= DP_MAX_PORTS)
 
669
                                return -EINVAL;
 
670
                        break;
 
671
 
 
672
 
 
673
                case OVS_ACTION_ATTR_POP_VLAN:
 
674
                        break;
 
675
 
 
676
                case OVS_ACTION_ATTR_PUSH_VLAN:
 
677
                        vlan = nla_data(a);
 
678
                        if (vlan->vlan_tpid != htons(ETH_P_8021Q))
 
679
                                return -EINVAL;
 
680
                        if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
 
681
                                return -EINVAL;
 
682
                        break;
 
683
 
 
684
                case OVS_ACTION_ATTR_SET:
 
685
                        err = validate_set(a, key);
 
686
                        if (err)
 
687
                                return err;
 
688
                        break;
 
689
 
 
690
                case OVS_ACTION_ATTR_SAMPLE:
 
691
                        err = validate_sample(a, key, depth);
 
692
                        if (err)
 
693
                                return err;
 
694
                        break;
 
695
 
 
696
                default:
 
697
                        return -EINVAL;
 
698
                }
 
699
        }
 
700
 
 
701
        if (rem > 0)
 
702
                return -EINVAL;
 
703
 
 
704
        return 0;
 
705
}
 
706
 
 
707
static void clear_stats(struct sw_flow *flow)
 
708
{
 
709
        flow->used = 0;
 
710
        flow->tcp_flags = 0;
 
711
        flow->packet_count = 0;
 
712
        flow->byte_count = 0;
 
713
}
 
714
 
 
715
static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
 
716
{
 
717
        struct ovs_header *ovs_header = info->userhdr;
 
718
        struct nlattr **a = info->attrs;
 
719
        struct sw_flow_actions *acts;
 
720
        struct sk_buff *packet;
 
721
        struct sw_flow *flow;
 
722
        struct datapath *dp;
 
723
        struct ethhdr *eth;
 
724
        int len;
 
725
        int err;
 
726
        int key_len;
 
727
 
 
728
        err = -EINVAL;
 
729
        if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
 
730
            !a[OVS_PACKET_ATTR_ACTIONS] ||
 
731
            nla_len(a[OVS_PACKET_ATTR_PACKET]) < ETH_HLEN)
 
732
                goto err;
 
733
 
 
734
        len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
 
735
        packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
 
736
        err = -ENOMEM;
 
737
        if (!packet)
 
738
                goto err;
 
739
        skb_reserve(packet, NET_IP_ALIGN);
 
740
 
 
741
        memcpy(__skb_put(packet, len), nla_data(a[OVS_PACKET_ATTR_PACKET]), len);
 
742
 
 
743
        skb_reset_mac_header(packet);
 
744
        eth = eth_hdr(packet);
 
745
 
 
746
        /* Normally, setting the skb 'protocol' field would be handled by a
 
747
         * call to eth_type_trans(), but it assumes there's a sending
 
748
         * device, which we may not have. */
 
749
        if (ntohs(eth->h_proto) >= 1536)
 
750
                packet->protocol = eth->h_proto;
 
751
        else
 
752
                packet->protocol = htons(ETH_P_802_2);
 
753
 
 
754
        /* Build an sw_flow for sending this packet. */
 
755
        flow = ovs_flow_alloc();
 
756
        err = PTR_ERR(flow);
 
757
        if (IS_ERR(flow))
 
758
                goto err_kfree_skb;
 
759
 
 
760
        err = ovs_flow_extract(packet, -1, &flow->key, &key_len);
 
761
        if (err)
 
762
                goto err_flow_put;
 
763
 
 
764
        err = ovs_flow_metadata_from_nlattrs(&flow->key.phy.priority,
 
765
                                             &flow->key.phy.in_port,
 
766
                                             &flow->key.phy.tun_id,
 
767
                                             a[OVS_PACKET_ATTR_KEY]);
 
768
        if (err)
 
769
                goto err_flow_put;
 
770
 
 
771
        err = validate_actions(a[OVS_PACKET_ATTR_ACTIONS], &flow->key, 0);
 
772
        if (err)
 
773
                goto err_flow_put;
 
774
 
 
775
        flow->hash = ovs_flow_hash(&flow->key, key_len);
 
776
 
 
777
        acts = ovs_flow_actions_alloc(a[OVS_PACKET_ATTR_ACTIONS]);
 
778
        err = PTR_ERR(acts);
 
779
        if (IS_ERR(acts))
 
780
                goto err_flow_put;
 
781
        rcu_assign_pointer(flow->sf_acts, acts);
 
782
 
 
783
        OVS_CB(packet)->flow = flow;
 
784
        packet->priority = flow->key.phy.priority;
 
785
 
 
786
        rcu_read_lock();
 
787
        dp = get_dp(ovs_header->dp_ifindex);
 
788
        err = -ENODEV;
 
789
        if (!dp)
 
790
                goto err_unlock;
 
791
 
 
792
        local_bh_disable();
 
793
        err = ovs_execute_actions(dp, packet);
 
794
        local_bh_enable();
 
795
        rcu_read_unlock();
 
796
 
 
797
        ovs_flow_put(flow);
 
798
        return err;
 
799
 
 
800
err_unlock:
 
801
        rcu_read_unlock();
 
802
err_flow_put:
 
803
        ovs_flow_put(flow);
 
804
err_kfree_skb:
 
805
        kfree_skb(packet);
 
806
err:
 
807
        return err;
 
808
}
 
809
 
 
810
static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
 
811
        [OVS_PACKET_ATTR_PACKET] = { .type = NLA_UNSPEC },
 
812
        [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
 
813
        [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
 
814
};
 
815
 
 
816
static struct genl_ops dp_packet_genl_ops[] = {
 
817
        { .cmd = OVS_PACKET_CMD_EXECUTE,
 
818
          .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
 
819
          .policy = packet_policy,
 
820
          .doit = ovs_packet_cmd_execute
 
821
        }
 
822
};
 
823
 
 
824
static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats)
 
825
{
 
826
        int i;
 
827
        struct flow_table *table = genl_dereference(dp->table);
 
828
 
 
829
        stats->n_flows = ovs_flow_tbl_count(table);
 
830
 
 
831
        stats->n_hit = stats->n_missed = stats->n_lost = 0;
 
832
        for_each_possible_cpu(i) {
 
833
                const struct dp_stats_percpu *percpu_stats;
 
834
                struct dp_stats_percpu local_stats;
 
835
                unsigned int start;
 
836
 
 
837
                percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
 
838
 
 
839
                do {
 
840
                        start = u64_stats_fetch_begin_bh(&percpu_stats->sync);
 
841
                        local_stats = *percpu_stats;
 
842
                } while (u64_stats_fetch_retry_bh(&percpu_stats->sync, start));
 
843
 
 
844
                stats->n_hit += local_stats.n_hit;
 
845
                stats->n_missed += local_stats.n_missed;
 
846
                stats->n_lost += local_stats.n_lost;
 
847
        }
 
848
}
 
849
 
 
850
static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
 
851
        [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
 
852
        [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
 
853
        [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
 
854
};
 
855
 
 
856
static struct genl_family dp_flow_genl_family = {
 
857
        .id = GENL_ID_GENERATE,
 
858
        .hdrsize = sizeof(struct ovs_header),
 
859
        .name = OVS_FLOW_FAMILY,
 
860
        .version = OVS_FLOW_VERSION,
 
861
        .maxattr = OVS_FLOW_ATTR_MAX
 
862
};
 
863
 
 
864
static struct genl_multicast_group ovs_dp_flow_multicast_group = {
 
865
        .name = OVS_FLOW_MCGROUP
 
866
};
 
867
 
 
868
/* Called with genl_lock. */
 
869
static int ovs_flow_cmd_fill_info(struct sw_flow *flow, struct datapath *dp,
 
870
                                  struct sk_buff *skb, u32 pid,
 
871
                                  u32 seq, u32 flags, u8 cmd)
 
872
{
 
873
        const int skb_orig_len = skb->len;
 
874
        const struct sw_flow_actions *sf_acts;
 
875
        struct ovs_flow_stats stats;
 
876
        struct ovs_header *ovs_header;
 
877
        struct nlattr *nla;
 
878
        unsigned long used;
 
879
        u8 tcp_flags;
 
880
        int err;
 
881
 
 
882
        sf_acts = rcu_dereference_protected(flow->sf_acts,
 
883
                                            lockdep_genl_is_held());
 
884
 
 
885
        ovs_header = genlmsg_put(skb, pid, seq, &dp_flow_genl_family, flags, cmd);
 
886
        if (!ovs_header)
 
887
                return -EMSGSIZE;
 
888
 
 
889
        ovs_header->dp_ifindex = get_dpifindex(dp);
 
890
 
 
891
        nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
 
892
        if (!nla)
 
893
                goto nla_put_failure;
 
894
        err = ovs_flow_to_nlattrs(&flow->key, skb);
 
895
        if (err)
 
896
                goto error;
 
897
        nla_nest_end(skb, nla);
 
898
 
 
899
        spin_lock_bh(&flow->lock);
 
900
        used = flow->used;
 
901
        stats.n_packets = flow->packet_count;
 
902
        stats.n_bytes = flow->byte_count;
 
903
        tcp_flags = flow->tcp_flags;
 
904
        spin_unlock_bh(&flow->lock);
 
905
 
 
906
        if (used &&
 
907
            nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
 
908
                goto nla_put_failure;
 
909
 
 
910
        if (stats.n_packets &&
 
911
            nla_put(skb, OVS_FLOW_ATTR_STATS,
 
912
                    sizeof(struct ovs_flow_stats), &stats))
 
913
                goto nla_put_failure;
 
914
 
 
915
        if (tcp_flags &&
 
916
            nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, tcp_flags))
 
917
                goto nla_put_failure;
 
918
 
 
919
        /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
 
920
         * this is the first flow to be dumped into 'skb'.  This is unusual for
 
921
         * Netlink but individual action lists can be longer than
 
922
         * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
 
923
         * The userspace caller can always fetch the actions separately if it
 
924
         * really wants them.  (Most userspace callers in fact don't care.)
 
925
         *
 
926
         * This can only fail for dump operations because the skb is always
 
927
         * properly sized for single flows.
 
928
         */
 
929
        err = nla_put(skb, OVS_FLOW_ATTR_ACTIONS, sf_acts->actions_len,
 
930
                      sf_acts->actions);
 
931
        if (err < 0 && skb_orig_len)
 
932
                goto error;
 
933
 
 
934
        return genlmsg_end(skb, ovs_header);
 
935
 
 
936
nla_put_failure:
 
937
        err = -EMSGSIZE;
 
938
error:
 
939
        genlmsg_cancel(skb, ovs_header);
 
940
        return err;
 
941
}
 
942
 
 
943
static struct sk_buff *ovs_flow_cmd_alloc_info(struct sw_flow *flow)
 
944
{
 
945
        const struct sw_flow_actions *sf_acts;
 
946
        int len;
 
947
 
 
948
        sf_acts = rcu_dereference_protected(flow->sf_acts,
 
949
                                            lockdep_genl_is_held());
 
950
 
 
951
        /* OVS_FLOW_ATTR_KEY */
 
952
        len = nla_total_size(FLOW_BUFSIZE);
 
953
        /* OVS_FLOW_ATTR_ACTIONS */
 
954
        len += nla_total_size(sf_acts->actions_len);
 
955
        /* OVS_FLOW_ATTR_STATS */
 
956
        len += nla_total_size(sizeof(struct ovs_flow_stats));
 
957
        /* OVS_FLOW_ATTR_TCP_FLAGS */
 
958
        len += nla_total_size(1);
 
959
        /* OVS_FLOW_ATTR_USED */
 
960
        len += nla_total_size(8);
 
961
 
 
962
        len += NLMSG_ALIGN(sizeof(struct ovs_header));
 
963
 
 
964
        return genlmsg_new(len, GFP_KERNEL);
 
965
}
 
966
 
 
967
static struct sk_buff *ovs_flow_cmd_build_info(struct sw_flow *flow,
 
968
                                               struct datapath *dp,
 
969
                                               u32 pid, u32 seq, u8 cmd)
 
970
{
 
971
        struct sk_buff *skb;
 
972
        int retval;
 
973
 
 
974
        skb = ovs_flow_cmd_alloc_info(flow);
 
975
        if (!skb)
 
976
                return ERR_PTR(-ENOMEM);
 
977
 
 
978
        retval = ovs_flow_cmd_fill_info(flow, dp, skb, pid, seq, 0, cmd);
 
979
        BUG_ON(retval < 0);
 
980
        return skb;
 
981
}
 
982
 
 
983
static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info)
 
984
{
 
985
        struct nlattr **a = info->attrs;
 
986
        struct ovs_header *ovs_header = info->userhdr;
 
987
        struct sw_flow_key key;
 
988
        struct sw_flow *flow;
 
989
        struct sk_buff *reply;
 
990
        struct datapath *dp;
 
991
        struct flow_table *table;
 
992
        int error;
 
993
        int key_len;
 
994
 
 
995
        /* Extract key. */
 
996
        error = -EINVAL;
 
997
        if (!a[OVS_FLOW_ATTR_KEY])
 
998
                goto error;
 
999
        error = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
 
1000
        if (error)
 
1001
                goto error;
 
1002
 
 
1003
        /* Validate actions. */
 
1004
        if (a[OVS_FLOW_ATTR_ACTIONS]) {
 
1005
                error = validate_actions(a[OVS_FLOW_ATTR_ACTIONS], &key,  0);
 
1006
                if (error)
 
1007
                        goto error;
 
1008
        } else if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW) {
 
1009
                error = -EINVAL;
 
1010
                goto error;
 
1011
        }
 
1012
 
 
1013
        dp = get_dp(ovs_header->dp_ifindex);
 
1014
        error = -ENODEV;
 
1015
        if (!dp)
 
1016
                goto error;
 
1017
 
 
1018
        table = genl_dereference(dp->table);
 
1019
        flow = ovs_flow_tbl_lookup(table, &key, key_len);
 
1020
        if (!flow) {
 
1021
                struct sw_flow_actions *acts;
 
1022
 
 
1023
                /* Bail out if we're not allowed to create a new flow. */
 
1024
                error = -ENOENT;
 
1025
                if (info->genlhdr->cmd == OVS_FLOW_CMD_SET)
 
1026
                        goto error;
 
1027
 
 
1028
                /* Expand table, if necessary, to make room. */
 
1029
                if (ovs_flow_tbl_need_to_expand(table)) {
 
1030
                        struct flow_table *new_table;
 
1031
 
 
1032
                        new_table = ovs_flow_tbl_expand(table);
 
1033
                        if (!IS_ERR(new_table)) {
 
1034
                                rcu_assign_pointer(dp->table, new_table);
 
1035
                                ovs_flow_tbl_deferred_destroy(table);
 
1036
                                table = genl_dereference(dp->table);
 
1037
                        }
 
1038
                }
 
1039
 
 
1040
                /* Allocate flow. */
 
1041
                flow = ovs_flow_alloc();
 
1042
                if (IS_ERR(flow)) {
 
1043
                        error = PTR_ERR(flow);
 
1044
                        goto error;
 
1045
                }
 
1046
                flow->key = key;
 
1047
                clear_stats(flow);
 
1048
 
 
1049
                /* Obtain actions. */
 
1050
                acts = ovs_flow_actions_alloc(a[OVS_FLOW_ATTR_ACTIONS]);
 
1051
                error = PTR_ERR(acts);
 
1052
                if (IS_ERR(acts))
 
1053
                        goto error_free_flow;
 
1054
                rcu_assign_pointer(flow->sf_acts, acts);
 
1055
 
 
1056
                /* Put flow in bucket. */
 
1057
                flow->hash = ovs_flow_hash(&key, key_len);
 
1058
                ovs_flow_tbl_insert(table, flow);
 
1059
 
 
1060
                reply = ovs_flow_cmd_build_info(flow, dp, info->snd_pid,
 
1061
                                                info->snd_seq,
 
1062
                                                OVS_FLOW_CMD_NEW);
 
1063
        } else {
 
1064
                /* We found a matching flow. */
 
1065
                struct sw_flow_actions *old_acts;
 
1066
                struct nlattr *acts_attrs;
 
1067
 
 
1068
                /* Bail out if we're not allowed to modify an existing flow.
 
1069
                 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
 
1070
                 * because Generic Netlink treats the latter as a dump
 
1071
                 * request.  We also accept NLM_F_EXCL in case that bug ever
 
1072
                 * gets fixed.
 
1073
                 */
 
1074
                error = -EEXIST;
 
1075
                if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW &&
 
1076
                    info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL))
 
1077
                        goto error;
 
1078
 
 
1079
                /* Update actions. */
 
1080
                old_acts = rcu_dereference_protected(flow->sf_acts,
 
1081
                                                     lockdep_genl_is_held());
 
1082
                acts_attrs = a[OVS_FLOW_ATTR_ACTIONS];
 
1083
                if (acts_attrs &&
 
1084
                   (old_acts->actions_len != nla_len(acts_attrs) ||
 
1085
                   memcmp(old_acts->actions, nla_data(acts_attrs),
 
1086
                          old_acts->actions_len))) {
 
1087
                        struct sw_flow_actions *new_acts;
 
1088
 
 
1089
                        new_acts = ovs_flow_actions_alloc(acts_attrs);
 
1090
                        error = PTR_ERR(new_acts);
 
1091
                        if (IS_ERR(new_acts))
 
1092
                                goto error;
 
1093
 
 
1094
                        rcu_assign_pointer(flow->sf_acts, new_acts);
 
1095
                        ovs_flow_deferred_free_acts(old_acts);
 
1096
                }
 
1097
 
 
1098
                reply = ovs_flow_cmd_build_info(flow, dp, info->snd_pid,
 
1099
                                               info->snd_seq, OVS_FLOW_CMD_NEW);
 
1100
 
 
1101
                /* Clear stats. */
 
1102
                if (a[OVS_FLOW_ATTR_CLEAR]) {
 
1103
                        spin_lock_bh(&flow->lock);
 
1104
                        clear_stats(flow);
 
1105
                        spin_unlock_bh(&flow->lock);
 
1106
                }
 
1107
        }
 
1108
 
 
1109
        if (!IS_ERR(reply))
 
1110
                genl_notify(reply, genl_info_net(info), info->snd_pid,
 
1111
                           ovs_dp_flow_multicast_group.id, info->nlhdr,
 
1112
                           GFP_KERNEL);
 
1113
        else
 
1114
                netlink_set_err(INIT_NET_GENL_SOCK, 0,
 
1115
                                ovs_dp_flow_multicast_group.id,
 
1116
                                PTR_ERR(reply));
 
1117
        return 0;
 
1118
 
 
1119
error_free_flow:
 
1120
        ovs_flow_put(flow);
 
1121
error:
 
1122
        return error;
 
1123
}
 
1124
 
 
1125
static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
 
1126
{
 
1127
        struct nlattr **a = info->attrs;
 
1128
        struct ovs_header *ovs_header = info->userhdr;
 
1129
        struct sw_flow_key key;
 
1130
        struct sk_buff *reply;
 
1131
        struct sw_flow *flow;
 
1132
        struct datapath *dp;
 
1133
        struct flow_table *table;
 
1134
        int err;
 
1135
        int key_len;
 
1136
 
 
1137
        if (!a[OVS_FLOW_ATTR_KEY])
 
1138
                return -EINVAL;
 
1139
        err = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
 
1140
        if (err)
 
1141
                return err;
 
1142
 
 
1143
        dp = get_dp(ovs_header->dp_ifindex);
 
1144
        if (!dp)
 
1145
                return -ENODEV;
 
1146
 
 
1147
        table = genl_dereference(dp->table);
 
1148
        flow = ovs_flow_tbl_lookup(table, &key, key_len);
 
1149
        if (!flow)
 
1150
                return -ENOENT;
 
1151
 
 
1152
        reply = ovs_flow_cmd_build_info(flow, dp, info->snd_pid,
 
1153
                                        info->snd_seq, OVS_FLOW_CMD_NEW);
 
1154
        if (IS_ERR(reply))
 
1155
                return PTR_ERR(reply);
 
1156
 
 
1157
        return genlmsg_reply(reply, info);
 
1158
}
 
1159
 
 
1160
static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
 
1161
{
 
1162
        struct nlattr **a = info->attrs;
 
1163
        struct ovs_header *ovs_header = info->userhdr;
 
1164
        struct sw_flow_key key;
 
1165
        struct sk_buff *reply;
 
1166
        struct sw_flow *flow;
 
1167
        struct datapath *dp;
 
1168
        struct flow_table *table;
 
1169
        int err;
 
1170
        int key_len;
 
1171
 
 
1172
        if (!a[OVS_FLOW_ATTR_KEY])
 
1173
                return flush_flows(ovs_header->dp_ifindex);
 
1174
        err = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
 
1175
        if (err)
 
1176
                return err;
 
1177
 
 
1178
        dp = get_dp(ovs_header->dp_ifindex);
 
1179
        if (!dp)
 
1180
                return -ENODEV;
 
1181
 
 
1182
        table = genl_dereference(dp->table);
 
1183
        flow = ovs_flow_tbl_lookup(table, &key, key_len);
 
1184
        if (!flow)
 
1185
                return -ENOENT;
 
1186
 
 
1187
        reply = ovs_flow_cmd_alloc_info(flow);
 
1188
        if (!reply)
 
1189
                return -ENOMEM;
 
1190
 
 
1191
        ovs_flow_tbl_remove(table, flow);
 
1192
 
 
1193
        err = ovs_flow_cmd_fill_info(flow, dp, reply, info->snd_pid,
 
1194
                                     info->snd_seq, 0, OVS_FLOW_CMD_DEL);
 
1195
        BUG_ON(err < 0);
 
1196
 
 
1197
        ovs_flow_deferred_free(flow);
 
1198
 
 
1199
        genl_notify(reply, genl_info_net(info), info->snd_pid,
 
1200
                    ovs_dp_flow_multicast_group.id, info->nlhdr, GFP_KERNEL);
 
1201
        return 0;
 
1202
}
 
1203
 
 
1204
static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
 
1205
{
 
1206
        struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
 
1207
        struct datapath *dp;
 
1208
        struct flow_table *table;
 
1209
 
 
1210
        dp = get_dp(ovs_header->dp_ifindex);
 
1211
        if (!dp)
 
1212
                return -ENODEV;
 
1213
 
 
1214
        table = genl_dereference(dp->table);
 
1215
 
 
1216
        for (;;) {
 
1217
                struct sw_flow *flow;
 
1218
                u32 bucket, obj;
 
1219
 
 
1220
                bucket = cb->args[0];
 
1221
                obj = cb->args[1];
 
1222
                flow = ovs_flow_tbl_next(table, &bucket, &obj);
 
1223
                if (!flow)
 
1224
                        break;
 
1225
 
 
1226
                if (ovs_flow_cmd_fill_info(flow, dp, skb,
 
1227
                                           NETLINK_CB(cb->skb).pid,
 
1228
                                           cb->nlh->nlmsg_seq, NLM_F_MULTI,
 
1229
                                           OVS_FLOW_CMD_NEW) < 0)
 
1230
                        break;
 
1231
 
 
1232
                cb->args[0] = bucket;
 
1233
                cb->args[1] = obj;
 
1234
        }
 
1235
        return skb->len;
 
1236
}
 
1237
 
 
1238
static struct genl_ops dp_flow_genl_ops[] = {
 
1239
        { .cmd = OVS_FLOW_CMD_NEW,
 
1240
          .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
 
1241
          .policy = flow_policy,
 
1242
          .doit = ovs_flow_cmd_new_or_set
 
1243
        },
 
1244
        { .cmd = OVS_FLOW_CMD_DEL,
 
1245
          .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
 
1246
          .policy = flow_policy,
 
1247
          .doit = ovs_flow_cmd_del
 
1248
        },
 
1249
        { .cmd = OVS_FLOW_CMD_GET,
 
1250
          .flags = 0,               /* OK for unprivileged users. */
 
1251
          .policy = flow_policy,
 
1252
          .doit = ovs_flow_cmd_get,
 
1253
          .dumpit = ovs_flow_cmd_dump
 
1254
        },
 
1255
        { .cmd = OVS_FLOW_CMD_SET,
 
1256
          .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
 
1257
          .policy = flow_policy,
 
1258
          .doit = ovs_flow_cmd_new_or_set,
 
1259
        },
 
1260
};
 
1261
 
 
1262
static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
 
1263
#ifdef HAVE_NLA_NUL_STRING
 
1264
        [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
 
1265
#endif
 
1266
        [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
 
1267
};
 
1268
 
 
1269
static struct genl_family dp_datapath_genl_family = {
 
1270
        .id = GENL_ID_GENERATE,
 
1271
        .hdrsize = sizeof(struct ovs_header),
 
1272
        .name = OVS_DATAPATH_FAMILY,
 
1273
        .version = OVS_DATAPATH_VERSION,
 
1274
        .maxattr = OVS_DP_ATTR_MAX
 
1275
};
 
1276
 
 
1277
static struct genl_multicast_group ovs_dp_datapath_multicast_group = {
 
1278
        .name = OVS_DATAPATH_MCGROUP
 
1279
};
 
1280
 
 
1281
static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
 
1282
                                u32 pid, u32 seq, u32 flags, u8 cmd)
 
1283
{
 
1284
        struct ovs_header *ovs_header;
 
1285
        struct ovs_dp_stats dp_stats;
 
1286
        int err;
 
1287
 
 
1288
        ovs_header = genlmsg_put(skb, pid, seq, &dp_datapath_genl_family,
 
1289
                                   flags, cmd);
 
1290
        if (!ovs_header)
 
1291
                goto error;
 
1292
 
 
1293
        ovs_header->dp_ifindex = get_dpifindex(dp);
 
1294
 
 
1295
        rcu_read_lock();
 
1296
        err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
 
1297
        rcu_read_unlock();
 
1298
        if (err)
 
1299
                goto nla_put_failure;
 
1300
 
 
1301
        get_dp_stats(dp, &dp_stats);
 
1302
        if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats), &dp_stats))
 
1303
                goto nla_put_failure;
 
1304
 
 
1305
        return genlmsg_end(skb, ovs_header);
 
1306
 
 
1307
nla_put_failure:
 
1308
        genlmsg_cancel(skb, ovs_header);
 
1309
error:
 
1310
        return -EMSGSIZE;
 
1311
}
 
1312
 
 
1313
static struct sk_buff *ovs_dp_cmd_build_info(struct datapath *dp, u32 pid,
 
1314
                                             u32 seq, u8 cmd)
 
1315
{
 
1316
        struct sk_buff *skb;
 
1317
        int retval;
 
1318
 
 
1319
        skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
 
1320
        if (!skb)
 
1321
                return ERR_PTR(-ENOMEM);
 
1322
 
 
1323
        retval = ovs_dp_cmd_fill_info(dp, skb, pid, seq, 0, cmd);
 
1324
        if (retval < 0) {
 
1325
                kfree_skb(skb);
 
1326
                return ERR_PTR(retval);
 
1327
        }
 
1328
        return skb;
 
1329
}
 
1330
 
 
1331
static int ovs_dp_cmd_validate(struct nlattr *a[OVS_DP_ATTR_MAX + 1])
 
1332
{
 
1333
        return CHECK_NUL_STRING(a[OVS_DP_ATTR_NAME], IFNAMSIZ - 1);
 
1334
}
 
1335
 
 
1336
/* Called with genl_mutex and optionally with RTNL lock also. */
 
1337
static struct datapath *lookup_datapath(struct ovs_header *ovs_header,
 
1338
                                        struct nlattr *a[OVS_DP_ATTR_MAX + 1])
 
1339
{
 
1340
        struct datapath *dp;
 
1341
 
 
1342
        if (!a[OVS_DP_ATTR_NAME])
 
1343
                dp = get_dp(ovs_header->dp_ifindex);
 
1344
        else {
 
1345
                struct vport *vport;
 
1346
 
 
1347
                rcu_read_lock();
 
1348
                vport = ovs_vport_locate(nla_data(a[OVS_DP_ATTR_NAME]));
 
1349
                dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
 
1350
                rcu_read_unlock();
 
1351
        }
 
1352
        return dp ? dp : ERR_PTR(-ENODEV);
 
1353
}
 
1354
 
 
1355
static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
 
1356
{
 
1357
        struct nlattr **a = info->attrs;
 
1358
        struct vport_parms parms;
 
1359
        struct sk_buff *reply;
 
1360
        struct datapath *dp;
 
1361
        struct vport *vport;
 
1362
        int err;
 
1363
 
 
1364
        err = -EINVAL;
 
1365
        if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
 
1366
                goto err;
 
1367
 
 
1368
        err = ovs_dp_cmd_validate(a);
 
1369
        if (err)
 
1370
                goto err;
 
1371
 
 
1372
        rtnl_lock();
 
1373
        err = -ENODEV;
 
1374
        if (!try_module_get(THIS_MODULE))
 
1375
                goto err_unlock_rtnl;
 
1376
 
 
1377
        err = -ENOMEM;
 
1378
        dp = kzalloc(sizeof(*dp), GFP_KERNEL);
 
1379
        if (dp == NULL)
 
1380
                goto err_put_module;
 
1381
        INIT_LIST_HEAD(&dp->port_list);
 
1382
 
 
1383
        /* Initialize kobject for bridge.  This will be added as
 
1384
         * /sys/class/net/<devname>/brif later, if sysfs is enabled. */
 
1385
        dp->ifobj.kset = NULL;
 
1386
        kobject_init(&dp->ifobj, &dp_ktype);
 
1387
 
 
1388
        /* Allocate table. */
 
1389
        err = -ENOMEM;
 
1390
        rcu_assign_pointer(dp->table, ovs_flow_tbl_alloc(TBL_MIN_BUCKETS));
 
1391
        if (!dp->table)
 
1392
                goto err_free_dp;
 
1393
 
 
1394
        dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
 
1395
        if (!dp->stats_percpu) {
 
1396
                err = -ENOMEM;
 
1397
                goto err_destroy_table;
 
1398
        }
 
1399
 
 
1400
        /* Set up our datapath device. */
 
1401
        parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
 
1402
        parms.type = OVS_VPORT_TYPE_INTERNAL;
 
1403
        parms.options = NULL;
 
1404
        parms.dp = dp;
 
1405
        parms.port_no = OVSP_LOCAL;
 
1406
        parms.upcall_pid = nla_get_u32(a[OVS_DP_ATTR_UPCALL_PID]);
 
1407
 
 
1408
        vport = new_vport(&parms);
 
1409
        if (IS_ERR(vport)) {
 
1410
                err = PTR_ERR(vport);
 
1411
                if (err == -EBUSY)
 
1412
                        err = -EEXIST;
 
1413
 
 
1414
                goto err_destroy_percpu;
 
1415
        }
 
1416
 
 
1417
        reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
 
1418
                                      info->snd_seq, OVS_DP_CMD_NEW);
 
1419
        err = PTR_ERR(reply);
 
1420
        if (IS_ERR(reply))
 
1421
                goto err_destroy_local_port;
 
1422
 
 
1423
        list_add_tail(&dp->list_node, &dps);
 
1424
        ovs_dp_sysfs_add_dp(dp);
 
1425
 
 
1426
        rtnl_unlock();
 
1427
 
 
1428
        genl_notify(reply, genl_info_net(info), info->snd_pid,
 
1429
                    ovs_dp_datapath_multicast_group.id, info->nlhdr,
 
1430
                    GFP_KERNEL);
 
1431
        return 0;
 
1432
 
 
1433
err_destroy_local_port:
 
1434
        ovs_dp_detach_port(rtnl_dereference(dp->ports[OVSP_LOCAL]));
 
1435
err_destroy_percpu:
 
1436
        free_percpu(dp->stats_percpu);
 
1437
err_destroy_table:
 
1438
        ovs_flow_tbl_destroy(genl_dereference(dp->table));
 
1439
err_free_dp:
 
1440
        kfree(dp);
 
1441
err_put_module:
 
1442
        module_put(THIS_MODULE);
 
1443
err_unlock_rtnl:
 
1444
        rtnl_unlock();
 
1445
err:
 
1446
        return err;
 
1447
}
 
1448
 
 
1449
static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
 
1450
{
 
1451
        struct vport *vport, *next_vport;
 
1452
        struct sk_buff *reply;
 
1453
        struct datapath *dp;
 
1454
        int err;
 
1455
 
 
1456
        err = ovs_dp_cmd_validate(info->attrs);
 
1457
        if (err)
 
1458
                goto exit;
 
1459
 
 
1460
        rtnl_lock();
 
1461
        dp = lookup_datapath(info->userhdr, info->attrs);
 
1462
        err = PTR_ERR(dp);
 
1463
        if (IS_ERR(dp))
 
1464
                goto exit_unlock;
 
1465
 
 
1466
        reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
 
1467
                                      info->snd_seq, OVS_DP_CMD_DEL);
 
1468
        err = PTR_ERR(reply);
 
1469
        if (IS_ERR(reply))
 
1470
                goto exit_unlock;
 
1471
 
 
1472
        list_for_each_entry_safe(vport, next_vport, &dp->port_list, node)
 
1473
                if (vport->port_no != OVSP_LOCAL)
 
1474
                        ovs_dp_detach_port(vport);
 
1475
 
 
1476
        ovs_dp_sysfs_del_dp(dp);
 
1477
        list_del(&dp->list_node);
 
1478
        ovs_dp_detach_port(rtnl_dereference(dp->ports[OVSP_LOCAL]));
 
1479
 
 
1480
        /* rtnl_unlock() will wait until all the references to devices that
 
1481
         * are pending unregistration have been dropped.  We do it here to
 
1482
         * ensure that any internal devices (which contain DP pointers) are
 
1483
         * fully destroyed before freeing the datapath.
 
1484
         */
 
1485
        rtnl_unlock();
 
1486
 
 
1487
        call_rcu(&dp->rcu, destroy_dp_rcu);
 
1488
        module_put(THIS_MODULE);
 
1489
 
 
1490
        genl_notify(reply, genl_info_net(info), info->snd_pid,
 
1491
                    ovs_dp_datapath_multicast_group.id, info->nlhdr,
 
1492
                    GFP_KERNEL);
 
1493
 
 
1494
        return 0;
 
1495
 
 
1496
exit_unlock:
 
1497
        rtnl_unlock();
 
1498
exit:
 
1499
        return err;
 
1500
}
 
1501
 
 
1502
static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
 
1503
{
 
1504
        struct sk_buff *reply;
 
1505
        struct datapath *dp;
 
1506
        int err;
 
1507
 
 
1508
        err = ovs_dp_cmd_validate(info->attrs);
 
1509
        if (err)
 
1510
                return err;
 
1511
 
 
1512
        dp = lookup_datapath(info->userhdr, info->attrs);
 
1513
        if (IS_ERR(dp))
 
1514
                return PTR_ERR(dp);
 
1515
 
 
1516
        reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
 
1517
                                      info->snd_seq, OVS_DP_CMD_NEW);
 
1518
        if (IS_ERR(reply)) {
 
1519
                err = PTR_ERR(reply);
 
1520
                netlink_set_err(INIT_NET_GENL_SOCK, 0,
 
1521
                                ovs_dp_datapath_multicast_group.id, err);
 
1522
                return 0;
 
1523
        }
 
1524
 
 
1525
        genl_notify(reply, genl_info_net(info), info->snd_pid,
 
1526
                    ovs_dp_datapath_multicast_group.id, info->nlhdr,
 
1527
                    GFP_KERNEL);
 
1528
 
 
1529
        return 0;
 
1530
}
 
1531
 
 
1532
static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
 
1533
{
 
1534
        struct sk_buff *reply;
 
1535
        struct datapath *dp;
 
1536
        int err;
 
1537
 
 
1538
        err = ovs_dp_cmd_validate(info->attrs);
 
1539
        if (err)
 
1540
                return err;
 
1541
 
 
1542
        dp = lookup_datapath(info->userhdr, info->attrs);
 
1543
        if (IS_ERR(dp))
 
1544
                return PTR_ERR(dp);
 
1545
 
 
1546
        reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
 
1547
                                      info->snd_seq, OVS_DP_CMD_NEW);
 
1548
        if (IS_ERR(reply))
 
1549
                return PTR_ERR(reply);
 
1550
 
 
1551
        return genlmsg_reply(reply, info);
 
1552
}
 
1553
 
 
1554
static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
 
1555
{
 
1556
        struct datapath *dp;
 
1557
        int skip = cb->args[0];
 
1558
        int i = 0;
 
1559
 
 
1560
        list_for_each_entry(dp, &dps, list_node) {
 
1561
                if (i >= skip &&
 
1562
                    ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).pid,
 
1563
                                         cb->nlh->nlmsg_seq, NLM_F_MULTI,
 
1564
                                         OVS_DP_CMD_NEW) < 0)
 
1565
                        break;
 
1566
                i++;
 
1567
        }
 
1568
 
 
1569
        cb->args[0] = i;
 
1570
 
 
1571
        return skb->len;
 
1572
}
 
1573
 
 
1574
static struct genl_ops dp_datapath_genl_ops[] = {
 
1575
        { .cmd = OVS_DP_CMD_NEW,
 
1576
          .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
 
1577
          .policy = datapath_policy,
 
1578
          .doit = ovs_dp_cmd_new
 
1579
        },
 
1580
        { .cmd = OVS_DP_CMD_DEL,
 
1581
          .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
 
1582
          .policy = datapath_policy,
 
1583
          .doit = ovs_dp_cmd_del
 
1584
        },
 
1585
        { .cmd = OVS_DP_CMD_GET,
 
1586
          .flags = 0,               /* OK for unprivileged users. */
 
1587
          .policy = datapath_policy,
 
1588
          .doit = ovs_dp_cmd_get,
 
1589
          .dumpit = ovs_dp_cmd_dump
 
1590
        },
 
1591
        { .cmd = OVS_DP_CMD_SET,
 
1592
          .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
 
1593
          .policy = datapath_policy,
 
1594
          .doit = ovs_dp_cmd_set,
 
1595
        },
 
1596
};
 
1597
 
 
1598
static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
 
1599
#ifdef HAVE_NLA_NUL_STRING
 
1600
        [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
 
1601
        [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
 
1602
        [OVS_VPORT_ATTR_ADDRESS] = { .len = ETH_ALEN },
 
1603
#else
 
1604
        [OVS_VPORT_ATTR_STATS] = { .minlen = sizeof(struct ovs_vport_stats) },
 
1605
        [OVS_VPORT_ATTR_ADDRESS] = { .minlen = ETH_ALEN },
 
1606
#endif
 
1607
        [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
 
1608
        [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
 
1609
        [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
 
1610
        [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
 
1611
};
 
1612
 
 
1613
static struct genl_family dp_vport_genl_family = {
 
1614
        .id = GENL_ID_GENERATE,
 
1615
        .hdrsize = sizeof(struct ovs_header),
 
1616
        .name = OVS_VPORT_FAMILY,
 
1617
        .version = OVS_VPORT_VERSION,
 
1618
        .maxattr = OVS_VPORT_ATTR_MAX
 
1619
};
 
1620
 
 
1621
struct genl_multicast_group ovs_dp_vport_multicast_group = {
 
1622
        .name = OVS_VPORT_MCGROUP
 
1623
};
 
1624
 
 
1625
/* Called with RTNL lock or RCU read lock. */
 
1626
static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
 
1627
                                   u32 pid, u32 seq, u32 flags, u8 cmd)
 
1628
{
 
1629
        struct ovs_header *ovs_header;
 
1630
        struct ovs_vport_stats vport_stats;
 
1631
        int err;
 
1632
 
 
1633
        ovs_header = genlmsg_put(skb, pid, seq, &dp_vport_genl_family,
 
1634
                                 flags, cmd);
 
1635
        if (!ovs_header)
 
1636
                return -EMSGSIZE;
 
1637
 
 
1638
        ovs_header->dp_ifindex = get_dpifindex(vport->dp);
 
1639
 
 
1640
        if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
 
1641
            nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
 
1642
            nla_put_string(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport)) ||
 
1643
            nla_put_u32(skb, OVS_VPORT_ATTR_UPCALL_PID, vport->upcall_pid))
 
1644
                goto nla_put_failure;
 
1645
 
 
1646
        ovs_vport_get_stats(vport, &vport_stats);
 
1647
        if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
 
1648
                    &vport_stats))
 
1649
                goto nla_put_failure;
 
1650
 
 
1651
        if (nla_put(skb, OVS_VPORT_ATTR_ADDRESS, ETH_ALEN,
 
1652
                    vport->ops->get_addr(vport)))
 
1653
                goto nla_put_failure;
 
1654
 
 
1655
        err = ovs_vport_get_options(vport, skb);
 
1656
        if (err == -EMSGSIZE)
 
1657
                goto error;
 
1658
 
 
1659
        return genlmsg_end(skb, ovs_header);
 
1660
 
 
1661
nla_put_failure:
 
1662
        err = -EMSGSIZE;
 
1663
error:
 
1664
        genlmsg_cancel(skb, ovs_header);
 
1665
        return err;
 
1666
}
 
1667
 
 
1668
/* Called with RTNL lock or RCU read lock. */
 
1669
struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 pid,
 
1670
                                         u32 seq, u8 cmd)
 
1671
{
 
1672
        struct sk_buff *skb;
 
1673
        int retval;
 
1674
 
 
1675
        skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
 
1676
        if (!skb)
 
1677
                return ERR_PTR(-ENOMEM);
 
1678
 
 
1679
        retval = ovs_vport_cmd_fill_info(vport, skb, pid, seq, 0, cmd);
 
1680
        if (retval < 0) {
 
1681
                kfree_skb(skb);
 
1682
                return ERR_PTR(retval);
 
1683
        }
 
1684
        return skb;
 
1685
}
 
1686
 
 
1687
static int ovs_vport_cmd_validate(struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
 
1688
{
 
1689
        return CHECK_NUL_STRING(a[OVS_VPORT_ATTR_NAME], IFNAMSIZ - 1);
 
1690
}
 
1691
 
 
1692
/* Called with RTNL lock or RCU read lock. */
 
1693
static struct vport *lookup_vport(struct ovs_header *ovs_header,
 
1694
                                  struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
 
1695
{
 
1696
        struct datapath *dp;
 
1697
        struct vport *vport;
 
1698
 
 
1699
        if (a[OVS_VPORT_ATTR_NAME]) {
 
1700
                vport = ovs_vport_locate(nla_data(a[OVS_VPORT_ATTR_NAME]));
 
1701
                if (!vport)
 
1702
                        return ERR_PTR(-ENODEV);
 
1703
                return vport;
 
1704
        } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
 
1705
                u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
 
1706
 
 
1707
                if (port_no >= DP_MAX_PORTS)
 
1708
                        return ERR_PTR(-EFBIG);
 
1709
 
 
1710
                dp = get_dp(ovs_header->dp_ifindex);
 
1711
                if (!dp)
 
1712
                        return ERR_PTR(-ENODEV);
 
1713
 
 
1714
                vport = rcu_dereference_rtnl(dp->ports[port_no]);
 
1715
                if (!vport)
 
1716
                        return ERR_PTR(-ENOENT);
 
1717
                return vport;
 
1718
        } else
 
1719
                return ERR_PTR(-EINVAL);
 
1720
}
 
1721
 
 
1722
/* Called with RTNL lock. */
 
1723
static int change_vport(struct vport *vport,
 
1724
                        struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
 
1725
{
 
1726
        int err = 0;
 
1727
 
 
1728
        if (a[OVS_VPORT_ATTR_STATS])
 
1729
                ovs_vport_set_stats(vport, nla_data(a[OVS_VPORT_ATTR_STATS]));
 
1730
 
 
1731
        if (a[OVS_VPORT_ATTR_ADDRESS])
 
1732
                err = ovs_vport_set_addr(vport, nla_data(a[OVS_VPORT_ATTR_ADDRESS]));
 
1733
 
 
1734
        return err;
 
1735
}
 
1736
 
 
1737
static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
 
1738
{
 
1739
        struct nlattr **a = info->attrs;
 
1740
        struct ovs_header *ovs_header = info->userhdr;
 
1741
        struct vport_parms parms;
 
1742
        struct sk_buff *reply;
 
1743
        struct vport *vport;
 
1744
        struct datapath *dp;
 
1745
        u32 port_no;
 
1746
        int err;
 
1747
 
 
1748
        err = -EINVAL;
 
1749
        if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
 
1750
            !a[OVS_VPORT_ATTR_UPCALL_PID])
 
1751
                goto exit;
 
1752
 
 
1753
        err = ovs_vport_cmd_validate(a);
 
1754
        if (err)
 
1755
                goto exit;
 
1756
 
 
1757
        rtnl_lock();
 
1758
        dp = get_dp(ovs_header->dp_ifindex);
 
1759
        err = -ENODEV;
 
1760
        if (!dp)
 
1761
                goto exit_unlock;
 
1762
 
 
1763
        if (a[OVS_VPORT_ATTR_PORT_NO]) {
 
1764
                port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
 
1765
 
 
1766
                err = -EFBIG;
 
1767
                if (port_no >= DP_MAX_PORTS)
 
1768
                        goto exit_unlock;
 
1769
 
 
1770
                vport = rtnl_dereference(dp->ports[port_no]);
 
1771
                err = -EBUSY;
 
1772
                if (vport)
 
1773
                        goto exit_unlock;
 
1774
        } else {
 
1775
                for (port_no = 1; ; port_no++) {
 
1776
                        if (port_no >= DP_MAX_PORTS) {
 
1777
                                err = -EFBIG;
 
1778
                                goto exit_unlock;
 
1779
                        }
 
1780
                        vport = rtnl_dereference(dp->ports[port_no]);
 
1781
                        if (!vport)
 
1782
                                break;
 
1783
                }
 
1784
        }
 
1785
 
 
1786
        parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
 
1787
        parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
 
1788
        parms.options = a[OVS_VPORT_ATTR_OPTIONS];
 
1789
        parms.dp = dp;
 
1790
        parms.port_no = port_no;
 
1791
        parms.upcall_pid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
 
1792
 
 
1793
        vport = new_vport(&parms);
 
1794
        err = PTR_ERR(vport);
 
1795
        if (IS_ERR(vport))
 
1796
                goto exit_unlock;
 
1797
 
 
1798
        ovs_dp_sysfs_add_if(vport);
 
1799
 
 
1800
        err = change_vport(vport, a);
 
1801
        if (!err) {
 
1802
                reply = ovs_vport_cmd_build_info(vport, info->snd_pid,
 
1803
                                                 info->snd_seq,
 
1804
                                                 OVS_VPORT_CMD_NEW);
 
1805
                if (IS_ERR(reply))
 
1806
                        err = PTR_ERR(reply);
 
1807
        }
 
1808
        if (err) {
 
1809
                ovs_dp_detach_port(vport);
 
1810
                goto exit_unlock;
 
1811
        }
 
1812
        genl_notify(reply, genl_info_net(info), info->snd_pid,
 
1813
                    ovs_dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
 
1814
 
 
1815
 
 
1816
exit_unlock:
 
1817
        rtnl_unlock();
 
1818
exit:
 
1819
        return err;
 
1820
}
 
1821
 
 
1822
static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
 
1823
{
 
1824
        struct nlattr **a = info->attrs;
 
1825
        struct sk_buff *reply;
 
1826
        struct vport *vport;
 
1827
        int err;
 
1828
 
 
1829
        err = ovs_vport_cmd_validate(a);
 
1830
        if (err)
 
1831
                goto exit;
 
1832
 
 
1833
        rtnl_lock();
 
1834
        vport = lookup_vport(info->userhdr, a);
 
1835
        err = PTR_ERR(vport);
 
1836
        if (IS_ERR(vport))
 
1837
                goto exit_unlock;
 
1838
 
 
1839
        err = 0;
 
1840
        if (a[OVS_VPORT_ATTR_TYPE] &&
 
1841
            nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type)
 
1842
                err = -EINVAL;
 
1843
 
 
1844
        if (!err && a[OVS_VPORT_ATTR_OPTIONS])
 
1845
                err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
 
1846
        if (!err)
 
1847
                err = change_vport(vport, a);
 
1848
        if (!err && a[OVS_VPORT_ATTR_UPCALL_PID])
 
1849
                vport->upcall_pid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
 
1850
 
 
1851
        reply = ovs_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
 
1852
                                         OVS_VPORT_CMD_NEW);
 
1853
        if (IS_ERR(reply)) {
 
1854
                err = PTR_ERR(reply);
 
1855
                netlink_set_err(INIT_NET_GENL_SOCK, 0,
 
1856
                                ovs_dp_vport_multicast_group.id, err);
 
1857
                return 0;
 
1858
        }
 
1859
 
 
1860
        genl_notify(reply, genl_info_net(info), info->snd_pid,
 
1861
                    ovs_dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
 
1862
 
 
1863
exit_unlock:
 
1864
        rtnl_unlock();
 
1865
exit:
 
1866
        return err;
 
1867
}
 
1868
 
 
1869
static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
 
1870
{
 
1871
        struct nlattr **a = info->attrs;
 
1872
        struct sk_buff *reply;
 
1873
        struct vport *vport;
 
1874
        int err;
 
1875
 
 
1876
        err = ovs_vport_cmd_validate(a);
 
1877
        if (err)
 
1878
                goto exit;
 
1879
 
 
1880
        rtnl_lock();
 
1881
        vport = lookup_vport(info->userhdr, a);
 
1882
        err = PTR_ERR(vport);
 
1883
        if (IS_ERR(vport))
 
1884
                goto exit_unlock;
 
1885
 
 
1886
        if (vport->port_no == OVSP_LOCAL) {
 
1887
                err = -EINVAL;
 
1888
                goto exit_unlock;
 
1889
        }
 
1890
 
 
1891
        reply = ovs_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
 
1892
                                         OVS_VPORT_CMD_DEL);
 
1893
        err = PTR_ERR(reply);
 
1894
        if (IS_ERR(reply))
 
1895
                goto exit_unlock;
 
1896
 
 
1897
        ovs_dp_detach_port(vport);
 
1898
 
 
1899
        genl_notify(reply, genl_info_net(info), info->snd_pid,
 
1900
                    ovs_dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
 
1901
 
 
1902
exit_unlock:
 
1903
        rtnl_unlock();
 
1904
exit:
 
1905
        return err;
 
1906
}
 
1907
 
 
1908
static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
 
1909
{
 
1910
        struct nlattr **a = info->attrs;
 
1911
        struct ovs_header *ovs_header = info->userhdr;
 
1912
        struct sk_buff *reply;
 
1913
        struct vport *vport;
 
1914
        int err;
 
1915
 
 
1916
        err = ovs_vport_cmd_validate(a);
 
1917
        if (err)
 
1918
                goto exit;
 
1919
 
 
1920
        rcu_read_lock();
 
1921
        vport = lookup_vport(ovs_header, a);
 
1922
        err = PTR_ERR(vport);
 
1923
        if (IS_ERR(vport))
 
1924
                goto exit_unlock;
 
1925
 
 
1926
        reply = ovs_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
 
1927
                                         OVS_VPORT_CMD_NEW);
 
1928
        err = PTR_ERR(reply);
 
1929
        if (IS_ERR(reply))
 
1930
                goto exit_unlock;
 
1931
 
 
1932
        rcu_read_unlock();
 
1933
 
 
1934
        return genlmsg_reply(reply, info);
 
1935
 
 
1936
exit_unlock:
 
1937
        rcu_read_unlock();
 
1938
exit:
 
1939
        return err;
 
1940
}
 
1941
 
 
1942
static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
 
1943
{
 
1944
        struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
 
1945
        struct datapath *dp;
 
1946
        u32 port_no;
 
1947
        int retval;
 
1948
 
 
1949
        dp = get_dp(ovs_header->dp_ifindex);
 
1950
        if (!dp)
 
1951
                return -ENODEV;
 
1952
 
 
1953
        rcu_read_lock();
 
1954
        for (port_no = cb->args[0]; port_no < DP_MAX_PORTS; port_no++) {
 
1955
                struct vport *vport;
 
1956
 
 
1957
                vport = rcu_dereference(dp->ports[port_no]);
 
1958
                if (!vport)
 
1959
                        continue;
 
1960
 
 
1961
                if (ovs_vport_cmd_fill_info(vport, skb, NETLINK_CB(cb->skb).pid,
 
1962
                                            cb->nlh->nlmsg_seq, NLM_F_MULTI,
 
1963
                                            OVS_VPORT_CMD_NEW) < 0)
 
1964
                        break;
 
1965
        }
 
1966
        rcu_read_unlock();
 
1967
 
 
1968
        cb->args[0] = port_no;
 
1969
        retval = skb->len;
 
1970
 
 
1971
        return retval;
 
1972
}
 
1973
 
 
1974
static struct genl_ops dp_vport_genl_ops[] = {
 
1975
        { .cmd = OVS_VPORT_CMD_NEW,
 
1976
          .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
 
1977
          .policy = vport_policy,
 
1978
          .doit = ovs_vport_cmd_new
 
1979
        },
 
1980
        { .cmd = OVS_VPORT_CMD_DEL,
 
1981
          .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
 
1982
          .policy = vport_policy,
 
1983
          .doit = ovs_vport_cmd_del
 
1984
        },
 
1985
        { .cmd = OVS_VPORT_CMD_GET,
 
1986
          .flags = 0,               /* OK for unprivileged users. */
 
1987
          .policy = vport_policy,
 
1988
          .doit = ovs_vport_cmd_get,
 
1989
          .dumpit = ovs_vport_cmd_dump
 
1990
        },
 
1991
        { .cmd = OVS_VPORT_CMD_SET,
 
1992
          .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
 
1993
          .policy = vport_policy,
 
1994
          .doit = ovs_vport_cmd_set,
 
1995
        },
 
1996
};
 
1997
 
 
1998
struct genl_family_and_ops {
 
1999
        struct genl_family *family;
 
2000
        struct genl_ops *ops;
 
2001
        int n_ops;
 
2002
        struct genl_multicast_group *group;
 
2003
};
 
2004
 
 
2005
static const struct genl_family_and_ops dp_genl_families[] = {
 
2006
        { &dp_datapath_genl_family,
 
2007
          dp_datapath_genl_ops, ARRAY_SIZE(dp_datapath_genl_ops),
 
2008
          &ovs_dp_datapath_multicast_group },
 
2009
        { &dp_vport_genl_family,
 
2010
          dp_vport_genl_ops, ARRAY_SIZE(dp_vport_genl_ops),
 
2011
          &ovs_dp_vport_multicast_group },
 
2012
        { &dp_flow_genl_family,
 
2013
          dp_flow_genl_ops, ARRAY_SIZE(dp_flow_genl_ops),
 
2014
          &ovs_dp_flow_multicast_group },
 
2015
        { &dp_packet_genl_family,
 
2016
          dp_packet_genl_ops, ARRAY_SIZE(dp_packet_genl_ops),
 
2017
          NULL },
 
2018
};
 
2019
 
 
2020
static void dp_unregister_genl(int n_families)
 
2021
{
 
2022
        int i;
 
2023
 
 
2024
        for (i = 0; i < n_families; i++)
 
2025
                genl_unregister_family(dp_genl_families[i].family);
 
2026
}
 
2027
 
 
2028
static int dp_register_genl(void)
 
2029
{
 
2030
        int n_registered;
 
2031
        int err;
 
2032
        int i;
 
2033
 
 
2034
        n_registered = 0;
 
2035
        for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
 
2036
                const struct genl_family_and_ops *f = &dp_genl_families[i];
 
2037
 
 
2038
                err = genl_register_family_with_ops(f->family, f->ops,
 
2039
                                                    f->n_ops);
 
2040
                if (err)
 
2041
                        goto error;
 
2042
                n_registered++;
 
2043
 
 
2044
                if (f->group) {
 
2045
                        err = genl_register_mc_group(f->family, f->group);
 
2046
                        if (err)
 
2047
                                goto error;
 
2048
                }
 
2049
        }
 
2050
 
 
2051
        return 0;
 
2052
 
 
2053
error:
 
2054
        dp_unregister_genl(n_registered);
 
2055
        return err;
 
2056
}
 
2057
 
 
2058
static int __init dp_init(void)
 
2059
{
 
2060
        struct sk_buff *dummy_skb;
 
2061
        int err;
 
2062
 
 
2063
        BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > sizeof(dummy_skb->cb));
 
2064
 
 
2065
        pr_info("Open vSwitch switching datapath %s, built "__DATE__" "__TIME__"\n",
 
2066
                VERSION BUILDNR);
 
2067
 
 
2068
        err = ovs_tnl_init();
 
2069
        if (err)
 
2070
                goto error;
 
2071
 
 
2072
        err = ovs_flow_init();
 
2073
        if (err)
 
2074
                goto error_tnl_exit;
 
2075
 
 
2076
        err = ovs_vport_init();
 
2077
        if (err)
 
2078
                goto error_flow_exit;
 
2079
 
 
2080
        err = register_netdevice_notifier(&ovs_dp_device_notifier);
 
2081
        if (err)
 
2082
                goto error_vport_exit;
 
2083
 
 
2084
        err = dp_register_genl();
 
2085
        if (err < 0)
 
2086
                goto error_unreg_notifier;
 
2087
 
 
2088
        return 0;
 
2089
 
 
2090
error_unreg_notifier:
 
2091
        unregister_netdevice_notifier(&ovs_dp_device_notifier);
 
2092
error_vport_exit:
 
2093
        ovs_vport_exit();
 
2094
error_flow_exit:
 
2095
        ovs_flow_exit();
 
2096
error_tnl_exit:
 
2097
        ovs_tnl_exit();
 
2098
error:
 
2099
        return err;
 
2100
}
 
2101
 
 
2102
static void dp_cleanup(void)
 
2103
{
 
2104
        rcu_barrier();
 
2105
        dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
 
2106
        unregister_netdevice_notifier(&ovs_dp_device_notifier);
 
2107
        ovs_vport_exit();
 
2108
        ovs_flow_exit();
 
2109
        ovs_tnl_exit();
 
2110
}
 
2111
 
 
2112
module_init(dp_init);
 
2113
module_exit(dp_cleanup);
 
2114
 
 
2115
MODULE_DESCRIPTION("Open vSwitch switching datapath");
 
2116
MODULE_LICENSE("GPL");