[infiniband] Allow for the creation of multicast groups
[ipxe.git] / src / net / netdevice.c
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 /* Reject network devices that are already available via a
667 * different hardware device.
668 */
669 duplicate = find_netdev_by_ll_addr ( ll_protocol, netdev->ll_addr );
670 if ( duplicate && ( duplicate->dev != netdev->dev ) ) {
671 DBGC ( netdev, "NETDEV rejecting duplicate (phys %s) of %s "
672 "(phys %s)\n", netdev->dev->name, duplicate->name,
673 duplicate->dev->name );
674 rc = -EEXIST;
675 goto err_duplicate;
676 }
677
678 /* Reject named network devices that already exist */
679 if ( netdev->name[0] && ( duplicate = find_netdev ( netdev->name ) ) ) {
680 DBGC ( netdev, "NETDEV rejecting duplicate name %s\n",
681 duplicate->name );
682 rc = -EEXIST;
683 goto err_duplicate;
684 }
685
686 /* Record device index and create device name */
687 if ( netdev->name[0] == '\0' ) {
688 snprintf ( netdev->name, sizeof ( netdev->name ), "net%d",
689 netdev_index );
690 }
691 netdev->index = ++netdev_index;
692
693 /* Use least significant bits of the link-layer address to
694 * improve the randomness of the (non-cryptographic) random
695 * number generator.
696 */
697 memcpy ( &seed, ( netdev->ll_addr + ll_protocol->ll_addr_len
698 - sizeof ( seed ) ), sizeof ( seed ) );
699 srand ( rand() ^ seed );
700
701 /* Add to device list */
702 netdev_get ( netdev );
703 list_add_tail ( &netdev->list, &net_devices );
704 DBGC ( netdev, "NETDEV %s registered (phys %s hwaddr %s)\n",
705 netdev->name, netdev->dev->name,
706 netdev_addr ( netdev ) );
707
708 /* Register per-netdev configuration settings */
709 if ( ( rc = register_settings ( netdev_settings ( netdev ),
710 NULL, netdev->name ) ) != 0 ) {
711 DBGC ( netdev, "NETDEV %s could not register settings: %s\n",
712 netdev->name, strerror ( rc ) );
713 goto err_register_settings;
714 }
715
716 /* Probe device */
717 for_each_table_entry ( driver, NET_DRIVERS ) {
718 if ( driver->probe && ( rc = driver->probe ( netdev ) ) != 0 ) {
719 DBGC ( netdev, "NETDEV %s could not add %s device: "
720 "%s\n", netdev->name, driver->name,
721 strerror ( rc ) );
722 goto err_probe;
723 }
724 }
725
726 return 0;
727
728 err_probe:
729 for_each_table_entry_continue_reverse ( driver, NET_DRIVERS ) {
730 if ( driver->remove )
731 driver->remove ( netdev );
732 }
733 clear_settings ( netdev_settings ( netdev ) );
734 unregister_settings ( netdev_settings ( netdev ) );
735 err_register_settings:
736 err_duplicate:
737 return rc;
738 }
739
740 /**
741 * Open network device
742 *
743 * @v netdev Network device
744 * @ret rc Return status code
745 */
746 int netdev_open ( struct net_device *netdev ) {
747 int rc;
748
749 /* Do nothing if device is already open */
750 if ( netdev->state & NETDEV_OPEN )
751 return 0;
752
753 DBGC ( netdev, "NETDEV %s opening\n", netdev->name );
754
755 /* Mark as opened */
756 netdev->state |= NETDEV_OPEN;
757
758 /* Open the device */
759 if ( ( rc = netdev->op->open ( netdev ) ) != 0 )
760 goto err;
761
762 /* Add to head of open devices list */
763 list_add ( &netdev->open_list, &open_net_devices );
764
765 /* Notify drivers of device state change */
766 netdev_notify ( netdev );
767
768 return 0;
769
770 err:
771 netdev->state &= ~NETDEV_OPEN;
772 return rc;
773 }
774
775 /**
776 * Close network device
777 *
778 * @v netdev Network device
779 */
780 void netdev_close ( struct net_device *netdev ) {
781 unsigned int num_configs;
782 unsigned int i;
783
784 /* Do nothing if device is already closed */
785 if ( ! ( netdev->state & NETDEV_OPEN ) )
786 return;
787
788 DBGC ( netdev, "NETDEV %s closing\n", netdev->name );
789
790 /* Terminate any ongoing configurations. Use intf_close()
791 * rather than intf_restart() to allow the cancellation to be
792 * reported back to us if a configuration is actually in
793 * progress.
794 */
795 num_configs = table_num_entries ( NET_DEVICE_CONFIGURATORS );
796 for ( i = 0 ; i < num_configs ; i++ )
797 intf_close ( &netdev->configs[i].job, -ECANCELED );
798
799 /* Remove from open devices list */
800 list_del ( &netdev->open_list );
801
802 /* Mark as closed */
803 netdev->state &= ~NETDEV_OPEN;
804
805 /* Notify drivers of device state change */
806 netdev_notify ( netdev );
807
808 /* Close the device */
809 netdev->op->close ( netdev );
810
811 /* Flush TX and RX queues */
812 netdev_tx_flush ( netdev );
813 netdev_rx_flush ( netdev );
814 }
815
816 /**
817 * Unregister network device
818 *
819 * @v netdev Network device
820 *
821 * Removes the network device from the list of network devices.
822 */
823 void unregister_netdev ( struct net_device *netdev ) {
824 struct net_driver *driver;
825
826 /* Ensure device is closed */
827 netdev_close ( netdev );
828
829 /* Remove device */
830 for_each_table_entry_reverse ( driver, NET_DRIVERS ) {
831 if ( driver->remove )
832 driver->remove ( netdev );
833 }
834
835 /* Unregister per-netdev configuration settings */
836 clear_settings ( netdev_settings ( netdev ) );
837 unregister_settings ( netdev_settings ( netdev ) );
838
839 /* Remove from device list */
840 DBGC ( netdev, "NETDEV %s unregistered\n", netdev->name );
841 list_del ( &netdev->list );
842 netdev_put ( netdev );
843
844 /* Reset network device index if no devices remain */
845 if ( list_empty ( &net_devices ) )
846 netdev_index = 0;
847 }
848
849 /** Enable or disable interrupts
850 *
851 * @v netdev Network device
852 * @v enable Interrupts should be enabled
853 */
854 void netdev_irq ( struct net_device *netdev, int enable ) {
855
856 /* Do nothing if device does not support interrupts */
857 if ( ! netdev_irq_supported ( netdev ) )
858 return;
859
860 /* Enable or disable device interrupts */
861 netdev->op->irq ( netdev, enable );
862
863 /* Record interrupt enabled state */
864 netdev->state &= ~NETDEV_IRQ_ENABLED;
865 if ( enable )
866 netdev->state |= NETDEV_IRQ_ENABLED;
867 }
868
869 /**
870 * Get network device by name
871 *
872 * @v name Network device name
873 * @ret netdev Network device, or NULL
874 */
875 struct net_device * find_netdev ( const char *name ) {
876 struct net_device *netdev;
877
878 /* Allow "netX" shortcut */
879 if ( strcmp ( name, "netX" ) == 0 )
880 return last_opened_netdev();
881
882 /* Identify network device by name */
883 list_for_each_entry ( netdev, &net_devices, list ) {
884 if ( strcmp ( netdev->name, name ) == 0 )
885 return netdev;
886 }
887
888 return NULL;
889 }
890
891 /**
892 * Get network device by index
893 *
894 * @v index Network device index
895 * @ret netdev Network device, or NULL
896 */
897 struct net_device * find_netdev_by_index ( unsigned int index ) {
898 struct net_device *netdev;
899
900 /* Identify network device by index */
901 list_for_each_entry ( netdev, &net_devices, list ) {
902 if ( netdev->index == index )
903 return netdev;
904 }
905
906 return NULL;
907 }
908
909 /**
910 * Get network device by PCI bus:dev.fn address
911 *
912 * @v bus_type Bus type
913 * @v location Bus location
914 * @ret netdev Network device, or NULL
915 */
916 struct net_device * find_netdev_by_location ( unsigned int bus_type,
917 unsigned int location ) {
918 struct net_device *netdev;
919
920 list_for_each_entry ( netdev, &net_devices, list ) {
921 if ( ( netdev->dev->desc.bus_type == bus_type ) &&
922 ( netdev->dev->desc.location == location ) )
923 return netdev;
924 }
925
926 return NULL;
927 }
928
929 /**
930 * Get network device by link-layer address
931 *
932 * @v ll_protocol Link-layer protocol
933 * @v ll_addr Link-layer address
934 * @ret netdev Network device, or NULL
935 */
936 struct net_device * find_netdev_by_ll_addr ( struct ll_protocol *ll_protocol,
937 const void *ll_addr ) {
938 struct net_device *netdev;
939
940 list_for_each_entry ( netdev, &net_devices, list ) {
941 if ( ( netdev->ll_protocol == ll_protocol ) &&
942 ( memcmp ( netdev->ll_addr, ll_addr,
943 ll_protocol->ll_addr_len ) == 0 ) )
944 return netdev;
945 }
946
947 return NULL;
948 }
949
950 /**
951 * Get most recently opened network device
952 *
953 * @ret netdev Most recently opened network device, or NULL
954 */
955 struct net_device * last_opened_netdev ( void ) {
956 struct net_device *netdev;
957
958 netdev = list_first_entry ( &open_net_devices, struct net_device,
959 open_list );
960 if ( ! netdev )
961 return NULL;
962
963 assert ( netdev_is_open ( netdev ) );
964 return netdev;
965 }
966
967 /**
968 * Transmit network-layer packet
969 *
970 * @v iobuf I/O buffer
971 * @v netdev Network device
972 * @v net_protocol Network-layer protocol
973 * @v ll_dest Destination link-layer address
974 * @v ll_source Source link-layer address
975 * @ret rc Return status code
976 *
977 * Prepends link-layer headers to the I/O buffer and transmits the
978 * packet via the specified network device. This function takes
979 * ownership of the I/O buffer.
980 */
981 int net_tx ( struct io_buffer *iobuf, struct net_device *netdev,
982 struct net_protocol *net_protocol, const void *ll_dest,
983 const void *ll_source ) {
984 struct ll_protocol *ll_protocol = netdev->ll_protocol;
985 int rc;
986
987 /* Add link-layer header */
988 if ( ( rc = ll_protocol->push ( netdev, iobuf, ll_dest, ll_source,
989 net_protocol->net_proto ) ) != 0 ) {
990 /* Record error for diagnosis */
991 netdev_tx_err ( netdev, iobuf, rc );
992 return rc;
993 }
994
995 /* Transmit packet */
996 return netdev_tx ( netdev, iobuf );
997 }
998
999 /**
1000 * Process received network-layer packet
1001 *
1002 * @v iobuf I/O buffer
1003 * @v netdev Network device
1004 * @v net_proto Network-layer protocol, in network-byte order
1005 * @v ll_dest Destination link-layer address
1006 * @v ll_source Source link-layer address
1007 * @v flags Packet flags
1008 * @ret rc Return status code
1009 */
1010 int net_rx ( struct io_buffer *iobuf, struct net_device *netdev,
1011 uint16_t net_proto, const void *ll_dest, const void *ll_source,
1012 unsigned int flags ) {
1013 struct net_protocol *net_protocol;
1014
1015 /* Hand off to network-layer protocol, if any */
1016 for_each_table_entry ( net_protocol, NET_PROTOCOLS ) {
1017 if ( net_protocol->net_proto == net_proto )
1018 return net_protocol->rx ( iobuf, netdev, ll_dest,
1019 ll_source, flags );
1020 }
1021
1022 DBGC ( netdev, "NETDEV %s unknown network protocol %04x\n",
1023 netdev->name, ntohs ( net_proto ) );
1024 free_iob ( iobuf );
1025 return -ENOTSUP;
1026 }
1027
1028 /**
1029 * Poll the network stack
1030 *
1031 * This polls all interfaces for received packets, and processes
1032 * packets from the RX queue.
1033 */
1034 void net_poll ( void ) {
1035 struct net_device *netdev;
1036 struct io_buffer *iobuf;
1037 struct ll_protocol *ll_protocol;
1038 const void *ll_dest;
1039 const void *ll_source;
1040 uint16_t net_proto;
1041 unsigned int flags;
1042 int rc;
1043
1044 /* Poll and process each network device */
1045 list_for_each_entry ( netdev, &net_devices, list ) {
1046
1047 /* Poll for new packets */
1048 profile_start ( &net_poll_profiler );
1049 netdev_poll ( netdev );
1050 profile_stop ( &net_poll_profiler );
1051
1052 /* Leave received packets on the queue if receive
1053 * queue processing is currently frozen. This will
1054 * happen when the raw packets are to be manually
1055 * dequeued using netdev_rx_dequeue(), rather than
1056 * processed via the usual networking stack.
1057 */
1058 if ( netdev_rx_frozen ( netdev ) )
1059 continue;
1060
1061 /* Process all received packets */
1062 while ( ( iobuf = netdev_rx_dequeue ( netdev ) ) ) {
1063
1064 DBGC2 ( netdev, "NETDEV %s processing %p (%p+%zx)\n",
1065 netdev->name, iobuf, iobuf->data,
1066 iob_len ( iobuf ) );
1067 profile_start ( &net_rx_profiler );
1068
1069 /* Remove link-layer header */
1070 ll_protocol = netdev->ll_protocol;
1071 if ( ( rc = ll_protocol->pull ( netdev, iobuf,
1072 &ll_dest, &ll_source,
1073 &net_proto,
1074 &flags ) ) != 0 ) {
1075 free_iob ( iobuf );
1076 continue;
1077 }
1078
1079 /* Hand packet to network layer */
1080 if ( ( rc = net_rx ( iob_disown ( iobuf ), netdev,
1081 net_proto, ll_dest,
1082 ll_source, flags ) ) != 0 ) {
1083 /* Record error for diagnosis */
1084 netdev_rx_err ( netdev, NULL, rc );
1085 }
1086 profile_stop ( &net_rx_profiler );
1087 }
1088 }
1089 }
1090
1091 /**
1092 * Single-step the network stack
1093 *
1094 * @v process Network stack process
1095 */
1096 static void net_step ( struct process *process __unused ) {
1097 net_poll();
1098 }
1099
1100 /**
1101 * Get the VLAN tag (when VLAN support is not present)
1102 *
1103 * @v netdev Network device
1104 * @ret tag 0, indicating that device is not a VLAN device
1105 */
1106 __weak unsigned int vlan_tag ( struct net_device *netdev __unused ) {
1107 return 0;
1108 }
1109
1110 /**
1111 * Identify VLAN device (when VLAN support is not present)
1112 *
1113 * @v trunk Trunk network device
1114 * @v tag VLAN tag
1115 * @ret netdev VLAN device, if any
1116 */
1117 __weak struct net_device * vlan_find ( struct net_device *trunk __unused,
1118 unsigned int tag __unused ) {
1119 return NULL;
1120 }
1121
1122 /** Networking stack process */
1123 PERMANENT_PROCESS ( net_process, net_step );
1124
1125 /**
1126 * Discard some cached network device data
1127 *
1128 * @ret discarded Number of cached items discarded
1129 */
1130 static unsigned int net_discard ( void ) {
1131 struct net_device *netdev;
1132 struct io_buffer *iobuf;
1133 unsigned int discarded = 0;
1134
1135 /* Try to drop one deferred TX packet from each network device */
1136 for_each_netdev ( netdev ) {
1137 if ( ( iobuf = list_first_entry ( &netdev->tx_deferred,
1138 struct io_buffer,
1139 list ) ) != NULL ) {
1140
1141 /* Discard first deferred packet */
1142 list_del ( &iobuf->list );
1143 free_iob ( iobuf );
1144
1145 /* Report discard */
1146 discarded++;
1147 }
1148 }
1149
1150 return discarded;
1151 }
1152
1153 /** Network device cache discarder */
1154 struct cache_discarder net_discarder __cache_discarder ( CACHE_NORMAL ) = {
1155 .discard = net_discard,
1156 };
1157
1158 /**
1159 * Find network device configurator
1160 *
1161 * @v name Name
1162 * @ret configurator Network device configurator, or NULL
1163 */
1164 struct net_device_configurator * find_netdev_configurator ( const char *name ) {
1165 struct net_device_configurator *configurator;
1166
1167 for_each_table_entry ( configurator, NET_DEVICE_CONFIGURATORS ) {
1168 if ( strcmp ( configurator->name, name ) == 0 )
1169 return configurator;
1170 }
1171 return NULL;
1172 }
1173
1174 /**
1175 * Start network device configuration
1176 *
1177 * @v netdev Network device
1178 * @v configurator Network device configurator
1179 * @ret rc Return status code
1180 */
1181 int netdev_configure ( struct net_device *netdev,
1182 struct net_device_configurator *configurator ) {
1183 struct net_device_configuration *config =
1184 netdev_configuration ( netdev, configurator );
1185 int rc;
1186
1187 /* Check applicability of configurator */
1188 if ( ! netdev_configurator_applies ( netdev, configurator ) ) {
1189 DBGC ( netdev, "NETDEV %s does not support configuration via "
1190 "%s\n", netdev->name, configurator->name );
1191 return -ENOTSUP;
1192 }
1193
1194 /* Terminate any ongoing configuration */
1195 intf_restart ( &config->job, -ECANCELED );
1196
1197 /* Mark configuration as being in progress */
1198 config->rc = -EINPROGRESS_CONFIG;
1199
1200 DBGC ( netdev, "NETDEV %s starting configuration via %s\n",
1201 netdev->name, configurator->name );
1202
1203 /* Start configuration */
1204 if ( ( rc = configurator->start ( &config->job, netdev ) ) != 0 ) {
1205 DBGC ( netdev, "NETDEV %s could not start configuration via "
1206 "%s: %s\n", netdev->name, configurator->name,
1207 strerror ( rc ) );
1208 config->rc = rc;
1209 return rc;
1210 }
1211
1212 return 0;
1213 }
1214
1215 /**
1216 * Start network device configuration via all supported configurators
1217 *
1218 * @v netdev Network device
1219 * @ret rc Return status code
1220 */
1221 int netdev_configure_all ( struct net_device *netdev ) {
1222 struct net_device_configurator *configurator;
1223 int rc;
1224
1225 /* Start configuration for each configurator */
1226 for_each_table_entry ( configurator, NET_DEVICE_CONFIGURATORS ) {
1227
1228 /* Skip any inapplicable configurators */
1229 if ( ! netdev_configurator_applies ( netdev, configurator ) )
1230 continue;
1231
1232 /* Start configuration */
1233 if ( ( rc = netdev_configure ( netdev, configurator ) ) != 0 )
1234 return rc;
1235 }
1236
1237 return 0;
1238 }
1239
1240 /**
1241 * Check if network device has a configuration with a specified status code
1242 *
1243 * @v netdev Network device
1244 * @v rc Status code
1245 * @ret has_rc Network device has a configuration with this status code
1246 */
1247 static int netdev_has_configuration_rc ( struct net_device *netdev, int rc ) {
1248 unsigned int num_configs;
1249 unsigned int i;
1250
1251 num_configs = table_num_entries ( NET_DEVICE_CONFIGURATORS );
1252 for ( i = 0 ; i < num_configs ; i++ ) {
1253 if ( netdev->configs[i].rc == rc )
1254 return 1;
1255 }
1256 return 0;
1257 }
1258
1259 /**
1260 * Check if network device configuration is in progress
1261 *
1262 * @v netdev Network device
1263 * @ret is_in_progress Network device configuration is in progress
1264 */
1265 int netdev_configuration_in_progress ( struct net_device *netdev ) {
1266
1267 return netdev_has_configuration_rc ( netdev, -EINPROGRESS_CONFIG );
1268 }
1269
1270 /**
1271 * Check if network device has at least one successful configuration
1272 *
1273 * @v netdev Network device
1274 * @v configurator Configurator
1275 * @ret rc Return status code
1276 */
1277 int netdev_configuration_ok ( struct net_device *netdev ) {
1278
1279 return netdev_has_configuration_rc ( netdev, 0 );
1280 }