iPXE
netdev_test.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2025 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
24FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25
26/** @file
27 *
28 * Network device tests
29 *
30 */
31
32/* Forcibly enable assertions */
33#undef NDEBUG
34
35#include <string.h>
36#include <stdio.h>
37#include <ipxe/netdevice.h>
38#include <ipxe/ethernet.h>
39#include <ipxe/if_ether.h>
40#include <ipxe/base16.h>
41#include <ipxe/test.h>
42#include "netdev_test.h"
43
44/**
45 * Open network device
46 *
47 * @v netdev Network device
48 * @ret rc Return status code
49 */
50static int testnet_open ( struct net_device *netdev __unused ) {
51
52 /* Do nothing, successfully */
53 return 0;
54}
55
56/**
57 * Close network device
58 *
59 * @v netdev Network device
60 */
61static void testnet_close ( struct net_device *netdev __unused ) {
62
63 /* Do nothing */
64}
65
66/**
67 * Transmit packet
68 *
69 * @v netdev Network device
70 * @v iobuf I/O buffer
71 * @ret rc Return status code
72 */
73static int testnet_transmit ( struct net_device *netdev,
74 struct io_buffer *iobuf ) {
75
76 /* Complete immediately */
77 netdev_tx_complete ( netdev, iobuf );
78 return 0;
79}
80
81/**
82 * Poll for completed and received packets
83 *
84 * @v netdev Network device
85 */
86static void testnet_poll ( struct net_device *netdev __unused ) {
87
88 /* Do nothing */
89}
90
91/** Test network device operations */
93 .open = testnet_open,
94 .close = testnet_close,
95 .transmit = testnet_transmit,
96 .poll = testnet_poll,
97};
98
99/**
100 * Report a network device creation test result
101 *
102 * @v testnet Test network device
103 * @v file Test code file
104 * @v line Test code line
105 */
106void testnet_okx ( struct testnet *testnet, const char *file,
107 unsigned int line ) {
108 struct testnet_setting *testset;
109 unsigned int i;
110
111 /* Allocate device */
113 okx ( testnet->netdev != NULL, file, line );
116 snprintf ( testnet->netdev->name, sizeof ( testnet->netdev->name ),
117 "%s", testnet->dev.name );
119 ETH_ALEN ) == ETH_ALEN, file, line );
120
121 /* Register device */
122 okx ( register_netdev ( testnet->netdev ) == 0, file, line );
123
124 /* Open device */
125 testnet_open_okx ( testnet, file, line );
126
127 /* Apply initial settings */
128 for ( i = 0 ; i < testnet->count ; i++ ) {
129 testset = &testnet->testset[i];
130 testnet_set_okx ( testnet, testset->name, testset->value,
131 file, line );
132 }
133}
134
135/**
136 * Report a network device opening test result
137 *
138 * @v testnet Test network device
139 * @v file Test code file
140 * @v line Test code line
141 */
142void testnet_open_okx ( struct testnet *testnet, const char *file,
143 unsigned int line ) {
144
145 /* Sanity check */
146 okx ( testnet->netdev != NULL, file, line );
147
148 /* Open device */
149 okx ( netdev_open ( testnet->netdev ) == 0, file, line );
150}
151
152/**
153 * Report a network device setting test result
154 *
155 * @v testnet Test network device
156 * @v name Setting name (relative to network device's settings)
157 * @v value Setting value
158 * @v file Test code file
159 * @v line Test code line
160 */
161void testnet_set_okx ( struct testnet *testnet, const char *name,
162 const char *value, const char *file,
163 unsigned int line ) {
164 char fullname[ strlen ( testnet->dev.name ) + 1 /* "." or "/" */ +
165 strlen ( name ) + 1 /* NUL */ ];
166 struct settings *settings;
167 struct setting setting;
168
169 /* Sanity check */
170 okx ( testnet->netdev != NULL, file, line );
172 okx ( settings != NULL, file, line );
173 okx ( strcmp ( settings->name, testnet->dev.name ) == 0, file, line );
174
175 /* Construct setting name */
176 snprintf ( fullname, sizeof ( fullname ), "%s%c%s", testnet->dev.name,
177 ( strchr ( name, '/' ) ? '.' : '/' ), name );
178
179 /* Parse setting name */
181 &settings, &setting ) == 0, file, line );
182
183 /* Apply setting */
184 okx ( storef_setting ( settings, &setting, value ) == 0, file, line );
185}
186
187/**
188 * Report a network device closing test result
189 *
190 * @v testnet Test network device
191 * @v file Test code file
192 * @v line Test code line
193 */
194void testnet_close_okx ( struct testnet *testnet, const char *file,
195 unsigned int line ) {
196
197 /* Sanity check */
198 okx ( testnet->netdev != NULL, file, line );
199
200 /* Close device */
202}
203
204/**
205 * Report a network device removal test result
206 *
207 * @v testnet Test network device
208 * @v file Test code file
209 * @v line Test code line
210 */
211void testnet_remove_okx ( struct testnet *testnet, const char *file,
212 unsigned int line ) {
213
214 /* Sanity check */
215 okx ( testnet->netdev != NULL, file, line );
216
217 /* Remove device */
222}
#define NULL
NULL pointer (VOID *)
Definition Base.h:322
pseudo_bit_t value[0x00020]
Definition arbel.h:2
const char * name
Definition ath9k_hw.c:1986
int hex_decode(char separator, const char *encoded, void *data, size_t len)
Decode hexadecimal string (with optional byte separator character)
Definition base16.c:77
Base16 encoding.
struct net_device * alloc_etherdev(size_t priv_size)
Allocate Ethernet device.
Definition ethernet.c:265
Ethernet protocol.
static struct net_device * netdev
Definition gdbudp.c:53
#define __unused
Declare a variable or data structure as unused.
Definition compiler.h:573
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
Definition compiler.h:896
#define ETH_ALEN
Definition if_ether.h:9
String functions.
void testnet_remove_okx(struct testnet *testnet, const char *file, unsigned int line)
Report a network device removal test result.
static struct net_device_operations testnet_operations
Test network device operations.
Definition netdev_test.c:92
static int testnet_transmit(struct net_device *netdev, struct io_buffer *iobuf)
Transmit packet.
Definition netdev_test.c:73
void testnet_set_okx(struct testnet *testnet, const char *name, const char *value, const char *file, unsigned int line)
Report a network device setting test result.
void testnet_okx(struct testnet *testnet, const char *file, unsigned int line)
Report a network device creation test result.
void testnet_open_okx(struct testnet *testnet, const char *file, unsigned int line)
Report a network device opening test result.
void testnet_close_okx(struct testnet *testnet, const char *file, unsigned int line)
Report a network device closing test result.
static void testnet_close(struct net_device *netdev __unused)
Close network device.
Definition netdev_test.c:61
static void testnet_poll(struct net_device *netdev __unused)
Poll for completed and received packets.
Definition netdev_test.c:86
static int testnet_open(struct net_device *netdev __unused)
Open network device.
Definition netdev_test.c:50
Network device tests.
void unregister_netdev(struct net_device *netdev)
Unregister network device.
Definition netdevice.c:946
int netdev_open(struct net_device *netdev)
Open network device.
Definition netdevice.c:866
void netdev_close(struct net_device *netdev)
Close network device.
Definition netdevice.c:900
int register_netdev(struct net_device *netdev)
Register network device.
Definition netdevice.c:760
Network device management.
static void netdev_init(struct net_device *netdev, struct net_device_operations *op)
Initialise a network device.
Definition netdevice.h:522
static void netdev_nullify(struct net_device *netdev)
Stop using a network device.
Definition netdevice.h:535
static void netdev_put(struct net_device *netdev)
Drop reference to network device.
Definition netdevice.h:579
static void netdev_tx_complete(struct net_device *netdev, struct io_buffer *iobuf)
Complete network transmission.
Definition netdevice.h:770
static struct settings * netdev_settings(struct net_device *netdev)
Get per-netdevice configuration settings block.
Definition netdevice.h:590
struct settings * autovivify_child_settings(struct settings *parent, const char *name)
Find or create child settings block.
Definition settings.c:307
int parse_setting_name(char *name, get_child_settings_t get_child, struct settings **settings, struct setting *setting)
Parse setting name.
Definition settings.c:1529
int storef_setting(struct settings *settings, const struct setting *setting, const char *value)
Store formatted value of setting.
Definition settings.c:1320
int strcmp(const char *first, const char *second)
Compare strings.
Definition string.c:174
char * strchr(const char *src, int character)
Find character within a string.
Definition string.c:272
size_t strlen(const char *src)
Get length of string.
Definition string.c:244
char name[40]
Name.
Definition device.h:79
A persistent I/O buffer.
Definition iobuf.h:38
Network device operations.
Definition netdevice.h:214
A network device.
Definition netdevice.h:353
uint8_t hw_addr[MAX_HW_ADDR_LEN]
Hardware address.
Definition netdevice.h:382
char name[NETDEV_NAME_LEN]
Name of this network device.
Definition netdevice.h:363
struct device * dev
Underlying hardware device.
Definition netdevice.h:365
A setting.
Definition settings.h:24
A settings block.
Definition settings.h:133
const char * name
Name.
Definition settings.h:137
A test network device setting.
Definition netdev_test.h:16
const char * name
Setting name (relative to network device's settings)
Definition netdev_test.h:18
const char * value
Value.
Definition netdev_test.h:20
A test network device.
Definition netdev_test.h:24
const char * hwaddr
MAC address.
Definition netdev_test.h:30
unsigned int count
Number of initial settings.
Definition netdev_test.h:34
struct net_device * netdev
Network device.
Definition netdev_test.h:26
struct device dev
Dummy physical device.
Definition netdev_test.h:28
struct testnet_setting * testset
Initial settings.
Definition netdev_test.h:32
Self-test infrastructure.
#define okx(success, file, line)
Report test result.
Definition test.h:44
int snprintf(char *buf, size_t size, const char *fmt,...)
Write a formatted string to a buffer.
Definition vsprintf.c:383