2
* Copyright (c) 2007-2012 Nicira Networks.
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.
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.
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
19
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21
#include <linux/init.h>
22
#include <linux/module.h>
23
#include <linux/if_arp.h>
24
#include <linux/if_vlan.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>
59
#include "vport-internal_dev.h"
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.
66
int (*ovs_dp_ioctl_hook)(struct net_device *dev, struct ifreq *rq, int cmd);
67
EXPORT_SYMBOL(ovs_dp_ioctl_hook);
72
* Writes to device state (add/remove datapath, port, set operations on vports,
73
* etc.) are protected by RTNL.
75
* Writes to other state (flow table modifications, set miscellaneous datapath
76
* parameters, etc.) are protected by genl_mutex. The RTNL lock nests inside
79
* Reads are protected by RCU.
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
86
/* Global list of datapaths to enable dumping them all out.
87
* Protected by genl_mutex.
89
static LIST_HEAD(dps);
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 *);
97
/* Must be called with rcu_read_lock, genl_mutex, or RTNL lock. */
98
static struct datapath *get_dp(int dp_ifindex)
100
struct datapath *dp = NULL;
101
struct net_device *dev;
104
dev = dev_get_by_index_rcu(&init_net, dp_ifindex);
106
struct vport *vport = ovs_internal_dev_get_vport(dev);
115
/* Must be called with rcu_read_lock or RTNL lock. */
116
const char *ovs_dp_name(const struct datapath *dp)
118
struct vport *vport = rcu_dereference_rtnl(dp->ports[OVSP_LOCAL]);
119
return vport->ops->get_name(vport);
122
static int get_dpifindex(struct datapath *dp)
129
local = rcu_dereference(dp->ports[OVSP_LOCAL]);
131
ifindex = local->ops->get_ifindex(local);
140
static size_t br_nlmsg_size(void)
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 */
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)
155
struct datapath *dp = port->dp;
156
struct ifinfomsg *hdr;
157
struct nlmsghdr *nlh;
159
if (!port->ops->get_ifindex)
162
nlh = nlmsg_put(skb, 0, 0, event, sizeof(*hdr), flags);
166
hdr = nlmsg_data(nlh);
167
hdr->ifi_family = AF_BRIDGE;
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);
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) :
183
nla_put(skb, IFLA_ADDRESS, ETH_ALEN, port->ops->get_addr(port)))
184
goto nla_put_failure;
186
return nlmsg_end(skb, nlh);
189
nlmsg_cancel(skb, nlh);
193
/* Caller must hold RTNL lock. */
194
static void dp_ifinfo_notify(int event, struct vport *port)
199
skb = nlmsg_new(br_nlmsg_size(), GFP_KERNEL);
205
err = dp_fill_ifinfo(skb, port, event, 0);
207
if (err == -ENODEV) {
210
/* -EMSGSIZE implies BUG in br_nlmsg_size() */
211
WARN_ON(err == -EMSGSIZE);
216
rtnl_notify(skb, &init_net, 0, RTNLGRP_LINK, NULL, GFP_KERNEL);
220
rtnl_set_sk_err(&init_net, RTNLGRP_LINK, err);
225
static void release_dp(struct kobject *kobj)
227
struct datapath *dp = container_of(kobj, struct datapath, ifobj);
231
static struct kobj_type dp_ktype = {
232
.release = release_dp
235
static void destroy_dp_rcu(struct rcu_head *rcu)
237
struct datapath *dp = container_of(rcu, struct datapath, rcu);
239
ovs_flow_tbl_destroy((__force struct flow_table *)dp->table);
240
free_percpu(dp->stats_percpu);
241
kobject_put(&dp->ifobj);
244
/* Called with RTNL lock and genl_lock. */
245
static struct vport *new_vport(const struct vport_parms *parms)
249
vport = ovs_vport_add(parms);
250
if (!IS_ERR(vport)) {
251
struct datapath *dp = parms->dp;
253
rcu_assign_pointer(dp->ports[parms->port_no], vport);
254
list_add(&vport->node, &dp->port_list);
256
dp_ifinfo_notify(RTM_NEWLINK, vport);
262
/* Called with RTNL lock. */
263
void ovs_dp_detach_port(struct vport *p)
267
if (p->port_no != OVSP_LOCAL)
268
ovs_dp_sysfs_del_if(p);
269
dp_ifinfo_notify(RTM_DELLINK, p);
271
/* First drop references to device. */
273
rcu_assign_pointer(p->dp->ports[p->port_no], NULL);
275
/* Then destroy it. */
279
/* Must be called with rcu_read_lock. */
280
void ovs_dp_process_received_packet(struct vport *p, struct sk_buff *skb)
282
struct datapath *dp = p->dp;
283
struct sw_flow *flow;
284
struct dp_stats_percpu *stats;
288
stats = per_cpu_ptr(dp->stats_percpu, smp_processor_id());
290
if (!OVS_CB(skb)->flow) {
291
struct sw_flow_key key;
294
/* Extract flow from 'skb' into 'key'. */
295
error = ovs_flow_extract(skb, p->port_no, &key, &key_len);
296
if (unlikely(error)) {
302
flow = ovs_flow_tbl_lookup(rcu_dereference(dp->table),
304
if (unlikely(!flow)) {
305
struct dp_upcall_info upcall;
307
upcall.cmd = OVS_PACKET_CMD_MISS;
309
upcall.userdata = NULL;
310
upcall.pid = p->upcall_pid;
311
ovs_dp_upcall(dp, skb, &upcall);
313
stats_counter = &stats->n_missed;
317
OVS_CB(skb)->flow = flow;
320
stats_counter = &stats->n_hit;
321
ovs_flow_used(OVS_CB(skb)->flow, skb);
322
ovs_execute_actions(dp, skb);
325
/* Update datapath statistics. */
326
u64_stats_update_begin(&stats->sync);
328
u64_stats_update_end(&stats->sync);
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
339
int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
340
const struct dp_upcall_info *upcall_info)
342
struct dp_stats_percpu *stats;
346
if (upcall_info->pid == 0) {
351
dp_ifindex = get_dpifindex(dp);
357
forward_ip_summed(skb, true);
359
if (!skb_is_gso(skb))
360
err = queue_userspace_packet(dp_ifindex, skb, upcall_info);
362
err = queue_gso_packets(dp_ifindex, skb, upcall_info);
369
stats = per_cpu_ptr(dp->stats_percpu, smp_processor_id());
371
u64_stats_update_begin(&stats->sync);
373
u64_stats_update_end(&stats->sync);
378
static int queue_gso_packets(int dp_ifindex, struct sk_buff *skb,
379
const struct dp_upcall_info *upcall_info)
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;
387
segs = skb_gso_segment(skb, NETIF_F_SG | NETIF_F_HW_CSUM);
389
return PTR_ERR(segs);
391
/* Queue all of the segments. */
394
err = queue_userspace_packet(dp_ifindex, skb, upcall_info);
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.
403
later_key = *upcall_info->key;
404
later_key.ip.frag = OVS_FRAG_TYPE_LATER;
406
later_info = *upcall_info;
407
later_info.key = &later_key;
408
upcall_info = &later_info;
410
} while ((skb = skb->next));
412
/* Free all of the segments. */
420
} while ((skb = nskb));
424
static int queue_userspace_packet(int dp_ifindex, struct sk_buff *skb,
425
const struct dp_upcall_info *upcall_info)
427
struct ovs_header *upcall;
428
struct sk_buff *nskb = NULL;
429
struct sk_buff *user_skb; /* to be queued to userspace */
434
if (vlan_tx_tag_present(skb)) {
435
nskb = skb_clone(skb, GFP_ATOMIC);
439
err = vlan_deaccel_tag(nskb);
446
if (nla_attr_size(skb->len) > USHRT_MAX) {
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);
457
user_skb = genlmsg_new(len, GFP_ATOMIC);
463
upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
464
0, upcall_info->cmd);
465
upcall->dp_ifindex = dp_ifindex;
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);
471
if (upcall_info->userdata)
472
nla_put_u64(user_skb, OVS_PACKET_ATTR_USERDATA,
473
nla_get_u64(upcall_info->userdata));
475
nla = __nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, skb->len);
477
skb_copy_and_csum_dev(skb, nla_data(nla));
479
err = genlmsg_unicast(&init_net, user_skb, upcall_info->pid);
486
/* Called with genl_mutex. */
487
static int flush_flows(int dp_ifindex)
489
struct flow_table *old_table;
490
struct flow_table *new_table;
493
dp = get_dp(dp_ifindex);
497
old_table = genl_dereference(dp->table);
498
new_table = ovs_flow_tbl_alloc(TBL_MIN_BUCKETS);
502
rcu_assign_pointer(dp->table, new_table);
504
ovs_flow_tbl_deferred_destroy(old_table);
508
static int validate_actions(const struct nlattr *attr,
509
const struct sw_flow_key *key, int depth);
511
static int validate_sample(const struct nlattr *attr,
512
const struct sw_flow_key *key, int depth)
514
const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
515
const struct nlattr *probability, *actions;
516
const struct nlattr *a;
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])
529
probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
530
if (!probability || nla_len(probability) != sizeof(u32))
533
actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
534
if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
536
return validate_actions(actions, key, depth + 1);
539
static int validate_tp_port(const struct sw_flow_key *flow_key)
541
if (flow_key->eth.type == htons(ETH_P_IP)) {
542
if (flow_key->ipv4.tp.src || flow_key->ipv4.tp.dst)
544
} else if (flow_key->eth.type == htons(ETH_P_IPV6)) {
545
if (flow_key->ipv6.tp.src || flow_key->ipv6.tp.dst)
552
static int validate_set(const struct nlattr *a,
553
const struct sw_flow_key *flow_key)
555
const struct nlattr *ovs_key = nla_data(a);
556
int key_type = nla_type(ovs_key);
558
/* There can be only one key in a action */
559
if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
562
if (key_type > OVS_KEY_ATTR_MAX ||
563
nla_len(ovs_key) != ovs_key_lens[key_type])
567
const struct ovs_key_ipv4 *ipv4_key;
569
case OVS_KEY_ATTR_PRIORITY:
570
case OVS_KEY_ATTR_TUN_ID:
571
case OVS_KEY_ATTR_ETHERNET:
574
case OVS_KEY_ATTR_IPV4:
575
if (flow_key->eth.type != htons(ETH_P_IP))
578
if (!flow_key->ip.proto)
581
ipv4_key = nla_data(ovs_key);
582
if (ipv4_key->ipv4_proto != flow_key->ip.proto)
585
if (ipv4_key->ipv4_frag != flow_key->ip.frag)
590
case OVS_KEY_ATTR_TCP:
591
if (flow_key->ip.proto != IPPROTO_TCP)
594
return validate_tp_port(flow_key);
596
case OVS_KEY_ATTR_UDP:
597
if (flow_key->ip.proto != IPPROTO_UDP)
600
return validate_tp_port(flow_key);
609
static int validate_userspace(const struct nlattr *attr)
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 },
615
struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
618
error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
619
attr, userspace_policy);
623
if (!a[OVS_USERSPACE_ATTR_PID] ||
624
!nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
630
static int validate_actions(const struct nlattr *attr,
631
const struct sw_flow_key *key, int depth)
633
const struct nlattr *a;
636
if (depth >= SAMPLE_ACTION_DEPTH)
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
649
const struct ovs_action_push_vlan *vlan;
650
int type = nla_type(a);
652
if (type > OVS_ACTION_ATTR_MAX ||
653
(action_lens[type] != nla_len(a) &&
654
action_lens[type] != (u32)-1))
658
case OVS_ACTION_ATTR_UNSPEC:
661
case OVS_ACTION_ATTR_USERSPACE:
662
err = validate_userspace(a);
667
case OVS_ACTION_ATTR_OUTPUT:
668
if (nla_get_u32(a) >= DP_MAX_PORTS)
673
case OVS_ACTION_ATTR_POP_VLAN:
676
case OVS_ACTION_ATTR_PUSH_VLAN:
678
if (vlan->vlan_tpid != htons(ETH_P_8021Q))
680
if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
684
case OVS_ACTION_ATTR_SET:
685
err = validate_set(a, key);
690
case OVS_ACTION_ATTR_SAMPLE:
691
err = validate_sample(a, key, depth);
707
static void clear_stats(struct sw_flow *flow)
711
flow->packet_count = 0;
712
flow->byte_count = 0;
715
static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
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;
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)
734
len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
735
packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
739
skb_reserve(packet, NET_IP_ALIGN);
741
memcpy(__skb_put(packet, len), nla_data(a[OVS_PACKET_ATTR_PACKET]), len);
743
skb_reset_mac_header(packet);
744
eth = eth_hdr(packet);
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;
752
packet->protocol = htons(ETH_P_802_2);
754
/* Build an sw_flow for sending this packet. */
755
flow = ovs_flow_alloc();
760
err = ovs_flow_extract(packet, -1, &flow->key, &key_len);
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]);
771
err = validate_actions(a[OVS_PACKET_ATTR_ACTIONS], &flow->key, 0);
775
flow->hash = ovs_flow_hash(&flow->key, key_len);
777
acts = ovs_flow_actions_alloc(a[OVS_PACKET_ATTR_ACTIONS]);
781
rcu_assign_pointer(flow->sf_acts, acts);
783
OVS_CB(packet)->flow = flow;
784
packet->priority = flow->key.phy.priority;
787
dp = get_dp(ovs_header->dp_ifindex);
793
err = ovs_execute_actions(dp, packet);
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 },
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
824
static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats)
827
struct flow_table *table = genl_dereference(dp->table);
829
stats->n_flows = ovs_flow_tbl_count(table);
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;
837
percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
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));
844
stats->n_hit += local_stats.n_hit;
845
stats->n_missed += local_stats.n_missed;
846
stats->n_lost += local_stats.n_lost;
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 },
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
864
static struct genl_multicast_group ovs_dp_flow_multicast_group = {
865
.name = OVS_FLOW_MCGROUP
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)
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;
882
sf_acts = rcu_dereference_protected(flow->sf_acts,
883
lockdep_genl_is_held());
885
ovs_header = genlmsg_put(skb, pid, seq, &dp_flow_genl_family, flags, cmd);
889
ovs_header->dp_ifindex = get_dpifindex(dp);
891
nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
893
goto nla_put_failure;
894
err = ovs_flow_to_nlattrs(&flow->key, skb);
897
nla_nest_end(skb, nla);
899
spin_lock_bh(&flow->lock);
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);
907
nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
908
goto nla_put_failure;
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;
916
nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, tcp_flags))
917
goto nla_put_failure;
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.)
926
* This can only fail for dump operations because the skb is always
927
* properly sized for single flows.
929
err = nla_put(skb, OVS_FLOW_ATTR_ACTIONS, sf_acts->actions_len,
931
if (err < 0 && skb_orig_len)
934
return genlmsg_end(skb, ovs_header);
939
genlmsg_cancel(skb, ovs_header);
943
static struct sk_buff *ovs_flow_cmd_alloc_info(struct sw_flow *flow)
945
const struct sw_flow_actions *sf_acts;
948
sf_acts = rcu_dereference_protected(flow->sf_acts,
949
lockdep_genl_is_held());
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);
962
len += NLMSG_ALIGN(sizeof(struct ovs_header));
964
return genlmsg_new(len, GFP_KERNEL);
967
static struct sk_buff *ovs_flow_cmd_build_info(struct sw_flow *flow,
969
u32 pid, u32 seq, u8 cmd)
974
skb = ovs_flow_cmd_alloc_info(flow);
976
return ERR_PTR(-ENOMEM);
978
retval = ovs_flow_cmd_fill_info(flow, dp, skb, pid, seq, 0, cmd);
983
static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info)
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;
991
struct flow_table *table;
997
if (!a[OVS_FLOW_ATTR_KEY])
999
error = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
1003
/* Validate actions. */
1004
if (a[OVS_FLOW_ATTR_ACTIONS]) {
1005
error = validate_actions(a[OVS_FLOW_ATTR_ACTIONS], &key, 0);
1008
} else if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW) {
1013
dp = get_dp(ovs_header->dp_ifindex);
1018
table = genl_dereference(dp->table);
1019
flow = ovs_flow_tbl_lookup(table, &key, key_len);
1021
struct sw_flow_actions *acts;
1023
/* Bail out if we're not allowed to create a new flow. */
1025
if (info->genlhdr->cmd == OVS_FLOW_CMD_SET)
1028
/* Expand table, if necessary, to make room. */
1029
if (ovs_flow_tbl_need_to_expand(table)) {
1030
struct flow_table *new_table;
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);
1040
/* Allocate flow. */
1041
flow = ovs_flow_alloc();
1043
error = PTR_ERR(flow);
1049
/* Obtain actions. */
1050
acts = ovs_flow_actions_alloc(a[OVS_FLOW_ATTR_ACTIONS]);
1051
error = PTR_ERR(acts);
1053
goto error_free_flow;
1054
rcu_assign_pointer(flow->sf_acts, acts);
1056
/* Put flow in bucket. */
1057
flow->hash = ovs_flow_hash(&key, key_len);
1058
ovs_flow_tbl_insert(table, flow);
1060
reply = ovs_flow_cmd_build_info(flow, dp, info->snd_pid,
1064
/* We found a matching flow. */
1065
struct sw_flow_actions *old_acts;
1066
struct nlattr *acts_attrs;
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
1075
if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW &&
1076
info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL))
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];
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;
1089
new_acts = ovs_flow_actions_alloc(acts_attrs);
1090
error = PTR_ERR(new_acts);
1091
if (IS_ERR(new_acts))
1094
rcu_assign_pointer(flow->sf_acts, new_acts);
1095
ovs_flow_deferred_free_acts(old_acts);
1098
reply = ovs_flow_cmd_build_info(flow, dp, info->snd_pid,
1099
info->snd_seq, OVS_FLOW_CMD_NEW);
1102
if (a[OVS_FLOW_ATTR_CLEAR]) {
1103
spin_lock_bh(&flow->lock);
1105
spin_unlock_bh(&flow->lock);
1110
genl_notify(reply, genl_info_net(info), info->snd_pid,
1111
ovs_dp_flow_multicast_group.id, info->nlhdr,
1114
netlink_set_err(INIT_NET_GENL_SOCK, 0,
1115
ovs_dp_flow_multicast_group.id,
1125
static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
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;
1137
if (!a[OVS_FLOW_ATTR_KEY])
1139
err = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
1143
dp = get_dp(ovs_header->dp_ifindex);
1147
table = genl_dereference(dp->table);
1148
flow = ovs_flow_tbl_lookup(table, &key, key_len);
1152
reply = ovs_flow_cmd_build_info(flow, dp, info->snd_pid,
1153
info->snd_seq, OVS_FLOW_CMD_NEW);
1155
return PTR_ERR(reply);
1157
return genlmsg_reply(reply, info);
1160
static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
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;
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]);
1178
dp = get_dp(ovs_header->dp_ifindex);
1182
table = genl_dereference(dp->table);
1183
flow = ovs_flow_tbl_lookup(table, &key, key_len);
1187
reply = ovs_flow_cmd_alloc_info(flow);
1191
ovs_flow_tbl_remove(table, flow);
1193
err = ovs_flow_cmd_fill_info(flow, dp, reply, info->snd_pid,
1194
info->snd_seq, 0, OVS_FLOW_CMD_DEL);
1197
ovs_flow_deferred_free(flow);
1199
genl_notify(reply, genl_info_net(info), info->snd_pid,
1200
ovs_dp_flow_multicast_group.id, info->nlhdr, GFP_KERNEL);
1204
static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1206
struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1207
struct datapath *dp;
1208
struct flow_table *table;
1210
dp = get_dp(ovs_header->dp_ifindex);
1214
table = genl_dereference(dp->table);
1217
struct sw_flow *flow;
1220
bucket = cb->args[0];
1222
flow = ovs_flow_tbl_next(table, &bucket, &obj);
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)
1232
cb->args[0] = bucket;
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
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
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
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,
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 },
1266
[OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
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
1277
static struct genl_multicast_group ovs_dp_datapath_multicast_group = {
1278
.name = OVS_DATAPATH_MCGROUP
1281
static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
1282
u32 pid, u32 seq, u32 flags, u8 cmd)
1284
struct ovs_header *ovs_header;
1285
struct ovs_dp_stats dp_stats;
1288
ovs_header = genlmsg_put(skb, pid, seq, &dp_datapath_genl_family,
1293
ovs_header->dp_ifindex = get_dpifindex(dp);
1296
err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
1299
goto nla_put_failure;
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;
1305
return genlmsg_end(skb, ovs_header);
1308
genlmsg_cancel(skb, ovs_header);
1313
static struct sk_buff *ovs_dp_cmd_build_info(struct datapath *dp, u32 pid,
1316
struct sk_buff *skb;
1319
skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1321
return ERR_PTR(-ENOMEM);
1323
retval = ovs_dp_cmd_fill_info(dp, skb, pid, seq, 0, cmd);
1326
return ERR_PTR(retval);
1331
static int ovs_dp_cmd_validate(struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1333
return CHECK_NUL_STRING(a[OVS_DP_ATTR_NAME], IFNAMSIZ - 1);
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])
1340
struct datapath *dp;
1342
if (!a[OVS_DP_ATTR_NAME])
1343
dp = get_dp(ovs_header->dp_ifindex);
1345
struct vport *vport;
1348
vport = ovs_vport_locate(nla_data(a[OVS_DP_ATTR_NAME]));
1349
dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
1352
return dp ? dp : ERR_PTR(-ENODEV);
1355
static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1357
struct nlattr **a = info->attrs;
1358
struct vport_parms parms;
1359
struct sk_buff *reply;
1360
struct datapath *dp;
1361
struct vport *vport;
1365
if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1368
err = ovs_dp_cmd_validate(a);
1374
if (!try_module_get(THIS_MODULE))
1375
goto err_unlock_rtnl;
1378
dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1380
goto err_put_module;
1381
INIT_LIST_HEAD(&dp->port_list);
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);
1388
/* Allocate table. */
1390
rcu_assign_pointer(dp->table, ovs_flow_tbl_alloc(TBL_MIN_BUCKETS));
1394
dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
1395
if (!dp->stats_percpu) {
1397
goto err_destroy_table;
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;
1405
parms.port_no = OVSP_LOCAL;
1406
parms.upcall_pid = nla_get_u32(a[OVS_DP_ATTR_UPCALL_PID]);
1408
vport = new_vport(&parms);
1409
if (IS_ERR(vport)) {
1410
err = PTR_ERR(vport);
1414
goto err_destroy_percpu;
1417
reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
1418
info->snd_seq, OVS_DP_CMD_NEW);
1419
err = PTR_ERR(reply);
1421
goto err_destroy_local_port;
1423
list_add_tail(&dp->list_node, &dps);
1424
ovs_dp_sysfs_add_dp(dp);
1428
genl_notify(reply, genl_info_net(info), info->snd_pid,
1429
ovs_dp_datapath_multicast_group.id, info->nlhdr,
1433
err_destroy_local_port:
1434
ovs_dp_detach_port(rtnl_dereference(dp->ports[OVSP_LOCAL]));
1436
free_percpu(dp->stats_percpu);
1438
ovs_flow_tbl_destroy(genl_dereference(dp->table));
1442
module_put(THIS_MODULE);
1449
static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1451
struct vport *vport, *next_vport;
1452
struct sk_buff *reply;
1453
struct datapath *dp;
1456
err = ovs_dp_cmd_validate(info->attrs);
1461
dp = lookup_datapath(info->userhdr, info->attrs);
1466
reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
1467
info->snd_seq, OVS_DP_CMD_DEL);
1468
err = PTR_ERR(reply);
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);
1476
ovs_dp_sysfs_del_dp(dp);
1477
list_del(&dp->list_node);
1478
ovs_dp_detach_port(rtnl_dereference(dp->ports[OVSP_LOCAL]));
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.
1487
call_rcu(&dp->rcu, destroy_dp_rcu);
1488
module_put(THIS_MODULE);
1490
genl_notify(reply, genl_info_net(info), info->snd_pid,
1491
ovs_dp_datapath_multicast_group.id, info->nlhdr,
1502
static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1504
struct sk_buff *reply;
1505
struct datapath *dp;
1508
err = ovs_dp_cmd_validate(info->attrs);
1512
dp = lookup_datapath(info->userhdr, info->attrs);
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);
1525
genl_notify(reply, genl_info_net(info), info->snd_pid,
1526
ovs_dp_datapath_multicast_group.id, info->nlhdr,
1532
static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1534
struct sk_buff *reply;
1535
struct datapath *dp;
1538
err = ovs_dp_cmd_validate(info->attrs);
1542
dp = lookup_datapath(info->userhdr, info->attrs);
1546
reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
1547
info->snd_seq, OVS_DP_CMD_NEW);
1549
return PTR_ERR(reply);
1551
return genlmsg_reply(reply, info);
1554
static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1556
struct datapath *dp;
1557
int skip = cb->args[0];
1560
list_for_each_entry(dp, &dps, list_node) {
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)
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
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
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
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,
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 },
1604
[OVS_VPORT_ATTR_STATS] = { .minlen = sizeof(struct ovs_vport_stats) },
1605
[OVS_VPORT_ATTR_ADDRESS] = { .minlen = ETH_ALEN },
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 },
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
1621
struct genl_multicast_group ovs_dp_vport_multicast_group = {
1622
.name = OVS_VPORT_MCGROUP
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)
1629
struct ovs_header *ovs_header;
1630
struct ovs_vport_stats vport_stats;
1633
ovs_header = genlmsg_put(skb, pid, seq, &dp_vport_genl_family,
1638
ovs_header->dp_ifindex = get_dpifindex(vport->dp);
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;
1646
ovs_vport_get_stats(vport, &vport_stats);
1647
if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
1649
goto nla_put_failure;
1651
if (nla_put(skb, OVS_VPORT_ATTR_ADDRESS, ETH_ALEN,
1652
vport->ops->get_addr(vport)))
1653
goto nla_put_failure;
1655
err = ovs_vport_get_options(vport, skb);
1656
if (err == -EMSGSIZE)
1659
return genlmsg_end(skb, ovs_header);
1664
genlmsg_cancel(skb, ovs_header);
1668
/* Called with RTNL lock or RCU read lock. */
1669
struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 pid,
1672
struct sk_buff *skb;
1675
skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1677
return ERR_PTR(-ENOMEM);
1679
retval = ovs_vport_cmd_fill_info(vport, skb, pid, seq, 0, cmd);
1682
return ERR_PTR(retval);
1687
static int ovs_vport_cmd_validate(struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1689
return CHECK_NUL_STRING(a[OVS_VPORT_ATTR_NAME], IFNAMSIZ - 1);
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])
1696
struct datapath *dp;
1697
struct vport *vport;
1699
if (a[OVS_VPORT_ATTR_NAME]) {
1700
vport = ovs_vport_locate(nla_data(a[OVS_VPORT_ATTR_NAME]));
1702
return ERR_PTR(-ENODEV);
1704
} else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1705
u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1707
if (port_no >= DP_MAX_PORTS)
1708
return ERR_PTR(-EFBIG);
1710
dp = get_dp(ovs_header->dp_ifindex);
1712
return ERR_PTR(-ENODEV);
1714
vport = rcu_dereference_rtnl(dp->ports[port_no]);
1716
return ERR_PTR(-ENOENT);
1719
return ERR_PTR(-EINVAL);
1722
/* Called with RTNL lock. */
1723
static int change_vport(struct vport *vport,
1724
struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1728
if (a[OVS_VPORT_ATTR_STATS])
1729
ovs_vport_set_stats(vport, nla_data(a[OVS_VPORT_ATTR_STATS]));
1731
if (a[OVS_VPORT_ATTR_ADDRESS])
1732
err = ovs_vport_set_addr(vport, nla_data(a[OVS_VPORT_ATTR_ADDRESS]));
1737
static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
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;
1749
if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
1750
!a[OVS_VPORT_ATTR_UPCALL_PID])
1753
err = ovs_vport_cmd_validate(a);
1758
dp = get_dp(ovs_header->dp_ifindex);
1763
if (a[OVS_VPORT_ATTR_PORT_NO]) {
1764
port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1767
if (port_no >= DP_MAX_PORTS)
1770
vport = rtnl_dereference(dp->ports[port_no]);
1775
for (port_no = 1; ; port_no++) {
1776
if (port_no >= DP_MAX_PORTS) {
1780
vport = rtnl_dereference(dp->ports[port_no]);
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];
1790
parms.port_no = port_no;
1791
parms.upcall_pid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
1793
vport = new_vport(&parms);
1794
err = PTR_ERR(vport);
1798
ovs_dp_sysfs_add_if(vport);
1800
err = change_vport(vport, a);
1802
reply = ovs_vport_cmd_build_info(vport, info->snd_pid,
1806
err = PTR_ERR(reply);
1809
ovs_dp_detach_port(vport);
1812
genl_notify(reply, genl_info_net(info), info->snd_pid,
1813
ovs_dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1822
static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
1824
struct nlattr **a = info->attrs;
1825
struct sk_buff *reply;
1826
struct vport *vport;
1829
err = ovs_vport_cmd_validate(a);
1834
vport = lookup_vport(info->userhdr, a);
1835
err = PTR_ERR(vport);
1840
if (a[OVS_VPORT_ATTR_TYPE] &&
1841
nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type)
1844
if (!err && a[OVS_VPORT_ATTR_OPTIONS])
1845
err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
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]);
1851
reply = ovs_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
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);
1860
genl_notify(reply, genl_info_net(info), info->snd_pid,
1861
ovs_dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1869
static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
1871
struct nlattr **a = info->attrs;
1872
struct sk_buff *reply;
1873
struct vport *vport;
1876
err = ovs_vport_cmd_validate(a);
1881
vport = lookup_vport(info->userhdr, a);
1882
err = PTR_ERR(vport);
1886
if (vport->port_no == OVSP_LOCAL) {
1891
reply = ovs_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
1893
err = PTR_ERR(reply);
1897
ovs_dp_detach_port(vport);
1899
genl_notify(reply, genl_info_net(info), info->snd_pid,
1900
ovs_dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1908
static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
1910
struct nlattr **a = info->attrs;
1911
struct ovs_header *ovs_header = info->userhdr;
1912
struct sk_buff *reply;
1913
struct vport *vport;
1916
err = ovs_vport_cmd_validate(a);
1921
vport = lookup_vport(ovs_header, a);
1922
err = PTR_ERR(vport);
1926
reply = ovs_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
1928
err = PTR_ERR(reply);
1934
return genlmsg_reply(reply, info);
1942
static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1944
struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1945
struct datapath *dp;
1949
dp = get_dp(ovs_header->dp_ifindex);
1954
for (port_no = cb->args[0]; port_no < DP_MAX_PORTS; port_no++) {
1955
struct vport *vport;
1957
vport = rcu_dereference(dp->ports[port_no]);
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)
1968
cb->args[0] = port_no;
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
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
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
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,
1998
struct genl_family_and_ops {
1999
struct genl_family *family;
2000
struct genl_ops *ops;
2002
struct genl_multicast_group *group;
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),
2020
static void dp_unregister_genl(int n_families)
2024
for (i = 0; i < n_families; i++)
2025
genl_unregister_family(dp_genl_families[i].family);
2028
static int dp_register_genl(void)
2035
for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
2036
const struct genl_family_and_ops *f = &dp_genl_families[i];
2038
err = genl_register_family_with_ops(f->family, f->ops,
2045
err = genl_register_mc_group(f->family, f->group);
2054
dp_unregister_genl(n_registered);
2058
static int __init dp_init(void)
2060
struct sk_buff *dummy_skb;
2063
BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > sizeof(dummy_skb->cb));
2065
pr_info("Open vSwitch switching datapath %s, built "__DATE__" "__TIME__"\n",
2068
err = ovs_tnl_init();
2072
err = ovs_flow_init();
2074
goto error_tnl_exit;
2076
err = ovs_vport_init();
2078
goto error_flow_exit;
2080
err = register_netdevice_notifier(&ovs_dp_device_notifier);
2082
goto error_vport_exit;
2084
err = dp_register_genl();
2086
goto error_unreg_notifier;
2090
error_unreg_notifier:
2091
unregister_netdevice_notifier(&ovs_dp_device_notifier);
2102
static void dp_cleanup(void)
2105
dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2106
unregister_netdevice_notifier(&ovs_dp_device_notifier);
2112
module_init(dp_init);
2113
module_exit(dp_cleanup);
2115
MODULE_DESCRIPTION("Open vSwitch switching datapath");
2116
MODULE_LICENSE("GPL");