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

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

Move network interface layer module messages definitions to standard library.

  • Property mode set to 100644
File size: 7.0 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 */
57#define DEFAULT_ADDR "\0\0\0\0\0\0"
58
59/** Default address length.
60 */
61#define DEFAULT_ADDR_LEN (sizeof(DEFAULT_ADDR) / sizeof(char))
62
63/** Loopback module name.
64 */
65#define NAME "lo"
66
67/** Network interface global data.
68 */
69netif_globals_t netif_globals;
70
71/** Changes the loopback state.
72 * @param[in] device The device structure.
73 * @param[in] state The new device state.
74 * @returns The new state if changed.
75 * @returns EOK otherwise.
76 */
77int change_state_message(netif_device_t * device, device_state_t state);
78
79/** Creates and returns the loopback network interface structure.
80 * @param[in] device_id The new devce identifier.
81 * @param[out] device The device structure.
82 * @returns EOK on success.
83 * @returns EXDEV if one loopback network interface already exists.
84 * @returns ENOMEM if there is not enough memory left.
85 */
86int create(device_id_t device_id, netif_device_t * * device);
87
88int netif_specific_message(ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count){
89 return ENOTSUP;
90}
91
92int netif_get_addr_message(device_id_t device_id, measured_string_ref address){
93 if(! address){
94 return EBADMEM;
95 }
96 address->value = str_dup(DEFAULT_ADDR);
97 address->length = DEFAULT_ADDR_LEN;
98 return EOK;
99}
100
101int netif_get_device_stats(device_id_t device_id, device_stats_ref stats){
102 ERROR_DECLARE;
103
104 netif_device_t * device;
105
106 if(! stats){
107 return EBADMEM;
108 }
109 ERROR_PROPAGATE(find_device(device_id, &device));
110 memcpy(stats, (device_stats_ref) device->specific, sizeof(device_stats_t));
111 return EOK;
112}
113
114int change_state_message(netif_device_t * device, device_state_t state)
115{
116 if (device->state != state) {
117 device->state = state;
118
119 printf("%s: State changed to %s\n", NAME,
120 (state == NETIF_ACTIVE) ? "active" : "stopped");
121
122 return state;
123 }
124
125 return EOK;
126}
127
128int create(device_id_t device_id, netif_device_t * * device){
129 int index;
130
131 if(netif_device_map_count(&netif_globals.device_map) > 0){
132 return EXDEV;
133 }else{
134 *device = (netif_device_t *) malloc(sizeof(netif_device_t));
135 if(!(*device)){
136 return ENOMEM;
137 }
138 (** device).specific = malloc(sizeof(device_stats_t));
139 if(! (** device).specific){
140 free(*device);
141 return ENOMEM;
142 }
143 null_device_stats((device_stats_ref)(** device).specific);
144 (** device).device_id = device_id;
145 (** device).nil_phone = -1;
146 (** device).state = NETIF_STOPPED;
147 index = netif_device_map_add(&netif_globals.device_map, (** device).device_id, * device);
148 if(index < 0){
149 free(*device);
150 free((** device).specific);
151 *device = NULL;
152 return index;
153 }
154 }
155 return EOK;
156}
157
158int netif_initialize(void){
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 ERROR_DECLARE;
166
167 netif_device_t * device;
168
169 // create a new device
170 ERROR_PROPAGATE(create(device_id, &device));
171 // print the settings
172 printf("%s: Device created (id: %d)\n", NAME, device->device_id);
173 return EOK;
174}
175
176int netif_send_message(device_id_t device_id, packet_t packet, services_t sender){
177 ERROR_DECLARE;
178
179 netif_device_t * device;
180 size_t length;
181 packet_t next;
182 int phone;
183
184 ERROR_PROPAGATE(find_device(device_id, &device));
185 if(device->state != NETIF_ACTIVE){
186 netif_pq_release(packet_get_id(packet));
187 return EFORWARD;
188 }
189 next = packet;
190 do{
191 ++ ((device_stats_ref) device->specific)->send_packets;
192 ++ ((device_stats_ref) device->specific)->receive_packets;
193 length = packet_get_data_length(next);
194 ((device_stats_ref) device->specific)->send_bytes += length;
195 ((device_stats_ref) device->specific)->receive_bytes += length;
196 next = pq_next(next);
197 }while(next);
198 phone = device->nil_phone;
199 fibril_rwlock_write_unlock(&netif_globals.lock);
200 nil_received_msg(phone, device_id, packet, sender);
201 fibril_rwlock_write_lock(&netif_globals.lock);
202 return EOK;
203}
204
205int netif_start_message(netif_device_t * device){
206 return change_state_message(device, NETIF_ACTIVE);
207}
208
209int netif_stop_message(netif_device_t * device){
210 return change_state_message(device, NETIF_STOPPED);
211}
212
213/** Default thread for new connections.
214 *
215 * @param[in] iid The initial message identifier.
216 * @param[in] icall The initial message call structure.
217 *
218 */
219static void netif_client_connection(ipc_callid_t iid, ipc_call_t *icall)
220{
221 /*
222 * Accept the connection
223 * - Answer the first IPC_M_CONNECT_ME_TO call.
224 */
225 ipc_answer_0(iid, EOK);
226
227 while(true) {
228 ipc_call_t answer;
229 int answer_count;
230
231 /* Clear the answer structure */
232 refresh_answer(&answer, &answer_count);
233
234 /* Fetch the next message */
235 ipc_call_t call;
236 ipc_callid_t callid = async_get_call(&call);
237
238 /* Process the message */
239 int res = netif_module_message(NAME, callid, &call, &answer,
240 &answer_count);
241
242 /* End if said to either by the message or the processing result */
243 if ((IPC_GET_METHOD(call) == IPC_M_PHONE_HUNGUP) || (res == EHANGUP))
244 return;
245
246 /* Answer the message */
247 answer_call(callid, res, &answer, answer_count);
248 }
249}
250
251int main(int argc, char *argv[])
252{
253 ERROR_DECLARE;
254
255 /* Start the module */
256 if (ERROR_OCCURRED(netif_module_start(netif_client_connection)))
257 return ERROR_CODE;
258
259 return EOK;
260}
261
262/** @}
263 */
Note: See TracBrowser for help on using the repository browser.