source: mainline/uspace/lib/c/include/net/device.h@ f37eb84

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since f37eb84 was 00d7e1b, checked in by Martin Decky <martin@…>, 14 years ago

networking improvements

  • start the networking stack from init
  • add loopback network interface driver (cherrypicked and sanitized from lp:~helenos-nicf/helenos/nicf)
  • add libnic and various small pieces from lp:~helenos-nicf/helenos/nicf
  • fix client side of NIC_GET_ADDRESS
  • net binary overhaul

Note: "ping 127.0.0.1" works, but the first three pings timeout for some reason

  • Property mode set to 100644
File size: 13.3 KB
Line 
1/*
2 * Copyright (c) 2009 Lukas Mejdrech
3 * Copyright (c) 2011 Radim Vansa
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/** @addtogroup libc
31 * @{
32 */
33
34/** @file
35 * Device identifier, state and usage statistics.
36 */
37
38#ifndef LIBC_DEVICE_ID_TYPE_H_
39#define LIBC_DEVICE_ID_TYPE_H_
40
41#include <adt/int_map.h>
42#include <net/eth_phys.h>
43#include <bool.h>
44
45/** Ethernet address length. */
46#define ETH_ADDR 6
47
48/** MAC printing format */
49#define PRIMAC "%02x:%02x:%02x:%02x:%02x:%02x"
50
51/** MAC arguments */
52#define ARGSMAC(__a) \
53 (__a)[0], (__a)[1], (__a)[2], (__a)[3], (__a)[4], (__a)[5]
54
55/* Compare MAC address with specific value */
56#define MAC_EQUALS_VALUE(__a, __a0, __a1, __a2, __a3, __a4, __a5) \
57 ((__a)[0] == (__a0) && (__a)[1] == (__a1) && (__a)[2] == (__a2) \
58 && (__a)[3] == (__a3) && (__a)[4] == (__a4) && (__a)[5] == (__a5))
59
60#define MAC_IS_ZERO(__x) \
61 MAC_EQUALS_VALUE(__x, 0, 0, 0, 0, 0, 0)
62
63/** Device identifier to generic type map declaration. */
64#define DEVICE_MAP_DECLARE INT_MAP_DECLARE
65
66/** Device identifier to generic type map implementation. */
67#define DEVICE_MAP_IMPLEMENT INT_MAP_IMPLEMENT
68
69/** Max length of any hw nic address (currently only eth) */
70#define NIC_MAX_ADDRESS_LENGTH 16
71
72/** Invalid device identifier. */
73#define NIC_DEVICE_INVALID_ID (-1)
74
75#define NIC_VENDOR_MAX_LENGTH 64
76#define NIC_MODEL_MAX_LENGTH 64
77#define NIC_PART_NUMBER_MAX_LENGTH 64
78#define NIC_SERIAL_NUMBER_MAX_LENGTH 64
79
80/**
81 * The bitmap uses single bit for each of the 2^12 = 4096 possible VLAN tags.
82 * This means its size is 4096/8 = 512 bytes.
83 */
84#define NIC_VLAN_BITMAP_SIZE 512
85
86#define NIC_DEVICE_PRINT_FMT "%x"
87
88/** Device identifier type. */
89typedef int nic_device_id_t;
90
91/**
92 * Structure covering the MAC address.
93 */
94typedef struct nic_address {
95 uint8_t address[ETH_ADDR];
96} nic_address_t;
97
98/** Device state. */
99typedef enum nic_device_state {
100 /**
101 * Device present and stopped. Moving device to this state means to discard
102 * all settings and WOL virtues, rebooting the NIC to state as if the
103 * computer just booted (or the NIC was just inserted in case of removable
104 * NIC).
105 */
106 NIC_STATE_STOPPED,
107 /**
108 * If the NIC is in this state no packets (frames) are transmitted nor
109 * received. However, the settings are not restarted. You can use this state
110 * to temporarily disable transmition/reception or atomically (with respect
111 * to incoming/outcoming packets) change frames acceptance etc.
112 */
113 NIC_STATE_DOWN,
114 /** Device is normally operating. */
115 NIC_STATE_ACTIVE,
116 /** Just a constant to limit the state numbers */
117 NIC_STATE_MAX,
118} nic_device_state_t;
119
120/**
121 * Channel operating mode used on the medium.
122 */
123typedef enum {
124 NIC_CM_UNKNOWN,
125 NIC_CM_FULL_DUPLEX,
126 NIC_CM_HALF_DUPLEX,
127 NIC_CM_SIMPLEX
128} nic_channel_mode_t;
129
130/**
131 * Role for the device (used e.g. for 1000Gb ethernet)
132 */
133typedef enum {
134 NIC_ROLE_UNKNOWN,
135 NIC_ROLE_AUTO,
136 NIC_ROLE_MASTER,
137 NIC_ROLE_SLAVE
138} nic_role_t;
139
140/**
141 * Current state of the cable in the device
142 */
143typedef enum {
144 NIC_CS_UNKNOWN,
145 NIC_CS_PLUGGED,
146 NIC_CS_UNPLUGGED
147} nic_cable_state_t;
148
149/**
150 * Result of the requested operation
151 */
152typedef enum {
153 /** Successfully disabled */
154 NIC_RESULT_DISABLED,
155 /** Successfully enabled */
156 NIC_RESULT_ENABLED,
157 /** Not supported at all */
158 NIC_RESULT_NOT_SUPPORTED,
159 /** Temporarily not available */
160 NIC_RESULT_NOT_AVAILABLE,
161 /** Result extensions */
162 NIC_RESULT_FIRST_EXTENSION
163} nic_result_t;
164
165/** Device usage statistics. */
166typedef struct nic_device_stats {
167 /** Total packets received (accepted). */
168 unsigned long receive_packets;
169 /** Total packets transmitted. */
170 unsigned long send_packets;
171 /** Total bytes received (accepted). */
172 unsigned long receive_bytes;
173 /** Total bytes transmitted. */
174 unsigned long send_bytes;
175 /** Bad packets received counter. */
176 unsigned long receive_errors;
177 /** Packet transmition problems counter. */
178 unsigned long send_errors;
179 /** Number of frames dropped due to insufficient space in RX buffers */
180 unsigned long receive_dropped;
181 /** Number of frames dropped due to insufficient space in TX buffers */
182 unsigned long send_dropped;
183 /** Total multicast packets received (accepted). */
184 unsigned long receive_multicast;
185 /** Total broadcast packets received (accepted). */
186 unsigned long receive_broadcast;
187 /** The number of collisions due to congestion on the medium. */
188 unsigned long collisions;
189 /** Unicast packets received but not accepted (filtered) */
190 unsigned long receive_filtered_unicast;
191 /** Multicast packets received but not accepted (filtered) */
192 unsigned long receive_filtered_multicast;
193 /** Broadcast packets received but not accepted (filtered) */
194 unsigned long receive_filtered_broadcast;
195
196 /* detailed receive_errors */
197
198 /** Received packet length error counter. */
199 unsigned long receive_length_errors;
200 /** Receiver buffer overflow counter. */
201 unsigned long receive_over_errors;
202 /** Received packet with crc error counter. */
203 unsigned long receive_crc_errors;
204 /** Received frame alignment error counter. */
205 unsigned long receive_frame_errors;
206 /** Receiver fifo overrun counter. */
207 unsigned long receive_fifo_errors;
208 /** Receiver missed packet counter. */
209 unsigned long receive_missed_errors;
210
211 /* detailed send_errors */
212
213 /** Transmitter aborted counter. */
214 unsigned long send_aborted_errors;
215 /** Transmitter carrier errors counter. */
216 unsigned long send_carrier_errors;
217 /** Transmitter fifo overrun counter. */
218 unsigned long send_fifo_errors;
219 /** Transmitter carrier errors counter. */
220 unsigned long send_heartbeat_errors;
221 /** Transmitter window errors counter. */
222 unsigned long send_window_errors;
223
224 /* for cslip etc */
225
226 /** Total compressed packets received. */
227 unsigned long receive_compressed;
228 /** Total compressed packet transmitted. */
229 unsigned long send_compressed;
230} nic_device_stats_t;
231
232/** Errors corresponding to those in the nic_device_stats_t */
233typedef enum {
234 NIC_SEC_BUFFER_FULL,
235 NIC_SEC_ABORTED,
236 NIC_SEC_CARRIER_LOST,
237 NIC_SEC_FIFO_OVERRUN,
238 NIC_SEC_HEARTBEAT,
239 NIC_SEC_WINDOW_ERROR,
240 /* Error encountered during TX but with other type of error */
241 NIC_SEC_OTHER
242} nic_send_error_cause_t;
243
244/** Errors corresponding to those in the nic_device_stats_t */
245typedef enum {
246 NIC_REC_BUFFER_FULL,
247 NIC_REC_LENGTH,
248 NIC_REC_BUFFER_OVERFLOW,
249 NIC_REC_CRC,
250 NIC_REC_FRAME_ALIGNMENT,
251 NIC_REC_FIFO_OVERRUN,
252 NIC_REC_MISSED,
253 /* Error encountered during RX but with other type of error */
254 NIC_REC_OTHER
255} nic_receive_error_cause_t;
256
257/**
258 * Information about the NIC that never changes - name, vendor, model,
259 * capabilites and so on.
260 */
261typedef struct nic_device_info {
262 /* Device identification */
263 char vendor_name[NIC_VENDOR_MAX_LENGTH];
264 char model_name[NIC_MODEL_MAX_LENGTH];
265 char part_number[NIC_PART_NUMBER_MAX_LENGTH];
266 char serial_number[NIC_SERIAL_NUMBER_MAX_LENGTH];
267 uint16_t vendor_id;
268 uint16_t device_id;
269 uint16_t subsystem_vendor_id;
270 uint16_t subsystem_id;
271 /* Device capabilities */
272 uint16_t ethernet_support[ETH_PHYS_LAYERS];
273
274 /** The mask of all modes which the device can advertise
275 *
276 * see ETH_AUTONEG_ macros in net/eth_phys.h of libc
277 */
278 uint32_t autoneg_support;
279} nic_device_info_t;
280
281/**
282 * Type of the ethernet frame
283 */
284typedef enum nic_frame_type {
285 NIC_FRAME_UNICAST,
286 NIC_FRAME_MULTICAST,
287 NIC_FRAME_BROADCAST
288} nic_frame_type_t;
289
290/**
291 * Specifies which unicast frames is the NIC receiving.
292 */
293typedef enum nic_unicast_mode {
294 NIC_UNICAST_UNKNOWN,
295 /** No unicast frames are received */
296 NIC_UNICAST_BLOCKED,
297 /** Only the frames with this NIC's MAC as destination are received */
298 NIC_UNICAST_DEFAULT,
299 /**
300 * Both frames with this NIC's MAC and those specified in the list are
301 * received
302 */
303 NIC_UNICAST_LIST,
304 /** All unicast frames are received */
305 NIC_UNICAST_PROMISC
306} nic_unicast_mode_t;
307
308typedef enum nic_multicast_mode {
309 NIC_MULTICAST_UNKNOWN,
310 /** No multicast frames are received */
311 NIC_MULTICAST_BLOCKED,
312 /** Frames with multicast addresses specified in this list are received */
313 NIC_MULTICAST_LIST,
314 /** All multicast frames are received */
315 NIC_MULTICAST_PROMISC
316} nic_multicast_mode_t;
317
318typedef enum nic_broadcast_mode {
319 NIC_BROADCAST_UNKNOWN,
320 /** Broadcast frames are dropped */
321 NIC_BROADCAST_BLOCKED,
322 /** Broadcast frames are received */
323 NIC_BROADCAST_ACCEPTED
324} nic_broadcast_mode_t;
325
326/**
327 * Structure covering the bitmap with VLAN tags.
328 */
329typedef struct nic_vlan_mask {
330 uint8_t bitmap[NIC_VLAN_BITMAP_SIZE];
331} nic_vlan_mask_t;
332
333/* WOL virtue identifier */
334typedef unsigned int nic_wv_id_t;
335
336/**
337 * Structure passed as argument for virtue NIC_WV_MAGIC_PACKET.
338 */
339typedef struct nic_wv_magic_packet_data {
340 uint8_t password[6];
341} nic_wv_magic_packet_data_t;
342
343/**
344 * Structure passed as argument for virtue NIC_WV_DIRECTED_IPV4
345 */
346typedef struct nic_wv_ipv4_data {
347 uint8_t address[4];
348} nic_wv_ipv4_data_t;
349
350/**
351 * Structure passed as argument for virtue NIC_WV_DIRECTED_IPV6
352 */
353typedef struct nic_wv_ipv6_data {
354 uint8_t address[16];
355} nic_wv_ipv6_data_t;
356
357/**
358 * WOL virtue types defining the interpretation of data passed to the virtue.
359 * Those tagged with S can have only single virtue active at one moment, those
360 * tagged with M can have multiple ones.
361 */
362typedef enum nic_wv_type {
363 /**
364 * Used for deletion of the virtue - in this case the mask, data and length
365 * arguments are ignored.
366 */
367 NIC_WV_NONE,
368 /** S
369 * Enabled <=> wakeup upon link change
370 */
371 NIC_WV_LINK_CHANGE,
372 /** S
373 * If this virtue is set up, wakeup can be issued by a magic packet frame.
374 * If the data argument is not NULL, it must contain
375 * nic_wv_magic_packet_data structure with the SecureOn password.
376 */
377 NIC_WV_MAGIC_PACKET,
378 /** M
379 * If the virtue is set up, wakeup can be issued by a frame targeted to
380 * device with MAC address specified in data. The data must contain
381 * nic_address_t structure.
382 */
383 NIC_WV_DESTINATION,
384 /** S
385 * Enabled <=> wakeup upon receiving broadcast frame
386 */
387 NIC_WV_BROADCAST,
388 /** S
389 * Enabled <=> wakeup upon receiving ARP Request
390 */
391 NIC_WV_ARP_REQUEST,
392 /** M
393 * If enabled, the wakeup is issued upon receiving frame with an IPv4 packet
394 * with IPv4 address specified in data. The data must contain
395 * nic_wv_ipv4_data structure.
396 */
397 NIC_WV_DIRECTED_IPV4,
398 /** M
399 * If enabled, the wakeup is issued upon receiving frame with an IPv4 packet
400 * with IPv6 address specified in data. The data must contain
401 * nic_wv_ipv6_data structure.
402 */
403 NIC_WV_DIRECTED_IPV6,
404 /** M
405 * First length/2 bytes in the argument are interpreted as mask, second
406 * length/2 bytes are interpreted as content.
407 * If enabled, the wakeup is issued upon receiving frame where the bytes
408 * with non-zero value in the mask equal to those in the content.
409 */
410 NIC_WV_FULL_MATCH,
411 /**
412 * Dummy value, do not use.
413 */
414 NIC_WV_MAX
415} nic_wv_type_t;
416
417/**
418 * Specifies the interrupt/polling mode used by the driver and NIC
419 */
420typedef enum nic_poll_mode {
421 /**
422 * NIC issues interrupts upon events.
423 */
424 NIC_POLL_IMMEDIATE,
425 /**
426 * Some uspace app calls nic_poll_now(...) in order to check the NIC state
427 * - no interrupts are received from the NIC.
428 */
429 NIC_POLL_ON_DEMAND,
430 /**
431 * The driver itself issues a poll request in a periodic manner. It is
432 * allowed to use hardware timer if the NIC supports it.
433 */
434 NIC_POLL_PERIODIC,
435 /**
436 * The driver itself issued a poll request in a periodic manner. The driver
437 * must create software timer, internal hardware timer of NIC must not be
438 * used even if the NIC supports it.
439 */
440 NIC_POLL_SOFTWARE_PERIODIC
441} nic_poll_mode_t;
442
443/**
444 * Says if this virtue type is a multi-virtue (there can be multiple virtues of
445 * this type at once).
446 *
447 * @param type
448 *
449 * @return true or false
450 */
451static inline int nic_wv_is_multi(nic_wv_type_t type) {
452 switch (type) {
453 case NIC_WV_FULL_MATCH:
454 case NIC_WV_DESTINATION:
455 case NIC_WV_DIRECTED_IPV4:
456 case NIC_WV_DIRECTED_IPV6:
457 return true;
458 default:
459 return false;
460 }
461}
462
463static inline const char *nic_device_state_to_string(nic_device_state_t state)
464{
465 switch (state) {
466 case NIC_STATE_STOPPED:
467 return "stopped";
468 case NIC_STATE_DOWN:
469 return "down";
470 case NIC_STATE_ACTIVE:
471 return "active";
472 default:
473 return "undefined";
474 }
475}
476
477#endif
478
479/** @}
480 */
Note: See TracBrowser for help on using the repository browser.