1 /*
2 * Copyright (C) 2006 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 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
22 */
23
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25
26 #include <stdint.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <byteswap.h>
30 #include <string.h>
31 #include <errno.h>
32 #include <config/general.h>
33 #include <ipxe/if_ether.h>
34 #include <ipxe/iobuf.h>
35 #include <ipxe/tables.h>
36 #include <ipxe/process.h>
37 #include <ipxe/init.h>
38 #include <ipxe/malloc.h>
39 #include <ipxe/device.h>
40 #include <ipxe/errortab.h>
41 #include <ipxe/profile.h>
42 #include <ipxe/fault.h>
43 #include <ipxe/vlan.h>
44 #include <ipxe/netdevice.h>
45
46 /** @file
47 *
48 * Network device management
49 *
50 */
51
52 /** List of network devices */
53 struct list_head net_devices = LIST_HEAD_INIT ( net_devices );
54
55 /** List of open network devices, in reverse order of opening */
56 static struct list_head open_net_devices = LIST_HEAD_INIT ( open_net_devices );
57
58 /** Network device index */
59 static unsigned int netdev_index = 0;
60
61 /** Network polling profiler */
62 static struct profiler net_poll_profiler __profiler = { .name = "net.poll" };
63
64 /** Network receive profiler */
65 static struct profiler net_rx_profiler __profiler = { .name = "net.rx" };
66
67 /** Network transmit profiler */
68 static struct profiler net_tx_profiler __profiler = { .name = "net.tx" };
69
70 /** Default unknown link status code */
71 #define EUNKNOWN_LINK_STATUS __einfo_error ( EINFO_EUNKNOWN_LINK_STATUS )
72 #define EINFO_EUNKNOWN_LINK_STATUS \
73 __einfo_uniqify ( EINFO_EINPROGRESS, 0x01, "Unknown" )
74
75 /** Default not-yet-attempted-configuration status code */
76 #define EUNUSED_CONFIG __einfo_error ( EINFO_EUNUSED_CONFIG )
77 #define EINFO_EUNUSED_CONFIG \
78 __einfo_uniqify ( EINFO_EINPROGRESS, 0x02, "Unused" )
79
80 /** Default configuration-in-progress status code */
81 #define EINPROGRESS_CONFIG __einfo_error ( EINFO_EINPROGRESS_CONFIG )
82 #define EINFO_EINPROGRESS_CONFIG \
83 __einfo_uniqify ( EINFO_EINPROGRESS, 0x03, "Incomplete" )
84
85 /** Default link-down status code */
86 #define ENOTCONN_LINK_DOWN __einfo_error ( EINFO_ENOTCONN_LINK_DOWN )
87 #define EINFO_ENOTCONN_LINK_DOWN \
88 __einfo_uniqify ( EINFO_ENOTCONN, 0x01, "Down" )
89
90 /** Human-readable message for the default link statuses */
91 struct errortab netdev_errors[] __errortab = {
92 __einfo_errortab ( EINFO_EUNKNOWN_LINK_STATUS ),
93 __einfo_errortab ( EINFO_ENOTCONN_LINK_DOWN ),
94 __einfo_errortab ( EINFO_EUNUSED_CONFIG ),
95 __einfo_errortab ( EINFO_EINPROGRESS_CONFIG ),
96 };
97
98 /**
99 * Check whether or not network device has a link-layer address
100 *
101 * @v netdev Network device
102 * @ret has_ll_addr Network device has a link-layer address
103 */
netdev_has_ll_addr(struct net_device * netdev)104 static int netdev_has_ll_addr ( struct net_device *netdev ) {
105 uint8_t *ll_addr = netdev->ll_addr;
106 size_t remaining = sizeof ( netdev->ll_addr );
107
108 while ( remaining-- ) {
109 if ( *(ll_addr++) != 0 )
110 return 1;
111 }
112 return 0;
113 }
114
115 /**
116 * Notify drivers of network device or link state change
117 *
118 * @v netdev Network device
119 */
netdev_notify(struct net_device * netdev)120 static void netdev_notify ( struct net_device *netdev ) {
121 struct net_driver *driver;
122
123 for_each_table_entry ( driver, NET_DRIVERS ) {
124 if ( driver->notify )
125 driver->notify ( netdev );
126 }
127 }
128
129 /**
130 * Freeze network device receive queue processing
131 *
132 * @v netdev Network device
133 */
netdev_rx_freeze(struct net_device * netdev)134 void netdev_rx_freeze ( struct net_device *netdev ) {
135
136 /* Mark receive queue processing as frozen */
137 netdev->state |= NETDEV_RX_FROZEN;
138
139 /* Notify drivers of change */
140 netdev_notify ( netdev );
141 }
142
143 /**
144 * Unfreeze network device receive queue processing
145 *
146 * @v netdev Network device
147 */
netdev_rx_unfreeze(struct net_device * netdev)148 void netdev_rx_unfreeze ( struct net_device *netdev ) {
149
150 /* Mark receive queue processing as not frozen */
151 netdev->state &= ~NETDEV_RX_FROZEN;
152
153 /* Notify drivers of change */
154 netdev_notify ( netdev );
155 }
156
157 /**
158 * Mark network device as having a specific link state
159 *
160 * @v netdev Network device
161 * @v rc Link status code
162 */
netdev_link_err(struct net_device * netdev,int rc)163 void netdev_link_err ( struct net_device *netdev, int rc ) {
164
165 /* Stop link block timer */
166 stop_timer ( &netdev->link_block );
167
168 /* Record link state */
169 netdev->link_rc = rc;
170 if ( netdev->link_rc == 0 ) {
171 DBGC ( netdev, "NETDEV %s link is up\n", netdev->name );
172 } else {
173 DBGC ( netdev, "NETDEV %s link is down: %s\n",
174 netdev->name, strerror ( netdev->link_rc ) );
175 }
176
177 /* Notify drivers of link state change */
178 netdev_notify ( netdev );
179 }
180
181 /**
182 * Mark network device as having link down
183 *
184 * @v netdev Network device
185 */
netdev_link_down(struct net_device * netdev)186 void netdev_link_down ( struct net_device *netdev ) {
187
188 /* Avoid clobbering a more detailed link status code, if one
189 * is already set.
190 */
191 if ( ( netdev->link_rc == 0 ) ||
192 ( netdev->link_rc == -EUNKNOWN_LINK_STATUS ) ) {
193 netdev_link_err ( netdev, -ENOTCONN_LINK_DOWN );
194 }
195 }
196
197 /**
198 * Mark network device link as being blocked
199 *
200 * @v netdev Network device
201 * @v timeout Timeout (in ticks)
202 */
netdev_link_block(struct net_device * netdev,unsigned long timeout)203 void netdev_link_block ( struct net_device *netdev, unsigned long timeout ) {
204
205 /* Start link block timer */
206 if ( ! netdev_link_blocked ( netdev ) ) {
207 DBGC ( netdev, "NETDEV %s link blocked for %ld ticks\n",
208 netdev->name, timeout );
209 }
210 start_timer_fixed ( &netdev->link_block, timeout );
211 }
212
213 /**
214 * Mark network device link as being unblocked
215 *
216 * @v netdev Network device
217 */
netdev_link_unblock(struct net_device * netdev)218 void netdev_link_unblock ( struct net_device *netdev ) {
219
220 /* Stop link block timer */
221 if ( netdev_link_blocked ( netdev ) )
222 DBGC ( netdev, "NETDEV %s link unblocked\n", netdev->name );
223 stop_timer ( &netdev->link_block );
224 }
225
226 /**
227 * Handle network device link block timer expiry
228 *
229 * @v timer Link block timer
230 * @v fail Failure indicator
231 */
netdev_link_block_expired(struct retry_timer * timer,int fail __unused)232 static void netdev_link_block_expired ( struct retry_timer *timer,
233 int fail __unused ) {
234 struct net_device *netdev =
235 container_of ( timer, struct net_device, link_block );
236
237 /* Assume link is no longer blocked */
238 DBGC ( netdev, "NETDEV %s link block expired\n", netdev->name );
239 }
240
241 /**
242 * Record network device statistic
243 *
244 * @v stats Network device statistics
245 * @v rc Status code
246 */
netdev_record_stat(struct net_device_stats * stats,int rc)247 static void netdev_record_stat ( struct net_device_stats *stats, int rc ) {
248 struct net_device_error *error;
249 struct net_device_error *least_common_error;
250 unsigned int i;
251
252 /* If this is not an error, just update the good counter */
253 if ( rc == 0 ) {
254 stats->good++;
255 return;
256 }
257
258 /* Update the bad counter */
259 stats->bad++;
260
261 /* Locate the appropriate error record */
262 least_common_error = &stats->errors[0];
263 for ( i = 0 ; i < ( sizeof ( stats->errors ) /
264 sizeof ( stats->errors[0] ) ) ; i++ ) {
265 error = &stats->errors[i];
266 /* Update matching record, if found */
267 if ( error->rc == rc ) {
268 error->count++;
269 return;
270 }
271 if ( error->count < least_common_error->count )
272 least_common_error = error;
273 }
274
275 /* Overwrite the least common error record */
276 least_common_error->rc = rc;
277 least_common_error->count = 1;
278 }
279
280 /**
281 * Transmit raw packet via network device
282 *
283 * @v netdev Network device
284 * @v iobuf I/O buffer
285 * @ret rc Return status code
286 *
287 * Transmits the packet via the specified network device. This
288 * function takes ownership of the I/O buffer.
289 */
netdev_tx(struct net_device * netdev,struct io_buffer * iobuf)290 int netdev_tx ( struct net_device *netdev, struct io_buffer *iobuf ) {
291 int rc;
292
293 DBGC2 ( netdev, "NETDEV %s transmitting %p (%p+%zx)\n",
294 netdev->name, iobuf, iobuf->data, iob_len ( iobuf ) );
295 profile_start ( &net_tx_profiler );
296
297 /* Enqueue packet */
298 list_add_tail ( &iobuf->list, &netdev->tx_queue );
299
300 /* Avoid calling transmit() on unopened network devices */
301 if ( ! netdev_is_open ( netdev ) ) {
302 rc = -ENETUNREACH;
303 goto err;
304 }
305
306 /* Discard packet (for test purposes) if applicable */
307 if ( ( rc = inject_fault ( NETDEV_DISCARD_RATE ) ) != 0 )
308 goto err;
309
310 /* Transmit packet */
311 if ( ( rc = netdev->op->transmit ( netdev, iobuf ) ) != 0 )
312 goto err;
313
314 profile_stop ( &net_tx_profiler );
315 return 0;
316
317 err:
318 netdev_tx_complete_err ( netdev, iobuf, rc );
319 return rc;
320 }
321
322 /**
323 * Defer transmitted packet
324 *
325 * @v netdev Network device
326 * @v iobuf I/O buffer
327 *
328 * Drivers may call netdev_tx_defer() if there is insufficient space
329 * in the transmit descriptor ring. Any packets deferred in this way
330 * will be automatically retransmitted as soon as space becomes
331 * available (i.e. as soon as the driver calls netdev_tx_complete()).
332 *
333 * The packet must currently be in the network device's TX queue.
334 *
335 * Drivers utilising netdev_tx_defer() must ensure that space in the
336 * transmit descriptor ring is freed up @b before calling
337 * netdev_tx_complete(). For example, if the ring is modelled using a
338 * producer counter and a consumer counter, then the consumer counter
339 * must be incremented before the call to netdev_tx_complete().
340 * Failure to do this will cause the retransmitted packet to be
341 * immediately redeferred (which will result in out-of-order
342 * transmissions and other nastiness).
343 */
netdev_tx_defer(struct net_device * netdev,struct io_buffer * iobuf)344 void netdev_tx_defer ( struct net_device *netdev, struct io_buffer *iobuf ) {
345
346 /* Catch data corruption as early as possible */
347 list_check_contains_entry ( iobuf, &netdev->tx_queue, list );
348
349 /* Remove from transmit queue */
350 list_del ( &iobuf->list );
351
352 /* Add to deferred transmit queue */
353 list_add_tail ( &iobuf->list, &netdev->tx_deferred );
354
355 /* Record "out of space" statistic */
356 netdev_tx_err ( netdev, NULL, -ENOBUFS );
357 }
358
359 /**
360 * Discard transmitted packet
361 *
362 * @v netdev Network device
363 * @v iobuf I/O buffer, or NULL
364 * @v rc Packet status code
365 *
366 * The packet is discarded and a TX error is recorded. This function
367 * takes ownership of the I/O buffer.
368 */
netdev_tx_err(struct net_device * netdev,struct io_buffer * iobuf,int rc)369 void netdev_tx_err ( struct net_device *netdev,
370 struct io_buffer *iobuf, int rc ) {
371
372 /* Update statistics counter */
373 netdev_record_stat ( &netdev->tx_stats, rc );
374 if ( rc == 0 ) {
375 DBGC2 ( netdev, "NETDEV %s transmission %p complete\n",
376 netdev->name, iobuf );
377 } else {
378 DBGC ( netdev, "NETDEV %s transmission %p failed: %s\n",
379 netdev->name, iobuf, strerror ( rc ) );
380 }
381
382 /* Discard packet */
383 free_iob ( iobuf );
384 }
385
386 /**
387 * Complete network transmission
388 *
389 * @v netdev Network device
390 * @v iobuf I/O buffer
391 * @v rc Packet status code
392 *
393 * The packet must currently be in the network device's TX queue.
394 */
netdev_tx_complete_err(struct net_device * netdev,struct io_buffer * iobuf,int rc)395 void netdev_tx_complete_err ( struct net_device *netdev,
396 struct io_buffer *iobuf, int rc ) {
397
398 /* Catch data corruption as early as possible */
399 list_check_contains_entry ( iobuf, &netdev->tx_queue, list );
400
401 /* Dequeue and free I/O buffer */
402 list_del ( &iobuf->list );
403 netdev_tx_err ( netdev, iobuf, rc );
404
405 /* Transmit first pending packet, if any */
406 if ( ( iobuf = list_first_entry ( &netdev->tx_deferred,
407 struct io_buffer, list ) ) != NULL ) {
408 list_del ( &iobuf->list );
409 netdev_tx ( netdev, iobuf );
410 }
411 }
412
413 /**
414 * Complete network transmission
415 *
416 * @v netdev Network device
417 * @v rc Packet status code
418 *
419 * Completes the oldest outstanding packet in the TX queue.
420 */
netdev_tx_complete_next_err(struct net_device * netdev,int rc)421 void netdev_tx_complete_next_err ( struct net_device *netdev, int rc ) {
422 struct io_buffer *iobuf;
423
424 if ( ( iobuf = list_first_entry ( &netdev->tx_queue, struct io_buffer,
425 list ) ) != NULL ) {
426 netdev_tx_complete_err ( netdev, iobuf, rc );
427 }
428 }
429
430 /**
431 * Flush device's transmit queue
432 *
433 * @v netdev Network device
434 */
netdev_tx_flush(struct net_device * netdev)435 static void netdev_tx_flush ( struct net_device *netdev ) {
436
437 /* Discard any packets in the TX queue. This will also cause
438 * any packets in the deferred TX queue to be discarded
439 * automatically.
440 */
441 while ( ! list_empty ( &netdev->tx_queue ) ) {
442 netdev_tx_complete_next_err ( netdev, -ECANCELED );
443 }
444 assert ( list_empty ( &netdev->tx_queue ) );
445 assert ( list_empty ( &netdev->tx_deferred ) );
446 }
447
448 /**
449 * Add packet to receive queue
450 *
451 * @v netdev Network device
452 * @v iobuf I/O buffer, or NULL
453 *
454 * The packet is added to the network device's RX queue. This
455 * function takes ownership of the I/O buffer.
456 */
netdev_rx(struct net_device * netdev,struct io_buffer * iobuf)457 void netdev_rx ( struct net_device *netdev, struct io_buffer *iobuf ) {
458 int rc;
459
460 DBGC2 ( netdev, "NETDEV %s received %p (%p+%zx)\n",
461 netdev->name, iobuf, iobuf->data, iob_len ( iobuf ) );
462
463 /* Discard packet (for test purposes) if applicable */
464 if ( ( rc = inject_fault ( NETDEV_DISCARD_RATE ) ) != 0 ) {
465 netdev_rx_err ( netdev, iobuf, rc );
466 return;
467 }
468
469 /* Enqueue packet */
470 list_add_tail ( &iobuf->list, &netdev->rx_queue );
471
472 /* Update statistics counter */
473 netdev_record_stat ( &netdev->rx_stats, 0 );
474 }
475
476 /**
477 * Discard received packet
478 *
479 * @v netdev Network device
480 * @v iobuf I/O buffer, or NULL
481 * @v rc Packet status code
482 *
483 * The packet is discarded and an RX error is recorded. This function
484 * takes ownership of the I/O buffer. @c iobuf may be NULL if, for
485 * example, the net device wishes to report an error due to being
486 * unable to allocate an I/O buffer.
487 */
netdev_rx_err(struct net_device * netdev,struct io_buffer * iobuf,int rc)488 void netdev_rx_err ( struct net_device *netdev,
489 struct io_buffer *iobuf, int rc ) {
490
491 DBGC ( netdev, "NETDEV %s failed to receive %p: %s\n",
492 netdev->name, iobuf, strerror ( rc ) );
493
494 /* Discard packet */
495 free_iob ( iobuf );
496
497 /* Update statistics counter */
498 netdev_record_stat ( &netdev->rx_stats, rc );
499 }
500
501 /**
502 * Poll for completed and received packets on network device
503 *
504 * @v netdev Network device
505 *
506 * Polls the network device for completed transmissions and received
507 * packets. Any received packets will be added to the RX packet queue
508 * via netdev_rx().
509 */
netdev_poll(struct net_device * netdev)510 void netdev_poll ( struct net_device *netdev ) {
511
512 if ( netdev_is_open ( netdev ) )
513 netdev->op->poll ( netdev );
514 }
515
516 /**
517 * Remove packet from device's receive queue
518 *
519 * @v netdev Network device
520 * @ret iobuf I/O buffer, or NULL
521 *
522 * Removes the first packet from the device's RX queue and returns it.
523 * Ownership of the packet is transferred to the caller.
524 */
netdev_rx_dequeue(struct net_device * netdev)525 struct io_buffer * netdev_rx_dequeue ( struct net_device *netdev ) {
526 struct io_buffer *iobuf;
527
528 iobuf = list_first_entry ( &netdev->rx_queue, struct io_buffer, list );
529 if ( ! iobuf )
530 return NULL;
531
532 list_del ( &iobuf->list );
533 return iobuf;
534 }
535
536 /**
537 * Flush device's receive queue
538 *
539 * @v netdev Network device
540 */
netdev_rx_flush(struct net_device * netdev)541 static void netdev_rx_flush ( struct net_device *netdev ) {
542 struct io_buffer *iobuf;
543
544 /* Discard any packets in the RX queue */
545 while ( ( iobuf = netdev_rx_dequeue ( netdev ) ) ) {
546 netdev_rx_err ( netdev, iobuf, -ECANCELED );
547 }
548 }
549
550 /**
551 * Finish network device configuration
552 *
553 * @v config Network device configuration
554 * @v rc Reason for completion
555 */
netdev_config_close(struct net_device_configuration * config,int rc)556 static void netdev_config_close ( struct net_device_configuration *config,
557 int rc ) {
558 struct net_device_configurator *configurator = config->configurator;
559 struct net_device *netdev = config->netdev;
560
561 /* Restart interface */
562 intf_restart ( &config->job, rc );
563
564 /* Record configuration result */
565 config->rc = rc;
566 if ( rc == 0 ) {
567 DBGC ( netdev, "NETDEV %s configured via %s\n",
568 netdev->name, configurator->name );
569 } else {
570 DBGC ( netdev, "NETDEV %s configuration via %s failed: %s\n",
571 netdev->name, configurator->name, strerror ( rc ) );
572 }
573 }
574
575 /** Network device configuration interface operations */
576 static struct interface_operation netdev_config_ops[] = {
577 INTF_OP ( intf_close, struct net_device_configuration *,
578 netdev_config_close ),
579 };
580
581 /** Network device configuration interface descriptor */
582 static struct interface_descriptor netdev_config_desc =
583 INTF_DESC ( struct net_device_configuration, job, netdev_config_ops );
584
585 /**
586 * Free network device
587 *
588 * @v refcnt Network device reference counter
589 */
free_netdev(struct refcnt * refcnt)590 static void free_netdev ( struct refcnt *refcnt ) {
591 struct net_device *netdev =
592 container_of ( refcnt, struct net_device, refcnt );
593
594 stop_timer ( &netdev->link_block );
595 netdev_tx_flush ( netdev );
596 netdev_rx_flush ( netdev );
597 clear_settings ( netdev_settings ( netdev ) );
598 free ( netdev );
599 }
600
601 /**
602 * Allocate network device
603 *
604 * @v priv_len Length of private data area (net_device::priv)
605 * @ret netdev Network device, or NULL
606 *
607 * Allocates space for a network device and its private data area.
608 */
alloc_netdev(size_t priv_len)609 struct net_device * alloc_netdev ( size_t priv_len ) {
610 struct net_device *netdev;
611 struct net_device_configurator *configurator;
612 struct net_device_configuration *config;
613 unsigned int num_configs;
614 size_t confs_len;
615 size_t total_len;
616
617 num_configs = table_num_entries ( NET_DEVICE_CONFIGURATORS );
618 confs_len = ( num_configs * sizeof ( netdev->configs[0] ) );
619 total_len = ( sizeof ( *netdev ) + confs_len + priv_len );
620 netdev = zalloc ( total_len );
621 if ( netdev ) {
622 ref_init ( &netdev->refcnt, free_netdev );
623 netdev->link_rc = -EUNKNOWN_LINK_STATUS;
624 timer_init ( &netdev->link_block, netdev_link_block_expired,
625 &netdev->refcnt );
626 INIT_LIST_HEAD ( &netdev->tx_queue );
627 INIT_LIST_HEAD ( &netdev->tx_deferred );
628 INIT_LIST_HEAD ( &netdev->rx_queue );
629 netdev_settings_init ( netdev );
630 config = netdev->configs;
631 for_each_table_entry ( configurator, NET_DEVICE_CONFIGURATORS ){
632 config->netdev = netdev;
633 config->configurator = configurator;
634 config->rc = -EUNUSED_CONFIG;
635 intf_init ( &config->job, &netdev_config_desc,
636 &netdev->refcnt );
637 config++;
638 }
639 netdev->priv = ( ( ( void * ) netdev ) + sizeof ( *netdev ) +
640 confs_len );
641 }
642 return netdev;
643 }
644
645 /**
646 * Register network device
647 *
648 * @v netdev Network device
649 * @ret rc Return status code
650 *
651 * Gives the network device a name and adds it to the list of network
652 * devices.
653 */
register_netdev(struct net_device * netdev)654 int register_netdev ( struct net_device *netdev ) {
655 struct ll_protocol *ll_protocol = netdev->ll_protocol;
656 struct net_driver *driver;
657 struct net_device *duplicate;
658 uint32_t seed;
659 int rc;
660
661 /* Set initial link-layer address, if not already set */
662 if ( ! netdev_has_ll_addr ( netdev ) ) {
663 ll_protocol->init_addr ( netdev->hw_addr, netdev->ll_addr );
664 }
665
666 /* Set MTU, if not already set */
667 if ( ! netdev->mtu ) {
668 netdev->mtu = ( netdev->max_pkt_len -
669 ll_protocol->ll_header_len );
670 }
671
672 /* Reject network devices that are already available via a
673 * different hardware device.
674 */
675 duplicate = find_netdev_by_ll_addr ( ll_protocol, netdev->ll_addr );
676 if ( duplicate && ( duplicate->dev != netdev->dev ) ) {
677 DBGC ( netdev, "NETDEV rejecting duplicate (phys %s) of %s "
678 "(phys %s)\n", netdev->dev->name, duplicate->name,
679 duplicate->dev->name );
680 rc = -EEXIST;
681 goto err_duplicate;
682 }
683
684 /* Reject named network devices that already exist */
685 if ( netdev->name[0] && ( duplicate = find_netdev ( netdev->name ) ) ) {
686 DBGC ( netdev, "NETDEV rejecting duplicate name %s\n",
687 duplicate->name );
688 rc = -EEXIST;
689 goto err_duplicate;
690 }
691
692 /* Record device index and create device name */
693 if ( netdev->name[0] == '\0' ) {
694 snprintf ( netdev->name, sizeof ( netdev->name ), "net%d",
695 netdev_index );
696 }
697 netdev->index = ++netdev_index;
698
699 /* Use least significant bits of the link-layer address to
700 * improve the randomness of the (non-cryptographic) random
701 * number generator.
702 */
703 memcpy ( &seed, ( netdev->ll_addr + ll_protocol->ll_addr_len
704 - sizeof ( seed ) ), sizeof ( seed ) );
705 srand ( rand() ^ seed );
706
707 /* Add to device list */
708 netdev_get ( netdev );
709 list_add_tail ( &netdev->list, &net_devices );
710 DBGC ( netdev, "NETDEV %s registered (phys %s hwaddr %s)\n",
711 netdev->name, netdev->dev->name,
712 netdev_addr ( netdev ) );
713
714 /* Register per-netdev configuration settings */
715 if ( ( rc = register_settings ( netdev_settings ( netdev ),
716 NULL, netdev->name ) ) != 0 ) {
717 DBGC ( netdev, "NETDEV %s could not register settings: %s\n",
718 netdev->name, strerror ( rc ) );
719 goto err_register_settings;
720 }
721
722 /* Probe device */
723 for_each_table_entry ( driver, NET_DRIVERS ) {
724 if ( driver->probe && ( rc = driver->probe ( netdev ) ) != 0 ) {
725 DBGC ( netdev, "NETDEV %s could not add %s device: "
726 "%s\n", netdev->name, driver->name,
727 strerror ( rc ) );
728 goto err_probe;
729 }
730 }
731
732 return 0;
733
734 err_probe:
735 for_each_table_entry_continue_reverse ( driver, NET_DRIVERS ) {
736 if ( driver->remove )
737 driver->remove ( netdev );
738 }
739 clear_settings ( netdev_settings ( netdev ) );
740 unregister_settings ( netdev_settings ( netdev ) );
741 err_register_settings:
742 list_del ( &netdev->list );
743 netdev_put ( netdev );
744 err_duplicate:
745 return rc;
746 }
747
748 /**
749 * Open network device
750 *
751 * @v netdev Network device
752 * @ret rc Return status code
753 */
netdev_open(struct net_device * netdev)754 int netdev_open ( struct net_device *netdev ) {
755 int rc;
756
757 /* Do nothing if device is already open */
758 if ( netdev->state & NETDEV_OPEN )
759 return 0;
760
761 DBGC ( netdev, "NETDEV %s opening\n", netdev->name );
762
763 /* Mark as opened */
764 netdev->state |= NETDEV_OPEN;
765
766 /* Open the device */
767 if ( ( rc = netdev->op->open ( netdev ) ) != 0 )
768 goto err;
769
770 /* Add to head of open devices list */
771 list_add ( &netdev->open_list, &open_net_devices );
772
773 /* Notify drivers of device state change */
774 netdev_notify ( netdev );
775
776 return 0;
777
778 err:
779 netdev->state &= ~NETDEV_OPEN;
780 return rc;
781 }
782
783 /**
784 * Close network device
785 *
786 * @v netdev Network device
787 */
netdev_close(struct net_device * netdev)788 void netdev_close ( struct net_device *netdev ) {
789 unsigned int num_configs;
790 unsigned int i;
791
792 /* Do nothing if device is already closed */
793 if ( ! ( netdev->state & NETDEV_OPEN ) )
794 return;
795
796 DBGC ( netdev, "NETDEV %s closing\n", netdev->name );
797
798 /* Terminate any ongoing configurations. Use intf_close()
799 * rather than intf_restart() to allow the cancellation to be
800 * reported back to us if a configuration is actually in
801 * progress.
802 */
803 num_configs = table_num_entries ( NET_DEVICE_CONFIGURATORS );
804 for ( i = 0 ; i < num_configs ; i++ )
805 intf_close ( &netdev->configs[i].job, -ECANCELED );
806
807 /* Remove from open devices list */
808 list_del ( &netdev->open_list );
809
810 /* Mark as closed */
811 netdev->state &= ~NETDEV_OPEN;
812
813 /* Notify drivers of device state change */
814 netdev_notify ( netdev );
815
816 /* Close the device */
817 netdev->op->close ( netdev );
818
819 /* Flush TX and RX queues */
820 netdev_tx_flush ( netdev );
821 netdev_rx_flush ( netdev );
822 }
823
824 /**
825 * Unregister network device
826 *
827 * @v netdev Network device
828 *
829 * Removes the network device from the list of network devices.
830 */
unregister_netdev(struct net_device * netdev)831 void unregister_netdev ( struct net_device *netdev ) {
832 struct net_driver *driver;
833
834 /* Ensure device is closed */
835 netdev_close ( netdev );
836
837 /* Remove device */
838 for_each_table_entry_reverse ( driver, NET_DRIVERS ) {
839 if ( driver->remove )
840 driver->remove ( netdev );
841 }
842
843 /* Unregister per-netdev configuration settings */
844 clear_settings ( netdev_settings ( netdev ) );
845 unregister_settings ( netdev_settings ( netdev ) );
846
847 /* Remove from device list */
848 DBGC ( netdev, "NETDEV %s unregistered\n", netdev->name );
849 list_del ( &netdev->list );
850 netdev_put ( netdev );
851
852 /* Reset network device index if no devices remain */
853 if ( list_empty ( &net_devices ) )
854 netdev_index = 0;
855 }
856
857 /** Enable or disable interrupts
858 *
859 * @v netdev Network device
860 * @v enable Interrupts should be enabled
861 */
netdev_irq(struct net_device * netdev,int enable)862 void netdev_irq ( struct net_device *netdev, int enable ) {
863
864 /* Do nothing if device does not support interrupts */
865 if ( ! netdev_irq_supported ( netdev ) )
866 return;
867
868 /* Enable or disable device interrupts */
869 netdev->op->irq ( netdev, enable );
870
871 /* Record interrupt enabled state */
872 netdev->state &= ~NETDEV_IRQ_ENABLED;
873 if ( enable )
874 netdev->state |= NETDEV_IRQ_ENABLED;
875 }
876
877 /**
878 * Get network device by name
879 *
880 * @v name Network device name
881 * @ret netdev Network device, or NULL
882 */
find_netdev(const char * name)883 struct net_device * find_netdev ( const char *name ) {
884 struct net_device *netdev;
885
886 /* Allow "netX" shortcut */
887 if ( strcmp ( name, "netX" ) == 0 )
888 return last_opened_netdev();
889
890 /* Identify network device by name */
891 list_for_each_entry ( netdev, &net_devices, list ) {
892 if ( strcmp ( netdev->name, name ) == 0 )
893 return netdev;
894 }
895
896 return NULL;
897 }
898
899 /**
900 * Get network device by index
901 *
902 * @v index Network device index
903 * @ret netdev Network device, or NULL
904 */
find_netdev_by_index(unsigned int index)905 struct net_device * find_netdev_by_index ( unsigned int index ) {
906 struct net_device *netdev;
907
908 /* Identify network device by index */
909 list_for_each_entry ( netdev, &net_devices, list ) {
910 if ( netdev->index == index )
911 return netdev;
912 }
913
914 return NULL;
915 }
916
917 /**
918 * Get network device by PCI bus:dev.fn address
919 *
920 * @v bus_type Bus type
921 * @v location Bus location
922 * @ret netdev Network device, or NULL
923 */
find_netdev_by_location(unsigned int bus_type,unsigned int location)924 struct net_device * find_netdev_by_location ( unsigned int bus_type,
925 unsigned int location ) {
926 struct net_device *netdev;
927
928 list_for_each_entry ( netdev, &net_devices, list ) {
929 if ( ( netdev->dev->desc.bus_type == bus_type ) &&
930 ( netdev->dev->desc.location == location ) )
931 return netdev;
932 }
933
934 return NULL;
935 }
936
937 /**
938 * Get network device by link-layer address
939 *
940 * @v ll_protocol Link-layer protocol
941 * @v ll_addr Link-layer address
942 * @ret netdev Network device, or NULL
943 */
find_netdev_by_ll_addr(struct ll_protocol * ll_protocol,const void * ll_addr)944 struct net_device * find_netdev_by_ll_addr ( struct ll_protocol *ll_protocol,
945 const void *ll_addr ) {
946 struct net_device *netdev;
947
948 list_for_each_entry ( netdev, &net_devices, list ) {
949 if ( ( netdev->ll_protocol == ll_protocol ) &&
950 ( memcmp ( netdev->ll_addr, ll_addr,
951 ll_protocol->ll_addr_len ) == 0 ) )
952 return netdev;
953 }
954
955 return NULL;
956 }
957
958 /**
959 * Get most recently opened network device
960 *
961 * @ret netdev Most recently opened network device, or NULL
962 */
last_opened_netdev(void)963 struct net_device * last_opened_netdev ( void ) {
964 struct net_device *netdev;
965
966 netdev = list_first_entry ( &open_net_devices, struct net_device,
967 open_list );
968 if ( ! netdev )
969 return NULL;
970
971 assert ( netdev_is_open ( netdev ) );
972 return netdev;
973 }
974
975 /**
976 * Transmit network-layer packet
977 *
978 * @v iobuf I/O buffer
979 * @v netdev Network device
980 * @v net_protocol Network-layer protocol
981 * @v ll_dest Destination link-layer address
982 * @v ll_source Source link-layer address
983 * @ret rc Return status code
984 *
985 * Prepends link-layer headers to the I/O buffer and transmits the
986 * packet via the specified network device. This function takes
987 * ownership of the I/O buffer.
988 */
net_tx(struct io_buffer * iobuf,struct net_device * netdev,struct net_protocol * net_protocol,const void * ll_dest,const void * ll_source)989 int net_tx ( struct io_buffer *iobuf, struct net_device *netdev,
990 struct net_protocol *net_protocol, const void *ll_dest,
991 const void *ll_source ) {
992 struct ll_protocol *ll_protocol = netdev->ll_protocol;
993 int rc;
994
995 /* Add link-layer header */
996 if ( ( rc = ll_protocol->push ( netdev, iobuf, ll_dest, ll_source,
997 net_protocol->net_proto ) ) != 0 ) {
998 /* Record error for diagnosis */
999 netdev_tx_err ( netdev, iobuf, rc );
1000 return rc;
1001 }
1002
1003 /* Transmit packet */
1004 return netdev_tx ( netdev, iobuf );
1005 }
1006
1007 /**
1008 * Process received network-layer packet
1009 *
1010 * @v iobuf I/O buffer
1011 * @v netdev Network device
1012 * @v net_proto Network-layer protocol, in network-byte order
1013 * @v ll_dest Destination link-layer address
1014 * @v ll_source Source link-layer address
1015 * @v flags Packet flags
1016 * @ret rc Return status code
1017 */
net_rx(struct io_buffer * iobuf,struct net_device * netdev,uint16_t net_proto,const void * ll_dest,const void * ll_source,unsigned int flags)1018 int net_rx ( struct io_buffer *iobuf, struct net_device *netdev,
1019 uint16_t net_proto, const void *ll_dest, const void *ll_source,
1020 unsigned int flags ) {
1021 struct net_protocol *net_protocol;
1022
1023 /* Hand off to network-layer protocol, if any */
1024 for_each_table_entry ( net_protocol, NET_PROTOCOLS ) {
1025 if ( net_protocol->net_proto == net_proto )
1026 return net_protocol->rx ( iobuf, netdev, ll_dest,
1027 ll_source, flags );
1028 }
1029
1030 DBGC ( netdev, "NETDEV %s unknown network protocol %04x\n",
1031 netdev->name, ntohs ( net_proto ) );
1032 free_iob ( iobuf );
1033 return -ENOTSUP;
1034 }
1035
1036 /**
1037 * Poll the network stack
1038 *
1039 * This polls all interfaces for received packets, and processes
1040 * packets from the RX queue.
1041 */
net_poll(void)1042 void net_poll ( void ) {
1043 struct net_device *netdev;
1044 struct io_buffer *iobuf;
1045 struct ll_protocol *ll_protocol;
1046 const void *ll_dest;
1047 const void *ll_source;
1048 uint16_t net_proto;
1049 unsigned int flags;
1050 int rc;
1051
1052 /* Poll and process each network device */
1053 list_for_each_entry ( netdev, &net_devices, list ) {
1054
1055 /* Poll for new packets */
1056 profile_start ( &net_poll_profiler );
1057 netdev_poll ( netdev );
1058 profile_stop ( &net_poll_profiler );
1059
1060 /* Leave received packets on the queue if receive
1061 * queue processing is currently frozen. This will
1062 * happen when the raw packets are to be manually
1063 * dequeued using netdev_rx_dequeue(), rather than
1064 * processed via the usual networking stack.
1065 */
1066 if ( netdev_rx_frozen ( netdev ) )
1067 continue;
1068
1069 /* Process all received packets */
1070 while ( ( iobuf = netdev_rx_dequeue ( netdev ) ) ) {
1071
1072 DBGC2 ( netdev, "NETDEV %s processing %p (%p+%zx)\n",
1073 netdev->name, iobuf, iobuf->data,
1074 iob_len ( iobuf ) );
1075 profile_start ( &net_rx_profiler );
1076
1077 /* Remove link-layer header */
1078 ll_protocol = netdev->ll_protocol;
1079 if ( ( rc = ll_protocol->pull ( netdev, iobuf,
1080 &ll_dest, &ll_source,
1081 &net_proto,
1082 &flags ) ) != 0 ) {
1083 free_iob ( iobuf );
1084 continue;
1085 }
1086
1087 /* Hand packet to network layer */
1088 if ( ( rc = net_rx ( iob_disown ( iobuf ), netdev,
1089 net_proto, ll_dest,
1090 ll_source, flags ) ) != 0 ) {
1091 /* Record error for diagnosis */
1092 netdev_rx_err ( netdev, NULL, rc );
1093 }
1094 profile_stop ( &net_rx_profiler );
1095 }
1096 }
1097 }
1098
1099 /**
1100 * Single-step the network stack
1101 *
1102 * @v process Network stack process
1103 */
net_step(struct process * process __unused)1104 static void net_step ( struct process *process __unused ) {
1105 net_poll();
1106 }
1107
1108 /**
1109 * Get the VLAN tag (when VLAN support is not present)
1110 *
1111 * @v netdev Network device
1112 * @ret tag 0, indicating that device is not a VLAN device
1113 */
vlan_tag(struct net_device * netdev __unused)1114 __weak unsigned int vlan_tag ( struct net_device *netdev __unused ) {
1115 return 0;
1116 }
1117
1118 /**
1119 * Identify VLAN device (when VLAN support is not present)
1120 *
1121 * @v trunk Trunk network device
1122 * @v tag VLAN tag
1123 * @ret netdev VLAN device, if any
1124 */
vlan_find(struct net_device * trunk __unused,unsigned int tag __unused)1125 __weak struct net_device * vlan_find ( struct net_device *trunk __unused,
1126 unsigned int tag __unused ) {
1127 return NULL;
1128 }
1129
1130 /** Networking stack process */
1131 PERMANENT_PROCESS ( net_process, net_step );
1132
1133 /**
1134 * Discard some cached network device data
1135 *
1136 * @ret discarded Number of cached items discarded
1137 */
net_discard(void)1138 static unsigned int net_discard ( void ) {
1139 struct net_device *netdev;
1140 struct io_buffer *iobuf;
1141 unsigned int discarded = 0;
1142
1143 /* Try to drop one deferred TX packet from each network device */
1144 for_each_netdev ( netdev ) {
1145 if ( ( iobuf = list_first_entry ( &netdev->tx_deferred,
1146 struct io_buffer,
1147 list ) ) != NULL ) {
1148
1149 /* Discard first deferred packet */
1150 list_del ( &iobuf->list );
1151 free_iob ( iobuf );
1152
1153 /* Report discard */
1154 discarded++;
1155 }
1156 }
1157
1158 return discarded;
1159 }
1160
1161 /** Network device cache discarder */
1162 struct cache_discarder net_discarder __cache_discarder ( CACHE_NORMAL ) = {
1163 .discard = net_discard,
1164 };
1165
1166 /**
1167 * Find network device configurator
1168 *
1169 * @v name Name
1170 * @ret configurator Network device configurator, or NULL
1171 */
find_netdev_configurator(const char * name)1172 struct net_device_configurator * find_netdev_configurator ( const char *name ) {
1173 struct net_device_configurator *configurator;
1174
1175 for_each_table_entry ( configurator, NET_DEVICE_CONFIGURATORS ) {
1176 if ( strcmp ( configurator->name, name ) == 0 )
1177 return configurator;
1178 }
1179 return NULL;
1180 }
1181
1182 /**
1183 * Start network device configuration
1184 *
1185 * @v netdev Network device
1186 * @v configurator Network device configurator
1187 * @ret rc Return status code
1188 */
netdev_configure(struct net_device * netdev,struct net_device_configurator * configurator)1189 int netdev_configure ( struct net_device *netdev,
1190 struct net_device_configurator *configurator ) {
1191 struct net_device_configuration *config =
1192 netdev_configuration ( netdev, configurator );
1193 int rc;
1194
1195 /* Check applicability of configurator */
1196 if ( ! netdev_configurator_applies ( netdev, configurator ) ) {
1197 DBGC ( netdev, "NETDEV %s does not support configuration via "
1198 "%s\n", netdev->name, configurator->name );
1199 return -ENOTSUP;
1200 }
1201
1202 /* Terminate any ongoing configuration */
1203 intf_restart ( &config->job, -ECANCELED );
1204
1205 /* Mark configuration as being in progress */
1206 config->rc = -EINPROGRESS_CONFIG;
1207
1208 DBGC ( netdev, "NETDEV %s starting configuration via %s\n",
1209 netdev->name, configurator->name );
1210
1211 /* Start configuration */
1212 if ( ( rc = configurator->start ( &config->job, netdev ) ) != 0 ) {
1213 DBGC ( netdev, "NETDEV %s could not start configuration via "
1214 "%s: %s\n", netdev->name, configurator->name,
1215 strerror ( rc ) );
1216 config->rc = rc;
1217 return rc;
1218 }
1219
1220 return 0;
1221 }
1222
1223 /**
1224 * Start network device configuration via all supported configurators
1225 *
1226 * @v netdev Network device
1227 * @ret rc Return status code
1228 */
netdev_configure_all(struct net_device * netdev)1229 int netdev_configure_all ( struct net_device *netdev ) {
1230 struct net_device_configurator *configurator;
1231 int rc;
1232
1233 /* Start configuration for each configurator */
1234 for_each_table_entry ( configurator, NET_DEVICE_CONFIGURATORS ) {
1235
1236 /* Skip any inapplicable configurators */
1237 if ( ! netdev_configurator_applies ( netdev, configurator ) )
1238 continue;
1239
1240 /* Start configuration */
1241 if ( ( rc = netdev_configure ( netdev, configurator ) ) != 0 )
1242 return rc;
1243 }
1244
1245 return 0;
1246 }
1247
1248 /**
1249 * Check if network device has a configuration with a specified status code
1250 *
1251 * @v netdev Network device
1252 * @v rc Status code
1253 * @ret has_rc Network device has a configuration with this status code
1254 */
netdev_has_configuration_rc(struct net_device * netdev,int rc)1255 static int netdev_has_configuration_rc ( struct net_device *netdev, int rc ) {
1256 unsigned int num_configs;
1257 unsigned int i;
1258
1259 num_configs = table_num_entries ( NET_DEVICE_CONFIGURATORS );
1260 for ( i = 0 ; i < num_configs ; i++ ) {
1261 if ( netdev->configs[i].rc == rc )
1262 return 1;
1263 }
1264 return 0;
1265 }
1266
1267 /**
1268 * Check if network device configuration is in progress
1269 *
1270 * @v netdev Network device
1271 * @ret is_in_progress Network device configuration is in progress
1272 */
netdev_configuration_in_progress(struct net_device * netdev)1273 int netdev_configuration_in_progress ( struct net_device *netdev ) {
1274
1275 return netdev_has_configuration_rc ( netdev, -EINPROGRESS_CONFIG );
1276 }
1277
1278 /**
1279 * Check if network device has at least one successful configuration
1280 *
1281 * @v netdev Network device
1282 * @v configurator Configurator
1283 * @ret rc Return status code
1284 */
netdev_configuration_ok(struct net_device * netdev)1285 int netdev_configuration_ok ( struct net_device *netdev ) {
1286
1287 return netdev_has_configuration_rc ( netdev, 0 );
1288 }
1289