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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 3c106e88 was 61bfc370, checked in by Martin Decky <martin@…>, 14 years ago
  • make measured_string and other network-related data types binary-safe
  • fix several network-related routines binary-safe (replace clearly suspicious use of str_lcmp() with bcmp())
  • rename spawn() to net_spawn()
  • Property mode set to 100644
File size: 6.9 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 <stdio.h>
40#include <str.h>
41
42#include <ipc/ipc.h>
43#include <ipc/services.h>
44#include <ipc/nil.h>
45
46#include <net/modules.h>
47#include <adt/measured_strings.h>
48#include <packet_client.h>
49#include <net/device.h>
50#include <nil_interface.h>
51#include <netif_interface.h>
52#include <netif_local.h>
53
54/** Default hardware address. */
55#define DEFAULT_ADDR 0
56
57/** Default address length. */
58#define DEFAULT_ADDR_LEN 6
59
60/** Loopback module name. */
61#define NAME "lo"
62
63/** Network interface global data. */
64netif_globals_t netif_globals;
65
66int netif_specific_message(ipc_callid_t callid, ipc_call_t *call,
67 ipc_call_t *answer, int *answer_count)
68{
69 return ENOTSUP;
70}
71
72int netif_get_addr_message(device_id_t device_id, measured_string_t *address)
73{
74 if (!address)
75 return EBADMEM;
76
77 uint8_t *addr = (uint8_t *) malloc(DEFAULT_ADDR_LEN);
78 memset(addr, DEFAULT_ADDR, DEFAULT_ADDR_LEN);
79
80 address->value = addr;
81 address->length = DEFAULT_ADDR_LEN;
82
83 return EOK;
84}
85
86int netif_get_device_stats(device_id_t device_id, device_stats_t *stats)
87{
88 netif_device_t *device;
89 int rc;
90
91 if (!stats)
92 return EBADMEM;
93
94 rc = find_device(device_id, &device);
95 if (rc != EOK)
96 return rc;
97
98 memcpy(stats, (device_stats_t *) device->specific,
99 sizeof(device_stats_t));
100
101 return EOK;
102}
103
104/** Changes the loopback state.
105 *
106 * @param[in] device The device structure.
107 * @param[in] state The new device state.
108 * @return The new state if changed.
109 * @return EOK otherwise.
110 */
111static int change_state_message(netif_device_t *device, device_state_t state)
112{
113 if (device->state != state) {
114 device->state = state;
115
116 printf("%s: State changed to %s\n", NAME,
117 (state == NETIF_ACTIVE) ? "active" : "stopped");
118
119 return state;
120 }
121
122 return EOK;
123}
124
125/** Creates and returns the loopback network interface structure.
126 *
127 * @param[in] device_id The new devce identifier.
128 * @param[out] device The device structure.
129 * @return EOK on success.
130 * @return EXDEV if one loopback network interface already exists.
131 * @return ENOMEM if there is not enough memory left.
132 */
133static int create(device_id_t device_id, netif_device_t **device)
134{
135 int index;
136
137 if (netif_device_map_count(&netif_globals.device_map) > 0)
138 return EXDEV;
139
140 *device = (netif_device_t *) malloc(sizeof(netif_device_t));
141 if (!*device)
142 return ENOMEM;
143
144 (*device)->specific = (device_stats_t *) malloc(sizeof(device_stats_t));
145 if (!(*device)->specific) {
146 free(*device);
147 return ENOMEM;
148 }
149
150 null_device_stats((device_stats_t *) (*device)->specific);
151 (*device)->device_id = device_id;
152 (*device)->nil_phone = -1;
153 (*device)->state = NETIF_STOPPED;
154 index = netif_device_map_add(&netif_globals.device_map,
155 (*device)->device_id, *device);
156
157 if (index < 0) {
158 free(*device);
159 free((*device)->specific);
160 *device = NULL;
161 return index;
162 }
163
164 return EOK;
165}
166
167int netif_initialize(void)
168{
169 sysarg_t phonehash;
170
171 return ipc_connect_to_me(PHONE_NS, SERVICE_LO, 0, 0, &phonehash);
172}
173
174int netif_probe_message(device_id_t device_id, int irq, uintptr_t io)
175{
176 netif_device_t *device;
177 int rc;
178
179 /* Create a new device */
180 rc = create(device_id, &device);
181 if (rc != EOK)
182 return rc;
183
184 /* Print the settings */
185 printf("%s: Device created (id: %d)\n", NAME, device->device_id);
186
187 return EOK;
188}
189
190int netif_send_message(device_id_t device_id, packet_t *packet, services_t sender)
191{
192 netif_device_t *device;
193 size_t length;
194 packet_t *next;
195 int phone;
196 int rc;
197
198 rc = find_device(device_id, &device);
199 if (rc != EOK)
200 return EOK;
201
202 if (device->state != NETIF_ACTIVE) {
203 netif_pq_release(packet_get_id(packet));
204 return EFORWARD;
205 }
206
207 next = packet;
208 do {
209 ((device_stats_t *) device->specific)->send_packets++;
210 ((device_stats_t *) device->specific)->receive_packets++;
211 length = packet_get_data_length(next);
212 ((device_stats_t *) device->specific)->send_bytes += length;
213 ((device_stats_t *) device->specific)->receive_bytes += length;
214 next = pq_next(next);
215 } while(next);
216
217 phone = device->nil_phone;
218 fibril_rwlock_write_unlock(&netif_globals.lock);
219 nil_received_msg(phone, device_id, packet, sender);
220 fibril_rwlock_write_lock(&netif_globals.lock);
221
222 return EOK;
223}
224
225int netif_start_message(netif_device_t *device)
226{
227 return change_state_message(device, NETIF_ACTIVE);
228}
229
230int netif_stop_message(netif_device_t *device)
231{
232 return change_state_message(device, NETIF_STOPPED);
233}
234
235/** Default thread for new connections.
236 *
237 * @param[in] iid The initial message identifier.
238 * @param[in] icall The initial message call structure.
239 */
240static void netif_client_connection(ipc_callid_t iid, ipc_call_t *icall)
241{
242 /*
243 * Accept the connection
244 * - Answer the first IPC_M_CONNECT_ME_TO call.
245 */
246 ipc_answer_0(iid, EOK);
247
248 while (true) {
249 ipc_call_t answer;
250 int answer_count;
251
252 /* Clear the answer structure */
253 refresh_answer(&answer, &answer_count);
254
255 /* Fetch the next message */
256 ipc_call_t call;
257 ipc_callid_t callid = async_get_call(&call);
258
259 /* Process the message */
260 int res = netif_module_message(NAME, callid, &call, &answer,
261 &answer_count);
262
263 /*
264 * End if told to either by the message or the processing
265 * result.
266 */
267 if ((IPC_GET_IMETHOD(call) == IPC_M_PHONE_HUNGUP) ||
268 (res == EHANGUP))
269 return;
270
271 /* Answer the message */
272 answer_call(callid, res, &answer, answer_count);
273 }
274}
275
276int main(int argc, char *argv[])
277{
278 int rc;
279
280 /* Start the module */
281 rc = netif_module_start(netif_client_connection);
282 return rc;
283}
284
285/** @}
286 */
Note: See TracBrowser for help on using the repository browser.