|
@@ -0,0 +1,163 @@
|
|
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., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
17
|
+ */
|
|
18
|
+
|
|
19
|
+#include <stdint.h>
|
|
20
|
+#include <byteswap.h>
|
|
21
|
+#include <errno.h>
|
|
22
|
+#include <gpxe/if_ether.h>
|
|
23
|
+#include <gpxe/pkbuff.h>
|
|
24
|
+#include <gpxe/netdevice.h>
|
|
25
|
+
|
|
26
|
+/** @file
|
|
27
|
+ *
|
|
28
|
+ * Network devices and network interfaces
|
|
29
|
+ *
|
|
30
|
+ */
|
|
31
|
+
|
|
32
|
+/** List of all registered network devices */
|
|
33
|
+static LIST_HEAD ( net_devices );
|
|
34
|
+
|
|
35
|
+/**
|
|
36
|
+ * Register network device
|
|
37
|
+ *
|
|
38
|
+ * @v netdev Network device
|
|
39
|
+ * @ret rc Return status code
|
|
40
|
+ *
|
|
41
|
+ * Adds the network device to the list of network devices.
|
|
42
|
+ */
|
|
43
|
+int register_netdevice ( struct net_device *netdev ) {
|
|
44
|
+ list_add ( &netdev->devices, &net_devices );
|
|
45
|
+ return 0;
|
|
46
|
+}
|
|
47
|
+
|
|
48
|
+/**
|
|
49
|
+ * Unregister network device
|
|
50
|
+ *
|
|
51
|
+ * @v netdev Network device
|
|
52
|
+ *
|
|
53
|
+ * Removes the network device from the list of network devices.
|
|
54
|
+ */
|
|
55
|
+void unregister_netdevice ( struct net_device *netdev ) {
|
|
56
|
+ list_del ( &netdev->devices );
|
|
57
|
+}
|
|
58
|
+
|
|
59
|
+/**
|
|
60
|
+ * Transmit packet via network device
|
|
61
|
+ *
|
|
62
|
+ * @v netdev Network device
|
|
63
|
+ * @v pkb Packet buffer
|
|
64
|
+ * @ret rc Return status code
|
|
65
|
+ *
|
|
66
|
+ * Transmits the packet via the network device. The @c pkb link-layer
|
|
67
|
+ * metadata must already have been filled in, and space for the
|
|
68
|
+ * link-layer header must already be present in the packet buffer.
|
|
69
|
+ */
|
|
70
|
+int netdev_send ( struct net_device *netdev, struct pk_buff *pkb ) {
|
|
71
|
+ int rc;
|
|
72
|
+
|
|
73
|
+ if ( pkb->net_proto != ETH_P_RAW ) {
|
|
74
|
+ if ( ( rc = netdev->build_llh ( netdev, pkb ) ) != 0 )
|
|
75
|
+ return rc;
|
|
76
|
+ }
|
|
77
|
+ return netdev->transmit ( netdev, pkb );
|
|
78
|
+}
|
|
79
|
+
|
|
80
|
+/**
|
|
81
|
+ * Poll for packet on network device
|
|
82
|
+ *
|
|
83
|
+ * @v netdev Network device
|
|
84
|
+ * @v pkb Packet buffer
|
|
85
|
+ * @ret rc Return status code
|
|
86
|
+ *
|
|
87
|
+ * Polls the network device for a packet. If a packet is available,
|
|
88
|
+ * it will be added to the packet buffer, and the link-layer metadata
|
|
89
|
+ * fields in @c pkb will be filled in.
|
|
90
|
+ */
|
|
91
|
+int netdev_poll ( struct net_device *netdev, struct pk_buff *pkb ) {
|
|
92
|
+ int rc;
|
|
93
|
+
|
|
94
|
+ if ( ( rc = netdev->poll ( netdev, pkb ) ) != 0 )
|
|
95
|
+ return rc;
|
|
96
|
+ return netdev->parse_llh ( netdev, pkb );
|
|
97
|
+}
|
|
98
|
+
|
|
99
|
+/**
|
|
100
|
+ * Transmit packet via network interface
|
|
101
|
+ *
|
|
102
|
+ * @v netif Network interface
|
|
103
|
+ * @v pkb Packet buffer
|
|
104
|
+ * @ret rc Return status code
|
|
105
|
+ *
|
|
106
|
+ * Transmits the packet via the network interface. The packet must
|
|
107
|
+ * start with a network-layer header (e.g. an IP header, for an IP
|
|
108
|
+ * interface). The packet contents are undefined on return.
|
|
109
|
+ */
|
|
110
|
+int netif_send ( struct net_interface *netif, struct pk_buff *pkb ) {
|
|
111
|
+ struct net_device *netdev = netif->netdev;
|
|
112
|
+ int rc;
|
|
113
|
+
|
|
114
|
+ if ( ( rc = netif->add_llh_metadata ( netif, pkb ) ) != 0 )
|
|
115
|
+ return rc;
|
|
116
|
+ pkb_push ( pkb, netdev->ll_hlen );
|
|
117
|
+ return netdev_send ( netdev, pkb );
|
|
118
|
+}
|
|
119
|
+
|
|
120
|
+/**
|
|
121
|
+ * Process received packet
|
|
122
|
+ *
|
|
123
|
+ * @v netif Network interface
|
|
124
|
+ * @v pkb Packet buffer
|
|
125
|
+ * @ret rc Return status code
|
|
126
|
+ *
|
|
127
|
+ * Processes a packet received via netdev_poll(). The interface
|
|
128
|
+ * corresponding to the network-layer protocol is identified, the
|
|
129
|
+ * link-layer header is stripped from the packet and the packet is
|
|
130
|
+ * passed to the net_interface::rx_packet() method.
|
|
131
|
+ */
|
|
132
|
+int netdev_rx_packet ( struct net_device *netdev, struct pk_buff *pkb ) {
|
|
133
|
+ struct net_interface *netif;
|
|
134
|
+
|
|
135
|
+ netif = netdev_find_netif ( netdev, pkb->net_proto );
|
|
136
|
+ if ( ! netif )
|
|
137
|
+ return -EAFNOSUPPORT;
|
|
138
|
+
|
|
139
|
+ pkb_pull ( pkb, netdev->ll_hlen );
|
|
140
|
+ return netif->rx_packet ( netif, pkb );
|
|
141
|
+}
|
|
142
|
+
|
|
143
|
+/**
|
|
144
|
+ * Poll for packet on all network devices
|
|
145
|
+ *
|
|
146
|
+ * @v pkb Packet buffer
|
|
147
|
+ * @ret netdev Network device
|
|
148
|
+ * @ret rc Return status code
|
|
149
|
+ *
|
|
150
|
+ * Polls all network devices for a packet. If a packet is available
|
|
151
|
+ * on any interface, @c netdev will be filled in and the packet will
|
|
152
|
+ * be received as per netdev_poll().
|
|
153
|
+ */
|
|
154
|
+int net_poll ( struct pk_buff *pkb, struct net_device **netdev ) {
|
|
155
|
+ int rc;
|
|
156
|
+
|
|
157
|
+ list_for_each_entry ( (*netdev), &net_devices, devices ) {
|
|
158
|
+ if ( ( rc = netdev_poll ( *netdev, pkb ) ) == 0 )
|
|
159
|
+ return rc;
|
|
160
|
+ }
|
|
161
|
+
|
|
162
|
+ return -EAGAIN;
|
|
163
|
+}
|