This repository was archived by the owner on Sep 15, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathnetdev.h
More file actions
567 lines (500 loc) · 18.5 KB
/
netdev.h
File metadata and controls
567 lines (500 loc) · 18.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
/****************************************************************************
* net/netdev/netdev.h
*
* Copyright (C) 2014-2015, 2018 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
#ifndef __NET_NETDEV_NETDEV_H
#define __NET_NETDEV_NETDEV_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/types.h>
#include <stdbool.h>
#include <nuttx/net/ip.h>
#ifdef CONFIG_NETDOWN_NOTIFIER
# include <nuttx/wqueue.h>
#endif
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/* If CONFIG_NETDEV_IFINDEX is enabled then there is limit to the number of
* devices that can be registered due to the nature of some static data.
*/
#define MAX_IFINDEX 32
/****************************************************************************
* Public Data
****************************************************************************/
#undef EXTERN
#if defined(__cplusplus)
#define EXTERN extern "C"
extern "C"
{
#else
#define EXTERN extern
#endif
/* List of registered Ethernet device drivers. You must have the network
* locked in order to access this list.
*
* NOTE that this duplicates a declaration in net/tcp/tcp.h
*/
EXTERN struct net_driver_s *g_netdevices;
#ifdef CONFIG_NETDEV_IFINDEX
/* The set of network devices that have been registered. This is used to
* assign a unique device index to the newly registered device.
*
* REVISIT: The width of g_nassigned limits the number of registered
* devices to 32 (MAX_IFINDEX).
*/
EXTERN uint32_t g_devset;
/* The set of network devices that have been freed. The purpose of this
* set is to postpone reuse of a interface index for as long as possible,
* i.e., don't reuse an interface index until all of the possible indices
* have been used.
*/
EXTERN uint32_t g_devfreed;
#endif
/****************************************************************************
* Public Types
****************************************************************************/
/* Callback from netdev_foreach() */
struct net_driver_s; /* Forward reference */
typedef int (*netdev_callback_t)(FAR struct net_driver_s *dev, FAR void *arg);
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
/****************************************************************************
* Name: netdev_ifup / netdev_ifdown
*
* Description:
* Bring the interface up/down
*
****************************************************************************/
void netdev_ifup(FAR struct net_driver_s *dev);
void netdev_ifdown(FAR struct net_driver_s *dev);
/****************************************************************************
* Name: netdev_verify
*
* Description:
* Verify that the specified device still exists
*
* Assumptions:
* The caller has locked the network.
*
****************************************************************************/
bool netdev_verify(FAR struct net_driver_s *dev);
/****************************************************************************
* Name: netdev_findbyname
*
* Description:
* Find a previously registered network device using its assigned
* network interface name
*
* Input Parameters:
* ifname The interface name of the device of interest
*
* Returned Value:
* Pointer to driver on success; null on failure
*
****************************************************************************/
FAR struct net_driver_s *netdev_findbyname(FAR const char *ifname);
/****************************************************************************
* Name: netdev_foreach
*
* Description:
* Enumerate each registered network device.
*
* NOTE: netdev semaphore held throughout enumeration.
*
* Input Parameters:
* callback - Will be called for each registered device
* arg - User argument passed to callback()
*
* Returned Value:
* 0:Enumeration completed 1:Enumeration terminated early by callback
*
****************************************************************************/
int netdev_foreach(netdev_callback_t callback, FAR void *arg);
/****************************************************************************
* Name: netdev_findby_lipv4addr
*
* Description:
* Find a previously registered network device by matching a local address
* with the subnet served by the device. Only "up" devices are considered
* (since a "down" device has no meaningful address).
*
* Input Parameters:
* lipaddr - Local, IPv4 address assigned to the network device. Or any
* IPv4 address on the sub-net served by the network device.
*
* Returned Value:
* Pointer to driver on success; null on failure
*
****************************************************************************/
#ifdef CONFIG_NET_IPv4
FAR struct net_driver_s *netdev_findby_lipv4addr(in_addr_t lipaddr);
#endif
/****************************************************************************
* Name: netdev_findby_lipv6addr
*
* Description:
* Find a previously registered network device by matching a local address
* with the subnet served by the device. Only "up" devices are considered
* (since a "down" device has no meaningful address).
*
* Input Parameters:
* lipaddr - Local, IPv6 address assigned to the network device. Or any
* IPv6 address on the sub-net served by the network device.
*
* Returned Value:
* Pointer to driver on success; null on failure
*
****************************************************************************/
#ifdef CONFIG_NET_IPv6
FAR struct net_driver_s *netdev_findby_lipv6addr(const net_ipv6addr_t lipaddr);
#endif
/****************************************************************************
* Name: netdev_findby_ripv4addr
*
* Description:
* Find a previously registered network device by matching the remote
* IPv4 address that can be reached by the device.
*
* Input Parameters:
* lipaddr - Local, bound address of a connection (used only if ripaddr is
* the broadcast address).
* ripaddr - Remote address of a connection to use in the lookup
*
* Returned Value:
* Pointer to driver on success; null on failure
*
****************************************************************************/
#ifdef CONFIG_NET_IPv4
FAR struct net_driver_s *netdev_findby_ripv4addr(in_addr_t lipaddr,
in_addr_t ripaddr);
#endif
/****************************************************************************
* Name: netdev_findby_ripv6addr
*
* Description:
* Find a previously registered network device by matching the remote
* IPv6 address that can be reached by the device.
*
* Input Parameters:
* lipaddr - Local, bound address of a connection (used only if ripaddr is
* a multicast address).
* ripaddr - Remote address of a connection to use in the lookup
*
* Returned Value:
* Pointer to driver on success; null on failure
*
****************************************************************************/
#ifdef CONFIG_NET_IPv6
FAR struct net_driver_s *netdev_findby_ripv6addr(const net_ipv6addr_t lipaddr,
const net_ipv6addr_t ripaddr);
#endif
/****************************************************************************
* Name: netdev_findbyindex
*
* Description:
* Find a previously registered network device by assigned interface index.
*
* Input Parameters:
* ifindex - The interface index. This is a one-based index and must be
* greater than zero.
*
* Returned Value:
* Pointer to driver on success; NULL on failure. This function will return
* NULL only if there is no device corresponding to the provided index.
*
****************************************************************************/
FAR struct net_driver_s *netdev_findbyindex(int ifindex);
/****************************************************************************
* Name: netdev_nextindex
*
* Description:
* Return the interface index to the next valid device.
*
* Input Parameters:
* ifindex - The first interface index to check. Usually in a traversal
* this would be the previous interface index plus 1.
*
* Returned Value:
* The interface index for the next network driver. -ENODEV is returned if
* there are no further devices with assigned interface indices.
*
****************************************************************************/
#ifdef CONFIG_NETDEV_IFINDEX
int netdev_nextindex(int ifindex);
#endif
/****************************************************************************
* Name: netdev_indextoname
*
* Description:
* The if_indextoname() function maps an interface index to its
* corresponding name.
*
* Input Parameters:
* ifname - Points to a buffer of at least IF_NAMESIZE bytes.
* if_indextoname() will place in this buffer the name of the
* interface with index ifindex.
*
* Returned Value:
* If ifindex is an interface index, then the function will return zero
* (OK). Otherwise, the function returns a negated errno value;
*
****************************************************************************/
#ifdef CONFIG_NETDEV_IFINDEX
int netdev_indextoname(unsigned int ifindex, FAR char *ifname);
#endif
/****************************************************************************
* Name: netdev_nametoindex
*
* Description:
* The if_nametoindex() function returns the interface index corresponding
* to name ifname.
*
* Input Parameters:
* ifname - The interface name
*
* Returned Value:
* The corresponding index if ifname is the name of an interface;
* otherwise, a negated errno value is returned.
*
****************************************************************************/
#ifdef CONFIG_NETDEV_IFINDEX
unsigned int netdev_nametoindex(FAR const char *ifname);
#endif
/****************************************************************************
* Name: netdev_default
*
* Description:
* Return the default network device. REVISIT: At present this function
* arbitrarily returns the first UP device at the head of the device
* list. Perhaps the default device should be a device name
* configuration option?
*
* So why is this here: It represents my current though for what to do
* if a socket is connected with INADDY_ANY. In this case, I suppose we
* should use the IP address associated with some default device???
*
* Input Parameters:
* NULL
*
* Returned Value:
* Pointer to default network driver on success; null on failure
*
****************************************************************************/
FAR struct net_driver_s *netdev_default(void);
/****************************************************************************
* Name: netdev_ipv4_txnotify
*
* Description:
* Notify the device driver that forwards the IPv4 address that new TX
* data is available.
*
* Input Parameters:
* lipaddr - The local address bound to the socket
* ripaddr - The remote address to send the data
*
* Returned Value:
* None
*
****************************************************************************/
#ifdef CONFIG_NET_IPv4
void netdev_ipv4_txnotify(in_addr_t lipaddr, in_addr_t ripaddr);
#endif /* CONFIG_NET_IPv4 */
/****************************************************************************
* Name: netdev_ipv6_txnotify
*
* Description:
* Notify the device driver that forwards the IPv4 address that new TX
* data is available.
*
* Input Parameters:
* lipaddr - The local address bound to the socket
* ripaddr - The remote address to send the data
*
* Returned Value:
* None
*
****************************************************************************/
#ifdef CONFIG_NET_IPv6
void netdev_ipv6_txnotify(FAR const net_ipv6addr_t lipaddr,
FAR const net_ipv6addr_t ripaddr);
#endif /* CONFIG_NET_IPv6 */
/****************************************************************************
* Name: netdev_txnotify_dev
*
* Description:
* Notify the device driver that new TX data is available. This variant
* would be called when the upper level logic already understands how the
* packet will be routed.
*
* Input Parameters:
* dev - The network device driver state structure.
*
* Returned Value:
* None
*
****************************************************************************/
void netdev_txnotify_dev(FAR struct net_driver_s *dev);
/****************************************************************************
* Name: netdev_count
*
* Description:
* Return the number of network devices
*
* Input Parameters:
* None
*
* Returned Value:
* The number of network devices
*
****************************************************************************/
int netdev_count(void);
/****************************************************************************
* Name: netdev_ipv4_ifconf
*
* Description:
* Return the IPv4 configuration of each network adapter
*
* Input Parameters:
* ifc - A reference to the instance of struct ifconf in which to return
* the information.
*
* Returned Value:
* Zero is returned on success; a negated errno value is returned on any
* failure.
*
* Assumptions:
* The nework is locked
*
****************************************************************************/
#ifdef CONFIG_NET_IPv4
struct ifconf; /* Forward reference */
int netdev_ipv4_ifconf(FAR struct ifconf *ifc);
#endif
/****************************************************************************
* Name: netdev_ipv6_ifconf
*
* Description:
* Return the IPv6 configuration of each network adapter
*
* Input Parameters:
* lifc - A reference to the instance of struct lifconf in which to return
* the information.
*
* Returned Value:
* Zero is returned on success; a negated errno value is returned on any
* failure.
*
* Assumptions:
* The nework is locked
*
****************************************************************************/
#ifdef CONFIG_NET_IPv6
struct lifconf; /* Forward reference */
int netdev_ipv6_ifconf(FAR struct lifconf *lifc);
#endif
/****************************************************************************
* Name: netdown_notifier_setup
*
* Description:
* Set up to perform a callback to the worker function the network goes
* down. The worker function will execute on the high priority worker
* thread.
*
* Input Parameters:
* worker - The worker function to execute on the high priority work
* queue when data is available in the UDP readahead buffer.
* dev - The network driver to be monitored
* arg - A user-defined argument that will be available to the worker
* function when it runs.
* Returned Value:
* > 0 - The signal notification is in place. The returned value is a
* key that may be used later in a call to
* netdown_notifier_teardown().
* == 0 - The the device is already down. No signal notification will
* be provided.
* < 0 - An unexpected error occurred and no signal will be sent. The
* returned value is a negated errno value that indicates the
* nature of the failure.
*
****************************************************************************/
#ifdef CONFIG_NETDOWN_NOTIFIER
int netdown_notifier_setup(worker_t worker, FAR struct net_driver_s *dev,
FAR void *arg);
#endif
/****************************************************************************
* Name: netdown_notifier_teardown
*
* Description:
* Eliminate a network down notification previously setup by
* netdown_notifier_setup(). This function should only be called if the
* notification should be aborted prior to the notification. The
* notification will automatically be torn down after the signal is sent.
*
* Input Parameters:
* key - The key value returned from a previous call to
* netdown_notifier_setup().
*
* Returned Value:
* Zero (OK) is returned on success; a negated errno value is returned on
* any failure.
*
****************************************************************************/
#ifdef CONFIG_NETDOWN_NOTIFIER
int netdown_notifier_teardown(int key);
#endif
/****************************************************************************
* Name: netdown_notifier_signal
*
* Description:
* A network has gone down has been buffered. Execute worker thread
* functions for all threads monitoring the state of the device.
*
* Input Parameters:
* dev - The TCP connection where read-ahead data was just buffered.
*
* Returned Value:
* None.
*
****************************************************************************/
#ifdef CONFIG_NETDOWN_NOTIFIER
void netdown_notifier_signal(FAR struct net_driver_s *dev);
#endif
#undef EXTERN
#ifdef __cplusplus
}
#endif
#endif /* __NET_NETDEV_NETDEV_H */