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

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

Rename string.h to str.h to avoid header conflict with standard C string.h.

  • Property mode set to 100644
File size: 6.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 <stdio.h>
40#include <str.h>
41
42#include <ipc/ipc.h>
43#include <ipc/services.h>
44
45#include "../../err.h"
46#include "../../messages.h"
47#include "../../modules.h"
48
49#include "../../structures/measured_strings.h"
50#include "../../structures/packet/packet_client.h"
51
52#include "../../include/device.h"
53#include "../../include/nil_interface.h"
54
55#include "../../nil/nil_messages.h"
56
57#include "../netif.h"
58#include "../netif_module.h"
59
60/** Default hardware address.
61 */
62#define DEFAULT_ADDR "\0\0\0\0\0\0"
63
64/** Default address length.
65 */
66#define DEFAULT_ADDR_LEN (sizeof(DEFAULT_ADDR) / sizeof(char))
67
68/** Loopback module name.
69 */
70#define NAME "lo - loopback interface"
71
72/** Network interface global data.
73 */
74netif_globals_t netif_globals;
75
76/** Changes the loopback state.
77 * @param[in] device The device structure.
78 * @param[in] state The new device state.
79 * @returns The new state if changed.
80 * @returns EOK otherwise.
81 */
82int change_state_message(device_ref device, device_state_t state);
83
84/** Creates and returns the loopback network interface structure.
85 * @param[in] device_id The new devce identifier.
86 * @param[out] device The device structure.
87 * @returns EOK on success.
88 * @returns EXDEV if one loopback network interface already exists.
89 * @returns ENOMEM if there is not enough memory left.
90 */
91int create(device_id_t device_id, device_ref * device);
92
93/** Prints the module name.
94 * @see NAME
95 */
96void module_print_name(void);
97
98int netif_specific_message(ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count){
99 return ENOTSUP;
100}
101
102int netif_get_addr_message(device_id_t device_id, measured_string_ref address){
103 if(! address){
104 return EBADMEM;
105 }
106 address->value = str_dup(DEFAULT_ADDR);
107 address->length = DEFAULT_ADDR_LEN;
108 return EOK;
109}
110
111int netif_get_device_stats(device_id_t device_id, device_stats_ref stats){
112 ERROR_DECLARE;
113
114 device_ref device;
115
116 if(! stats){
117 return EBADMEM;
118 }
119 ERROR_PROPAGATE(find_device(device_id, &device));
120 memcpy(stats, (device_stats_ref) device->specific, sizeof(device_stats_t));
121 return EOK;
122}
123
124int change_state_message(device_ref device, device_state_t state){
125 if(device->state != state){
126 device->state = state;
127 printf("State changed to %s\n", (state == NETIF_ACTIVE) ? "ACTIVE" : "STOPPED");
128 return state;
129 }
130 return EOK;
131}
132
133int create(device_id_t device_id, device_ref * device){
134 int index;
135
136 if(device_map_count(&netif_globals.device_map) > 0){
137 return EXDEV;
138 }else{
139 *device = (device_ref) malloc(sizeof(device_t));
140 if(!(*device)){
141 return ENOMEM;
142 }
143 (** device).specific = malloc(sizeof(device_stats_t));
144 if(! (** device).specific){
145 free(*device);
146 return ENOMEM;
147 }
148 null_device_stats((device_stats_ref)(** device).specific);
149 (** device).device_id = device_id;
150 (** device).nil_phone = -1;
151 (** device).state = NETIF_STOPPED;
152 index = device_map_add(&netif_globals.device_map, (** device).device_id, * device);
153 if(index < 0){
154 free(*device);
155 free((** device).specific);
156 *device = NULL;
157 return index;
158 }
159 }
160 return EOK;
161}
162
163int netif_initialize(void){
164 ipcarg_t phonehash;
165
166 return REGISTER_ME(SERVICE_LO, &phonehash);
167}
168
169void module_print_name(void){
170 printf("%s", NAME);
171}
172
173int netif_probe_message(device_id_t device_id, int irq, uintptr_t io){
174 ERROR_DECLARE;
175
176 device_ref device;
177
178 // create a new device
179 ERROR_PROPAGATE(create(device_id, &device));
180 // print the settings
181 printf("New device created:\n\tid\t= %d\n", device->device_id);
182 return EOK;
183}
184
185int netif_send_message(device_id_t device_id, packet_t packet, services_t sender){
186 ERROR_DECLARE;
187
188 device_ref device;
189 size_t length;
190 packet_t next;
191 int phone;
192
193 ERROR_PROPAGATE(find_device(device_id, &device));
194 if(device->state != NETIF_ACTIVE){
195 netif_pq_release(packet_get_id(packet));
196 return EFORWARD;
197 }
198 next = packet;
199 do{
200 ++ ((device_stats_ref) device->specific)->send_packets;
201 ++ ((device_stats_ref) device->specific)->receive_packets;
202 length = packet_get_data_length(next);
203 ((device_stats_ref) device->specific)->send_bytes += length;
204 ((device_stats_ref) device->specific)->receive_bytes += length;
205 next = pq_next(next);
206 }while(next);
207 phone = device->nil_phone;
208 fibril_rwlock_write_unlock(&netif_globals.lock);
209 nil_received_msg(phone, device_id, packet, sender);
210 fibril_rwlock_write_lock(&netif_globals.lock);
211 return EOK;
212}
213
214int netif_start_message(device_ref device){
215 return change_state_message(device, NETIF_ACTIVE);
216}
217
218int netif_stop_message(device_ref device){
219 return change_state_message(device, NETIF_STOPPED);
220}
221
222/** @}
223 */
Note: See TracBrowser for help on using the repository browser.