]> git.proxmox.com Git - ovs.git/blame - datapath/vport.c
datapath: Increase maximum number of datapath ports.
[ovs.git] / datapath / vport.c
CommitLineData
f2459fe7 1/*
2a4999f3 2 * Copyright (c) 2007-2012 Nicira Networks.
f2459fe7 3 *
a9a29d22
JG
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of version 2 of the GNU General Public
6 * License as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 * 02110-1301, USA
f2459fe7
JG
17 */
18
f2459fe7
JG
19#include <linux/etherdevice.h>
20#include <linux/if.h>
38aeef15 21#include <linux/if_vlan.h>
2a4999f3 22#include <linux/jhash.h>
f2459fe7
JG
23#include <linux/kernel.h>
24#include <linux/list.h>
25#include <linux/mutex.h>
26#include <linux/percpu.h>
057dd6d2 27#include <linux/rcupdate.h>
f2459fe7 28#include <linux/rtnetlink.h>
3fbd517a 29#include <linux/compat.h>
e90b1cf9 30#include <linux/version.h>
2a4999f3 31#include <net/net_namespace.h>
f2459fe7 32
2a4999f3 33#include "datapath.h"
f2459fe7 34#include "vport.h"
b19e8815 35#include "vport-internal_dev.h"
f2459fe7 36
5953c70e
JG
37/* List of statically compiled vport implementations. Don't forget to also
38 * add yours to the list at the bottom of vport.h. */
b279fccf 39static const struct vport_ops *base_vport_ops_list[] = {
850b6b3b
JG
40 &ovs_netdev_vport_ops,
41 &ovs_internal_vport_ops,
42 &ovs_patch_vport_ops,
43 &ovs_gre_vport_ops,
e90b1cf9 44#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
850b6b3b 45 &ovs_capwap_vport_ops,
e90b1cf9 46#endif
f2459fe7
JG
47};
48
49static const struct vport_ops **vport_ops_list;
50static int n_vport_types;
51
057dd6d2 52/* Protected by RCU read lock for reading, RTNL lock for writing. */
f2459fe7
JG
53static struct hlist_head *dev_table;
54#define VPORT_HASH_BUCKETS 1024
55
f2459fe7 56/**
850b6b3b 57 * ovs_vport_init - initialize vport subsystem
f2459fe7
JG
58 *
59 * Called at module load time to initialize the vport subsystem and any
60 * compiled in vport types.
61 */
850b6b3b 62int ovs_vport_init(void)
f2459fe7
JG
63{
64 int err;
65 int i;
66
67 dev_table = kzalloc(VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
68 GFP_KERNEL);
69 if (!dev_table) {
70 err = -ENOMEM;
71 goto error;
72 }
73
74 vport_ops_list = kmalloc(ARRAY_SIZE(base_vport_ops_list) *
75 sizeof(struct vport_ops *), GFP_KERNEL);
76 if (!vport_ops_list) {
77 err = -ENOMEM;
78 goto error_dev_table;
79 }
80
81 for (i = 0; i < ARRAY_SIZE(base_vport_ops_list); i++) {
b279fccf 82 const struct vport_ops *new_ops = base_vport_ops_list[i];
f2459fe7 83
f2459fe7
JG
84 if (new_ops->init)
85 err = new_ops->init();
86 else
87 err = 0;
88
89 if (!err)
90 vport_ops_list[n_vport_types++] = new_ops;
91 else if (new_ops->flags & VPORT_F_REQUIRED) {
850b6b3b 92 ovs_vport_exit();
f2459fe7
JG
93 goto error;
94 }
95 }
96
97 return 0;
98
99error_dev_table:
100 kfree(dev_table);
101error:
102 return err;
103}
104
f2459fe7 105/**
850b6b3b 106 * ovs_vport_exit - shutdown vport subsystem
f2459fe7
JG
107 *
108 * Called at module exit time to shutdown the vport subsystem and any
109 * initialized vport types.
110 */
850b6b3b 111void ovs_vport_exit(void)
f2459fe7
JG
112{
113 int i;
114
f2459fe7
JG
115 for (i = 0; i < n_vport_types; i++) {
116 if (vport_ops_list[i]->exit)
117 vport_ops_list[i]->exit();
118 }
119
120 kfree(vport_ops_list);
121 kfree(dev_table);
122}
123
2a4999f3 124static struct hlist_head *hash_bucket(struct net *net, const char *name)
f2459fe7 125{
2a4999f3 126 unsigned int hash = jhash(name, strlen(name), (unsigned long) net);
f2459fe7
JG
127 return &dev_table[hash & (VPORT_HASH_BUCKETS - 1)];
128}
129
130/**
850b6b3b 131 * ovs_vport_locate - find a port that has already been created
f2459fe7
JG
132 *
133 * @name: name of port to find
134 *
057dd6d2 135 * Must be called with RTNL or RCU read lock.
f2459fe7 136 */
2a4999f3 137struct vport *ovs_vport_locate(struct net *net, const char *name)
f2459fe7 138{
2a4999f3 139 struct hlist_head *bucket = hash_bucket(net, name);
f2459fe7
JG
140 struct vport *vport;
141 struct hlist_node *node;
142
057dd6d2 143 hlist_for_each_entry_rcu(vport, node, bucket, hash_node)
2a4999f3
PS
144 if (!strcmp(name, vport->ops->get_name(vport)) &&
145 net_eq(ovs_dp_get_net(vport->dp), net))
057dd6d2 146 return vport;
f2459fe7 147
057dd6d2 148 return NULL;
f2459fe7
JG
149}
150
e779d8d9
BP
151static void release_vport(struct kobject *kobj)
152{
153 struct vport *p = container_of(kobj, struct vport, kobj);
154 kfree(p);
155}
156
157static struct kobj_type brport_ktype = {
158#ifdef CONFIG_SYSFS
850b6b3b 159 .sysfs_ops = &ovs_brport_sysfs_ops,
e779d8d9
BP
160#endif
161 .release = release_vport
162};
163
f2459fe7 164/**
850b6b3b 165 * ovs_vport_alloc - allocate and initialize new vport
f2459fe7
JG
166 *
167 * @priv_size: Size of private data area to allocate.
168 * @ops: vport device ops
169 *
170 * Allocate and initialize a new vport defined by @ops. The vport will contain
171 * a private data area of size @priv_size that can be accessed using
172 * vport_priv(). vports that are no longer needed should be released with
850b6b3b 173 * ovs_vport_free().
f2459fe7 174 */
850b6b3b
JG
175struct vport *ovs_vport_alloc(int priv_size, const struct vport_ops *ops,
176 const struct vport_parms *parms)
f2459fe7
JG
177{
178 struct vport *vport;
179 size_t alloc_size;
180
181 alloc_size = sizeof(struct vport);
182 if (priv_size) {
183 alloc_size = ALIGN(alloc_size, VPORT_ALIGN);
184 alloc_size += priv_size;
185 }
186
187 vport = kzalloc(alloc_size, GFP_KERNEL);
188 if (!vport)
189 return ERR_PTR(-ENOMEM);
190
e779d8d9
BP
191 vport->dp = parms->dp;
192 vport->port_no = parms->port_no;
b063d9f0 193 vport->upcall_pid = parms->upcall_pid;
f2459fe7 194 vport->ops = ops;
95b1d73a 195 INIT_HLIST_NODE(&vport->dp_hash_node);
f2459fe7 196
e779d8d9
BP
197 /* Initialize kobject for bridge. This will be added as
198 * /sys/class/net/<devname>/brport later, if sysfs is enabled. */
199 vport->kobj.kset = NULL;
200 kobject_init(&vport->kobj, &brport_ktype);
201
f613a0d7 202 vport->percpu_stats = alloc_percpu(struct vport_percpu_stats);
9697e433
DC
203 if (!vport->percpu_stats) {
204 kfree(vport);
f613a0d7 205 return ERR_PTR(-ENOMEM);
9697e433 206 }
f2459fe7 207
f613a0d7 208 spin_lock_init(&vport->stats_lock);
f2459fe7
JG
209
210 return vport;
211}
212
213/**
850b6b3b 214 * ovs_vport_free - uninitialize and free vport
f2459fe7
JG
215 *
216 * @vport: vport to free
217 *
850b6b3b 218 * Frees a vport allocated with ovs_vport_alloc() when it is no longer needed.
057dd6d2
BP
219 *
220 * The caller must ensure that an RCU grace period has passed since the last
221 * time @vport was in a datapath.
f2459fe7 222 */
850b6b3b 223void ovs_vport_free(struct vport *vport)
f2459fe7 224{
f613a0d7 225 free_percpu(vport->percpu_stats);
f2459fe7 226
e779d8d9 227 kobject_put(&vport->kobj);
f2459fe7
JG
228}
229
230/**
850b6b3b 231 * ovs_vport_add - add vport device (for kernel callers)
f2459fe7 232 *
94903c98 233 * @parms: Information about new vport.
f2459fe7 234 *
7237e4f4 235 * Creates a new vport with the specified configuration (which is dependent on
91dbd46c 236 * device type). RTNL lock must be held.
f2459fe7 237 */
850b6b3b 238struct vport *ovs_vport_add(const struct vport_parms *parms)
f2459fe7
JG
239{
240 struct vport *vport;
241 int err = 0;
242 int i;
243
244 ASSERT_RTNL();
f2459fe7
JG
245
246 for (i = 0; i < n_vport_types; i++) {
c283069c 247 if (vport_ops_list[i]->type == parms->type) {
2a4999f3
PS
248 struct hlist_head *bucket;
249
94903c98 250 vport = vport_ops_list[i]->create(parms);
f2459fe7
JG
251 if (IS_ERR(vport)) {
252 err = PTR_ERR(vport);
253 goto out;
254 }
255
2a4999f3
PS
256 bucket = hash_bucket(ovs_dp_get_net(vport->dp),
257 vport->ops->get_name(vport));
258 hlist_add_head_rcu(&vport->hash_node, bucket);
f2459fe7
JG
259 return vport;
260 }
261 }
262
263 err = -EAFNOSUPPORT;
264
265out:
266 return ERR_PTR(err);
267}
268
269/**
850b6b3b 270 * ovs_vport_set_options - modify existing vport device (for kernel callers)
f2459fe7
JG
271 *
272 * @vport: vport to modify.
c3827f61 273 * @port: New configuration.
f2459fe7
JG
274 *
275 * Modifies an existing device with the specified configuration (which is
c19e6535 276 * dependent on device type). RTNL lock must be held.
f2459fe7 277 */
850b6b3b 278int ovs_vport_set_options(struct vport *vport, struct nlattr *options)
f2459fe7
JG
279{
280 ASSERT_RTNL();
f2459fe7 281
c19e6535 282 if (!vport->ops->set_options)
f2459fe7 283 return -EOPNOTSUPP;
c19e6535 284 return vport->ops->set_options(vport, options);
f2459fe7
JG
285}
286
287/**
850b6b3b 288 * ovs_vport_del - delete existing vport device
f2459fe7
JG
289 *
290 * @vport: vport to delete.
291 *
7237e4f4 292 * Detaches @vport from its datapath and destroys it. It is possible to fail
057dd6d2 293 * for reasons such as lack of memory. RTNL lock must be held.
f2459fe7 294 */
850b6b3b 295void ovs_vport_del(struct vport *vport)
f2459fe7
JG
296{
297 ASSERT_RTNL();
f2459fe7 298
057dd6d2 299 hlist_del_rcu(&vport->hash_node);
f2459fe7 300
3544358a 301 vport->ops->destroy(vport);
f2459fe7
JG
302}
303
f2459fe7 304/**
850b6b3b 305 * ovs_vport_set_addr - set device Ethernet address (for kernel callers)
f2459fe7
JG
306 *
307 * @vport: vport on which to set Ethernet address.
308 * @addr: New address.
309 *
310 * Sets the Ethernet address of the given device. Some devices may not support
311 * setting the Ethernet address, in which case the result will always be
312 * -EOPNOTSUPP. RTNL lock must be held.
313 */
850b6b3b 314int ovs_vport_set_addr(struct vport *vport, const unsigned char *addr)
f2459fe7
JG
315{
316 ASSERT_RTNL();
317
318 if (!is_valid_ether_addr(addr))
319 return -EADDRNOTAVAIL;
320
321 if (vport->ops->set_addr)
322 return vport->ops->set_addr(vport, addr);
323 else
324 return -EOPNOTSUPP;
325}
326
780e6207 327/**
850b6b3b 328 * ovs_vport_set_stats - sets offset device stats
780e6207
JG
329 *
330 * @vport: vport on which to set stats
331 * @stats: stats to set
332 *
333 * Provides a set of transmit, receive, and error stats to be added as an
334 * offset to the collect data when stats are retreived. Some devices may not
335 * support setting the stats, in which case the result will always be
ed099e92
BP
336 * -EOPNOTSUPP.
337 *
338 * Must be called with RTNL lock.
780e6207 339 */
850b6b3b 340void ovs_vport_set_stats(struct vport *vport, struct ovs_vport_stats *stats)
780e6207
JG
341{
342 ASSERT_RTNL();
343
f613a0d7
PS
344 spin_lock_bh(&vport->stats_lock);
345 vport->offset_stats = *stats;
346 spin_unlock_bh(&vport->stats_lock);
780e6207
JG
347}
348
780e6207 349/**
850b6b3b 350 * ovs_vport_get_stats - retrieve device stats
780e6207
JG
351 *
352 * @vport: vport from which to retrieve the stats
353 * @stats: location to store stats
354 *
355 * Retrieves transmit, receive, and error stats for the given device.
ed099e92
BP
356 *
357 * Must be called with RTNL lock or rcu_read_lock.
780e6207 358 */
850b6b3b 359void ovs_vport_get_stats(struct vport *vport, struct ovs_vport_stats *stats)
780e6207 360{
bc22fc3e 361 int i;
780e6207 362
bc22fc3e
BP
363 /* We potentially have 3 sources of stats that need to be
364 * combined: those we have collected (split into err_stats and
365 * percpu_stats), offset_stats from set_stats(), and device
f613a0d7 366 * error stats from netdev->get_stats() (for errors that happen
bc22fc3e 367 * downstream and therefore aren't reported through our
f613a0d7
PS
368 * vport_record_error() function).
369 * Stats from first two sources are merged and reported by ovs over
370 * OVS_VPORT_ATTR_STATS.
371 * netdev-stats can be directly read over netlink-ioctl.
372 */
780e6207 373
bc22fc3e 374 spin_lock_bh(&vport->stats_lock);
780e6207 375
bc22fc3e 376 *stats = vport->offset_stats;
780e6207 377
bc22fc3e
BP
378 stats->rx_errors += vport->err_stats.rx_errors;
379 stats->tx_errors += vport->err_stats.tx_errors;
380 stats->tx_dropped += vport->err_stats.tx_dropped;
381 stats->rx_dropped += vport->err_stats.rx_dropped;
780e6207 382
bc22fc3e 383 spin_unlock_bh(&vport->stats_lock);
780e6207 384
bc22fc3e
BP
385 for_each_possible_cpu(i) {
386 const struct vport_percpu_stats *percpu_stats;
387 struct vport_percpu_stats local_stats;
821cb9fa 388 unsigned int start;
780e6207 389
bc22fc3e 390 percpu_stats = per_cpu_ptr(vport->percpu_stats, i);
38c6ecbc 391
bc22fc3e 392 do {
821cb9fa 393 start = u64_stats_fetch_begin_bh(&percpu_stats->sync);
bc22fc3e 394 local_stats = *percpu_stats;
821cb9fa 395 } while (u64_stats_fetch_retry_bh(&percpu_stats->sync, start));
38c6ecbc 396
bc22fc3e
BP
397 stats->rx_bytes += local_stats.rx_bytes;
398 stats->rx_packets += local_stats.rx_packets;
399 stats->tx_bytes += local_stats.tx_bytes;
400 stats->tx_packets += local_stats.tx_packets;
265d5020 401 }
780e6207
JG
402}
403
dd851cbb 404/**
850b6b3b 405 * ovs_vport_get_options - retrieve device options
dd851cbb 406 *
c19e6535
BP
407 * @vport: vport from which to retrieve the options.
408 * @skb: sk_buff where options should be appended.
dd851cbb 409 *
c19e6535 410 * Retrieves the configuration of the given device, appending an
df2c07f4 411 * %OVS_VPORT_ATTR_OPTIONS attribute that in turn contains nested
ed099e92 412 * vport-specific attributes to @skb.
c19e6535
BP
413 *
414 * Returns 0 if successful, -EMSGSIZE if @skb has insufficient room, or another
f0fef760
BP
415 * negative error code if a real error occurred. If an error occurs, @skb is
416 * left unmodified.
ed099e92
BP
417 *
418 * Must be called with RTNL lock or rcu_read_lock.
dd851cbb 419 */
850b6b3b 420int ovs_vport_get_options(const struct vport *vport, struct sk_buff *skb)
dd851cbb 421{
c19e6535
BP
422 struct nlattr *nla;
423
df2c07f4 424 nla = nla_nest_start(skb, OVS_VPORT_ATTR_OPTIONS);
c19e6535
BP
425 if (!nla)
426 return -EMSGSIZE;
427
428 if (vport->ops->get_options) {
429 int err = vport->ops->get_options(vport, skb);
f0fef760
BP
430 if (err) {
431 nla_nest_cancel(skb, nla);
c19e6535 432 return err;
f0fef760 433 }
c19e6535
BP
434 }
435
436 nla_nest_end(skb, nla);
437 return 0;
dd851cbb
JP
438}
439
f2459fe7 440/**
850b6b3b 441 * ovs_vport_receive - pass up received packet to the datapath for processing
f2459fe7
JG
442 *
443 * @vport: vport that received the packet
444 * @skb: skb that was received
445 *
8819fac7
JG
446 * Must be called with rcu_read_lock. The packet cannot be shared and
447 * skb->data should point to the Ethernet header. The caller must have already
448 * called compute_ip_summed() to initialize the checksumming fields.
f2459fe7 449 */
850b6b3b 450void ovs_vport_receive(struct vport *vport, struct sk_buff *skb)
f2459fe7 451{
f613a0d7 452 struct vport_percpu_stats *stats;
f2459fe7 453
f613a0d7 454 stats = per_cpu_ptr(vport->percpu_stats, smp_processor_id());
38c6ecbc 455
821cb9fa 456 u64_stats_update_begin(&stats->sync);
f613a0d7
PS
457 stats->rx_packets++;
458 stats->rx_bytes += skb->len;
821cb9fa 459 u64_stats_update_end(&stats->sync);
f2459fe7 460
3976f6d5
JG
461 if (!(vport->ops->flags & VPORT_F_FLOW))
462 OVS_CB(skb)->flow = NULL;
463
f2459fe7
JG
464 if (!(vport->ops->flags & VPORT_F_TUN_ID))
465 OVS_CB(skb)->tun_id = 0;
466
850b6b3b 467 ovs_dp_process_received_packet(vport, skb);
f2459fe7
JG
468}
469
470/**
850b6b3b 471 * ovs_vport_send - send a packet on a device
f2459fe7
JG
472 *
473 * @vport: vport on which to send the packet
474 * @skb: skb to send
475 *
476 * Sends the given packet and returns the length of data sent. Either RTNL
477 * lock or rcu_read_lock must be held.
478 */
850b6b3b 479int ovs_vport_send(struct vport *vport, struct sk_buff *skb)
f2459fe7 480{
24b019f8 481 int sent = vport->ops->send(vport, skb);
f2459fe7 482
58d01ad9
PS
483 if (likely(sent)) {
484 struct vport_percpu_stats *stats;
38c6ecbc 485
58d01ad9 486 stats = per_cpu_ptr(vport->percpu_stats, smp_processor_id());
f2459fe7 487
821cb9fa 488 u64_stats_update_begin(&stats->sync);
58d01ad9
PS
489 stats->tx_packets++;
490 stats->tx_bytes += sent;
821cb9fa 491 u64_stats_update_end(&stats->sync);
58d01ad9 492 }
55574bb0 493 return sent;
f2459fe7
JG
494}
495
496/**
850b6b3b 497 * ovs_vport_record_error - indicate device error to generic stats layer
f2459fe7
JG
498 *
499 * @vport: vport that encountered the error
500 * @err_type: one of enum vport_err_type types to indicate the error type
501 *
502 * If using the vport generic stats layer indicate that an error of the given
503 * type has occured.
504 */
850b6b3b 505void ovs_vport_record_error(struct vport *vport, enum vport_err_type err_type)
f2459fe7 506{
e9141eec 507 spin_lock(&vport->stats_lock);
f2459fe7 508
f613a0d7
PS
509 switch (err_type) {
510 case VPORT_E_RX_DROPPED:
511 vport->err_stats.rx_dropped++;
512 break;
f2459fe7 513
f613a0d7
PS
514 case VPORT_E_RX_ERROR:
515 vport->err_stats.rx_errors++;
516 break;
f2459fe7 517
f613a0d7
PS
518 case VPORT_E_TX_DROPPED:
519 vport->err_stats.tx_dropped++;
520 break;
f2459fe7 521
f613a0d7
PS
522 case VPORT_E_TX_ERROR:
523 vport->err_stats.tx_errors++;
524 break;
525 };
f2459fe7 526
e9141eec 527 spin_unlock(&vport->stats_lock);
f2459fe7 528}