Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / net / vlan.c
1 /*
2  * Copyright (C) 2010 Michael Brown <mbrown@fensystems.co.uk>.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  */
19
20 FILE_LICENCE ( GPL2_OR_LATER );
21
22 #include <stdint.h>
23 #include <string.h>
24 #include <stdio.h>
25 #include <errno.h>
26 #include <byteswap.h>
27 #include <ipxe/features.h>
28 #include <ipxe/if_ether.h>
29 #include <ipxe/ethernet.h>
30 #include <ipxe/netdevice.h>
31 #include <ipxe/iobuf.h>
32 #include <ipxe/vlan.h>
33
34 /** @file
35  *
36  * Virtual LANs
37  *
38  */
39
40 FEATURE ( FEATURE_PROTOCOL, "VLAN", DHCP_EB_FEATURE_VLAN, 1 );
41
42 struct net_protocol vlan_protocol __net_protocol;
43
44 /** VLAN device private data */
45 struct vlan_device {
46         /** Trunk network device */
47         struct net_device *trunk;
48         /** VLAN tag */
49         unsigned int tag;
50         /** Default priority */
51         unsigned int priority;
52 };
53
54 /**
55  * Open VLAN device
56  *
57  * @v netdev            Network device
58  * @ret rc              Return status code
59  */
60 static int vlan_open ( struct net_device *netdev ) {
61         struct vlan_device *vlan = netdev->priv;
62
63         return netdev_open ( vlan->trunk );
64 }
65
66 /**
67  * Close VLAN device
68  *
69  * @v netdev            Network device
70  */
71 static void vlan_close ( struct net_device *netdev ) {
72         struct vlan_device *vlan = netdev->priv;
73
74         netdev_close ( vlan->trunk );
75 }
76
77 /**
78  * Transmit packet on VLAN device
79  *
80  * @v netdev            Network device
81  * @v iobuf             I/O buffer
82  * @ret rc              Return status code
83  */
84 static int vlan_transmit ( struct net_device *netdev,
85                            struct io_buffer *iobuf ) {
86         struct vlan_device *vlan = netdev->priv;
87         struct net_device *trunk = vlan->trunk;
88         struct ll_protocol *ll_protocol;
89         struct vlan_header *vlanhdr;
90         uint8_t ll_dest_copy[ETH_ALEN];
91         uint8_t ll_source_copy[ETH_ALEN];
92         const void *ll_dest;
93         const void *ll_source;
94         uint16_t net_proto;
95         unsigned int flags;
96         int rc;
97
98         /* Strip link-layer header and preserve link-layer header fields */
99         ll_protocol = netdev->ll_protocol;
100         if ( ( rc = ll_protocol->pull ( netdev, iobuf, &ll_dest, &ll_source,
101                                         &net_proto, &flags ) ) != 0 ) {
102                 DBGC ( netdev, "VLAN %s could not parse link-layer header: "
103                        "%s\n", netdev->name, strerror ( rc ) );
104                 return rc;
105         }
106         memcpy ( ll_dest_copy, ll_dest, ETH_ALEN );
107         memcpy ( ll_source_copy, ll_source, ETH_ALEN );
108
109         /* Construct VLAN header */
110         vlanhdr = iob_push ( iobuf, sizeof ( *vlanhdr ) );
111         vlanhdr->tci = htons ( VLAN_TCI ( vlan->tag, vlan->priority ) );
112         vlanhdr->net_proto = net_proto;
113
114         /* Reclaim I/O buffer from VLAN device's TX queue */
115         list_del ( &iobuf->list );
116
117         /* Transmit packet on trunk device */
118         if ( ( rc = net_tx ( iob_disown ( iobuf ), trunk, &vlan_protocol,
119                              ll_dest_copy, ll_source_copy ) ) != 0 ) {
120                 DBGC ( netdev, "VLAN %s could not transmit: %s\n",
121                        netdev->name, strerror ( rc ) );
122                 /* Cannot return an error status, since that would
123                  * cause the I/O buffer to be double-freed.
124                  */
125                 return 0;
126         }
127
128         return 0;
129 }
130
131 /**
132  * Poll VLAN device
133  *
134  * @v netdev            Network device
135  */
136 static void vlan_poll ( struct net_device *netdev ) {
137         struct vlan_device *vlan = netdev->priv;
138
139         /* Poll trunk device */
140         netdev_poll ( vlan->trunk );
141 }
142
143 /**
144  * Enable/disable interrupts on VLAN device
145  *
146  * @v netdev            Network device
147  * @v enable            Interrupts should be enabled
148  */
149 static void vlan_irq ( struct net_device *netdev, int enable ) {
150         struct vlan_device *vlan = netdev->priv;
151
152         /* Enable/disable interrupts on trunk device.  This is not at
153          * all robust, but there is no sensible course of action
154          * available.
155          */
156         netdev_irq ( vlan->trunk, enable );
157 }
158
159 /** VLAN device operations */
160 static struct net_device_operations vlan_operations = {
161         .open           = vlan_open,
162         .close          = vlan_close,
163         .transmit       = vlan_transmit,
164         .poll           = vlan_poll,
165         .irq            = vlan_irq,
166 };
167
168 /**
169  * Synchronise VLAN device
170  *
171  * @v netdev            Network device
172  */
173 static void vlan_sync ( struct net_device *netdev ) {
174         struct vlan_device *vlan = netdev->priv;
175         struct net_device *trunk = vlan->trunk;
176
177         /* Synchronise link status */
178         if ( netdev->link_rc != trunk->link_rc )
179                 netdev_link_err ( netdev, trunk->link_rc );
180
181         /* Synchronise open/closed status */
182         if ( netdev_is_open ( trunk ) ) {
183                 if ( ! netdev_is_open ( netdev ) )
184                         netdev_open ( netdev );
185         } else {
186                 if ( netdev_is_open ( netdev ) )
187                         netdev_close ( netdev );
188         }
189 }
190
191 /**
192  * Identify VLAN device
193  *
194  * @v trunk             Trunk network device
195  * @v tag               VLAN tag
196  * @ret netdev          VLAN device, if any
197  */
198 struct net_device * vlan_find ( struct net_device *trunk, unsigned int tag ) {
199         struct net_device *netdev;
200         struct vlan_device *vlan;
201
202         for_each_netdev ( netdev ) {
203                 if ( netdev->op != &vlan_operations )
204                         continue;
205                 vlan = netdev->priv;
206                 if ( ( vlan->trunk == trunk ) && ( vlan->tag == tag ) )
207                         return netdev;
208         }
209         return NULL;
210 }
211
212 /**
213  * Process incoming VLAN packet
214  *
215  * @v iobuf             I/O buffer
216  * @v trunk             Trunk network device
217  * @v ll_dest           Link-layer destination address
218  * @v ll_source         Link-layer source address
219  * @v flags             Packet flags
220  * @ret rc              Return status code
221  */
222 static int vlan_rx ( struct io_buffer *iobuf, struct net_device *trunk,
223                      const void *ll_dest, const void *ll_source,
224                      unsigned int flags __unused ) {
225         struct vlan_header *vlanhdr = iobuf->data;
226         struct net_device *netdev;
227         struct ll_protocol *ll_protocol;
228         uint8_t ll_dest_copy[ETH_ALEN];
229         uint8_t ll_source_copy[ETH_ALEN];
230         uint16_t tag;
231         int rc;
232
233         /* Sanity check */
234         if ( iob_len ( iobuf ) < sizeof ( *vlanhdr ) ) {
235                 DBGC ( trunk, "VLAN %s received underlength packet (%zd "
236                        "bytes)\n", trunk->name, iob_len ( iobuf ) );
237                 rc = -EINVAL;
238                 goto err_sanity;
239         }
240
241         /* Identify VLAN device */
242         tag = VLAN_TAG ( ntohs ( vlanhdr->tci ) );
243         netdev = vlan_find ( trunk, tag );
244         if ( ! netdev ) {
245                 DBGC2 ( trunk, "VLAN %s received packet for unknown VLAN "
246                         "%d\n", trunk->name, tag );
247                 rc = -EPIPE;
248                 goto err_no_vlan;
249         }
250
251         /* Strip VLAN header and preserve original link-layer header fields */
252         iob_pull ( iobuf, sizeof ( *vlanhdr ) );
253         ll_protocol = trunk->ll_protocol;
254         memcpy ( ll_dest_copy, ll_dest, ETH_ALEN );
255         memcpy ( ll_source_copy, ll_source, ETH_ALEN );
256
257         /* Reconstruct link-layer header for VLAN device */
258         ll_protocol = netdev->ll_protocol;
259         if ( ( rc = ll_protocol->push ( netdev, iobuf, ll_dest_copy,
260                                         ll_source_copy,
261                                         vlanhdr->net_proto ) ) != 0 ) {
262                 DBGC ( netdev, "VLAN %s could not reconstruct link-layer "
263                        "header: %s\n", netdev->name, strerror ( rc ) );
264                 goto err_ll_push;
265         }
266
267         /* Enqueue packet on VLAN device */
268         netdev_rx ( netdev, iob_disown ( iobuf ) );
269         return 0;
270
271  err_ll_push:
272  err_no_vlan:
273  err_sanity:
274         free_iob ( iobuf );
275         return rc;
276 }
277
278 /** VLAN protocol */
279 struct net_protocol vlan_protocol __net_protocol = {
280         .name = "VLAN",
281         .net_proto = htons ( ETH_P_8021Q ),
282         .rx = vlan_rx,
283 };
284
285 /**
286  * Get the VLAN tag
287  *
288  * @v netdev            Network device
289  * @ret tag             VLAN tag, or 0 if device is not a VLAN device
290  */
291 unsigned int vlan_tag ( struct net_device *netdev ) {
292         struct vlan_device *vlan;
293
294         if ( netdev->op == &vlan_operations ) {
295                 vlan = netdev->priv;
296                 return vlan->tag;
297         } else {
298                 return 0;
299         }
300 }
301
302 /**
303  * Check if network device can be used as a VLAN trunk device
304  *
305  * @v trunk             Trunk network device
306  * @ret is_ok           Trunk network device is usable
307  *
308  * VLAN devices will be created as Ethernet devices.  (We cannot
309  * simply clone the link layer of the trunk network device, because
310  * this link layer may expect the network device structure to contain
311  * some link-layer-private data.)  The trunk network device must
312  * therefore have a link layer that is in some sense 'compatible' with
313  * Ethernet; specifically, it must have link-layer addresses that are
314  * the same length as Ethernet link-layer addresses.
315  *
316  * As an additional check, and primarily to assist with the sanity of
317  * the FCoE code, we refuse to allow nested VLANs.
318  */
319 int vlan_can_be_trunk ( struct net_device *trunk ) {
320
321         return ( ( trunk->ll_protocol->ll_addr_len == ETH_ALEN ) &&
322                  ( trunk->op != &vlan_operations ) );
323 }
324
325 /**
326  * Create VLAN device
327  *
328  * @v trunk             Trunk network device
329  * @v tag               VLAN tag
330  * @v priority          Default VLAN priority
331  * @ret rc              Return status code
332  */
333 int vlan_create ( struct net_device *trunk, unsigned int tag,
334                   unsigned int priority ) {
335         struct net_device *netdev;
336         struct vlan_device *vlan;
337         int rc;
338
339         /* If VLAN already exists, just update the priority */
340         if ( ( netdev = vlan_find ( trunk, tag ) ) != NULL ) {
341                 vlan = netdev->priv;
342                 if ( priority != vlan->priority ) {
343                         DBGC ( netdev, "VLAN %s priority changed from %d to "
344                                "%d\n", netdev->name, vlan->priority, priority );
345                 }
346                 vlan->priority = priority;
347                 return 0;
348         }
349
350         /* Sanity checks */
351         if ( ! vlan_can_be_trunk ( trunk ) ) {
352                 DBGC ( trunk, "VLAN %s cannot create VLAN on non-trunk "
353                        "device\n", trunk->name );
354                 rc = -ENOTTY;
355                 goto err_sanity;
356         }
357         if ( ! VLAN_TAG_IS_VALID ( tag ) ) {
358                 DBGC ( trunk, "VLAN %s cannot create VLAN with invalid tag "
359                        "%d\n", trunk->name, tag );
360                 rc = -EINVAL;
361                 goto err_sanity;
362         }
363         if ( ! VLAN_PRIORITY_IS_VALID ( priority ) ) {
364                 DBGC ( trunk, "VLAN %s cannot create VLAN with invalid "
365                        "priority %d\n", trunk->name, priority );
366                 rc = -EINVAL;
367                 goto err_sanity;
368         }
369
370         /* Allocate and initialise structure */
371         netdev = alloc_etherdev ( sizeof ( *vlan ) );
372         if ( ! netdev ) {
373                 rc = -ENOMEM;
374                 goto err_alloc_etherdev;
375         }
376         netdev_init ( netdev, &vlan_operations );
377         netdev->dev = trunk->dev;
378         memcpy ( netdev->hw_addr, trunk->ll_addr, ETH_ALEN );
379         vlan = netdev->priv;
380         vlan->trunk = netdev_get ( trunk );
381         vlan->tag = tag;
382         vlan->priority = priority;
383
384         /* Construct VLAN device name */
385         snprintf ( netdev->name, sizeof ( netdev->name ), "%s-%d",
386                    trunk->name, vlan->tag );
387
388         /* Register VLAN device */
389         if ( ( rc = register_netdev ( netdev ) ) != 0 ) {
390                 DBGC ( netdev, "VLAN %s could not register: %s\n",
391                        netdev->name, strerror ( rc ) );
392                 goto err_register;
393         }
394
395         /* Synchronise with trunk device */
396         vlan_sync ( netdev );
397
398         DBGC ( netdev, "VLAN %s created with tag %d and priority %d\n",
399                netdev->name, vlan->tag, vlan->priority );
400
401         return 0;
402
403         unregister_netdev ( netdev );
404  err_register:
405         netdev_nullify ( netdev );
406         netdev_put ( netdev );
407         netdev_put ( trunk );
408  err_alloc_etherdev:
409  err_sanity:
410         return rc;
411 }
412
413 /**
414  * Destroy VLAN device
415  *
416  * @v netdev            Network device
417  * @ret rc              Return status code
418  */
419 int vlan_destroy ( struct net_device *netdev ) {
420         struct vlan_device *vlan = netdev->priv;
421         struct net_device *trunk;
422
423         /* Sanity check */
424         if ( netdev->op != &vlan_operations ) {
425                 DBGC ( netdev, "VLAN %s cannot destroy non-VLAN device\n",
426                        netdev->name );
427                 return -ENOTTY;
428         }
429
430         DBGC ( netdev, "VLAN %s destroyed\n", netdev->name );
431
432         /* Remove VLAN device */
433         unregister_netdev ( netdev );
434         trunk = vlan->trunk;
435         netdev_nullify ( netdev );
436         netdev_put ( netdev );
437         netdev_put ( trunk );
438
439         return 0;
440 }
441
442 /**
443  * Handle trunk network device link state change
444  *
445  * @v trunk             Trunk network device
446  */
447 static void vlan_notify ( struct net_device *trunk ) {
448         struct net_device *netdev;
449         struct vlan_device *vlan;
450
451         for_each_netdev ( netdev ) {
452                 if ( netdev->op != &vlan_operations )
453                         continue;
454                 vlan = netdev->priv;
455                 if ( vlan->trunk == trunk )
456                         vlan_sync ( netdev );
457         }
458 }
459
460 /**
461  * Destroy first VLAN device for a given trunk
462  *
463  * @v trunk             Trunk network device
464  * @ret found           A VLAN device was found
465  */
466 static int vlan_remove_first ( struct net_device *trunk ) {
467         struct net_device *netdev;
468         struct vlan_device *vlan;
469
470         for_each_netdev ( netdev ) {
471                 if ( netdev->op != &vlan_operations )
472                         continue;
473                 vlan = netdev->priv;
474                 if ( vlan->trunk == trunk ) {
475                         vlan_destroy ( netdev );
476                         return 1;
477                 }
478         }
479         return 0;
480 }
481
482 /**
483  * Destroy all VLAN devices for a given trunk
484  *
485  * @v trunk             Trunk network device
486  */
487 static void vlan_remove ( struct net_device *trunk ) {
488
489         /* Remove all VLAN devices attached to this trunk, safe
490          * against arbitrary net device removal.
491          */
492         while ( vlan_remove_first ( trunk ) ) {}
493 }
494
495 /** VLAN driver */
496 struct net_driver vlan_driver __net_driver = {
497         .name = "VLAN",
498         .notify = vlan_notify,
499         .remove = vlan_remove,
500 };