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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 1bfd3d3 was 1bfd3d3, checked in by Jiri Svoboda <jiri@…>, 15 years ago

Replace @returns with @return.

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