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