source: mainline/uspace/srv/net/netif/lo/lo.c@ c4fbf7fd

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since c4fbf7fd was c4fbf7fd, checked in by Jakub Jermar <jakub@…>, 15 years ago

Cleanup lo.

  • Property mode set to 100644
File size: 6.8 KB
Line 
1/*
2 * Copyright (c) 2009 Lukas Mejdrech
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup lo
30 * @{
31 */
32
33/** @file
34 * Loopback network interface implementation.
35 */
36
37#include <async.h>
38#include <errno.h>
39#include <err.h>
40#include <stdio.h>
41#include <str.h>
42
43#include <ipc/ipc.h>
44#include <ipc/services.h>
45#include <ipc/nil.h>
46
47#include <net/modules.h>
48#include <adt/measured_strings.h>
49#include <packet_client.h>
50#include <net/device.h>
51#include <nil_interface.h>
52#include <netif_interface.h>
53#include <netif_local.h>
54
55/** Default hardware address. */
56#define DEFAULT_ADDR "\0\0\0\0\0\0"
57
58/** Default address length. */
59#define DEFAULT_ADDR_LEN (sizeof(DEFAULT_ADDR) / sizeof(char))
60
61/** Loopback module name. */
62#define NAME "lo"
63
64/** Network interface global data. */
65netif_globals_t netif_globals;
66
67int
68netif_specific_message(ipc_callid_t callid, ipc_call_t *call,
69 ipc_call_t *answer, int *answer_count)
70{
71 return ENOTSUP;
72}
73
74int netif_get_addr_message(device_id_t device_id, measured_string_ref address)
75{
76 if (!address)
77 return EBADMEM;
78 address->value = str_dup(DEFAULT_ADDR);
79 address->length = DEFAULT_ADDR_LEN;
80 return EOK;
81}
82
83int netif_get_device_stats(device_id_t device_id, device_stats_ref stats)
84{
85 ERROR_DECLARE;
86
87 netif_device_t *device;
88
89 if (!stats)
90 return EBADMEM;
91 ERROR_PROPAGATE(find_device(device_id, &device));
92 memcpy(stats, (device_stats_ref) device->specific,
93 sizeof(device_stats_t));
94 return EOK;
95}
96
97/** Changes the loopback state.
98 *
99 * @param[in] device The device structure.
100 * @param[in] state The new device state.
101 * @returns The new state if changed.
102 * @returns EOK otherwise.
103 */
104static int change_state_message(netif_device_t *device, device_state_t state)
105{
106 if (device->state != state) {
107 device->state = state;
108
109 printf("%s: State changed to %s\n", NAME,
110 (state == NETIF_ACTIVE) ? "active" : "stopped");
111
112 return state;
113 }
114
115 return EOK;
116}
117
118/** Creates and returns the loopback network interface structure.
119 *
120 * @param[in] device_id The new devce identifier.
121 * @param[out] device The device structure.
122 * @returns EOK on success.
123 * @returns EXDEV if one loopback network interface already exists.
124 * @returns ENOMEM if there is not enough memory left.
125 */
126static int create(device_id_t device_id, netif_device_t **device)
127{
128 int index;
129
130 if (netif_device_map_count(&netif_globals.device_map) > 0)
131 return EXDEV;
132
133 *device = (netif_device_t *) malloc(sizeof(netif_device_t));
134 if (!*device)
135 return ENOMEM;
136 (*device)->specific = (device_stats_t *) malloc(sizeof(device_stats_t));
137 if (!(*device)->specific) {
138 free(*device);
139 return ENOMEM;
140 }
141 null_device_stats((device_stats_ref) (*device)->specific);
142 (*device)->device_id = device_id;
143 (*device)->nil_phone = -1;
144 (*device)->state = NETIF_STOPPED;
145 index = netif_device_map_add(&netif_globals.device_map,
146 (*device)->device_id, *device);
147 if (index < 0) {
148 free(*device);
149 free((*device)->specific);
150 *device = NULL;
151 return index;
152 }
153
154 return EOK;
155}
156
157int netif_initialize(void)
158{
159 ipcarg_t phonehash;
160
161 return REGISTER_ME(SERVICE_LO, &phonehash);
162}
163
164int netif_probe_message(device_id_t device_id, int irq, uintptr_t io)
165{
166 ERROR_DECLARE;
167
168 netif_device_t * device;
169
170 // create a new device
171 ERROR_PROPAGATE(create(device_id, &device));
172 // print the settings
173 printf("%s: Device created (id: %d)\n", NAME, device->device_id);
174 return EOK;
175}
176
177int netif_send_message(device_id_t device_id, packet_t packet, services_t sender)
178{
179 ERROR_DECLARE;
180
181 netif_device_t *device;
182 size_t length;
183 packet_t next;
184 int phone;
185
186 ERROR_PROPAGATE(find_device(device_id, &device));
187 if (device->state != NETIF_ACTIVE) {
188 netif_pq_release(packet_get_id(packet));
189 return EFORWARD;
190 }
191 next = packet;
192 do {
193 ((device_stats_ref) device->specific)->send_packets++;
194 ((device_stats_ref) device->specific)->receive_packets++;
195 length = packet_get_data_length(next);
196 ((device_stats_ref) device->specific)->send_bytes += length;
197 ((device_stats_ref) device->specific)->receive_bytes += length;
198 next = pq_next(next);
199 } while(next);
200 phone = device->nil_phone;
201 fibril_rwlock_write_unlock(&netif_globals.lock);
202 nil_received_msg(phone, device_id, packet, sender);
203 fibril_rwlock_write_lock(&netif_globals.lock);
204
205 return EOK;
206}
207
208int netif_start_message(netif_device_t *device)
209{
210 return change_state_message(device, NETIF_ACTIVE);
211}
212
213int netif_stop_message(netif_device_t *device)
214{
215 return change_state_message(device, NETIF_STOPPED);
216}
217
218/** Default thread for new connections.
219 *
220 * @param[in] iid The initial message identifier.
221 * @param[in] icall The initial message call structure.
222 */
223static void netif_client_connection(ipc_callid_t iid, ipc_call_t *icall)
224{
225 /*
226 * Accept the connection
227 * - Answer the first IPC_M_CONNECT_ME_TO call.
228 */
229 ipc_answer_0(iid, EOK);
230
231 while (true) {
232 ipc_call_t answer;
233 int answer_count;
234
235 /* Clear the answer structure */
236 refresh_answer(&answer, &answer_count);
237
238 /* Fetch the next message */
239 ipc_call_t call;
240 ipc_callid_t callid = async_get_call(&call);
241
242 /* Process the message */
243 int res = netif_module_message(NAME, callid, &call, &answer,
244 &answer_count);
245
246 /*
247 * End if said to either by the message or the processing
248 * result.
249 */
250 if ((IPC_GET_METHOD(call) == IPC_M_PHONE_HUNGUP) ||
251 (res == EHANGUP))
252 return;
253
254 /* Answer the message */
255 answer_call(callid, res, &answer, answer_count);
256 }
257}
258
259int main(int argc, char *argv[])
260{
261 ERROR_DECLARE;
262
263 /* Start the module */
264 if (ERROR_OCCURRED(netif_module_start(netif_client_connection)))
265 return ERROR_CODE;
266
267 return EOK;
268}
269
270/** @}
271 */
Note: See TracBrowser for help on using the repository browser.