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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 8023571 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
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
30 * @{
31 */
32
33/** @file
34 * Loopback network interface implementation.
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>
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 */
[aadf01e]66#define DEFAULT_ADDR_LEN (sizeof(DEFAULT_ADDR) / sizeof(char))
[21580dd]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 */
[aadf01e]82int change_state_message(device_ref device, device_state_t state);
[21580dd]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 */
[aadf01e]91int create(device_id_t device_id, device_ref * device);
[21580dd]92
93/** Prints the module name.
94 * @see NAME
95 */
[aadf01e]96void module_print_name(void);
[21580dd]97
[aadf01e]98int netif_specific_message(ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count){
[21580dd]99 return ENOTSUP;
100}
101
[aadf01e]102int netif_get_addr_message(device_id_t device_id, measured_string_ref address){
103 if(! address){
104 return EBADMEM;
105 }
[a000878c]106 address->value = str_dup(DEFAULT_ADDR);
[21580dd]107 address->length = DEFAULT_ADDR_LEN;
108 return EOK;
109}
110
[aadf01e]111int netif_get_device_stats(device_id_t device_id, device_stats_ref stats){
[21580dd]112 ERROR_DECLARE;
113
[aadf01e]114 device_ref device;
[21580dd]115
[aadf01e]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));
[21580dd]121 return EOK;
122}
123
[aadf01e]124int change_state_message(device_ref device, device_state_t state){
125 if(device->state != state){
[21580dd]126 device->state = state;
[aadf01e]127 printf("State changed to %s\n", (state == NETIF_ACTIVE) ? "ACTIVE" : "STOPPED");
[21580dd]128 return state;
129 }
130 return EOK;
131}
132
[aadf01e]133int create(device_id_t device_id, device_ref * device){
134 int index;
[21580dd]135
[aadf01e]136 if(device_map_count(&netif_globals.device_map) > 0){
[21580dd]137 return EXDEV;
138 }else{
[aadf01e]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);
[21580dd]146 return ENOMEM;
147 }
[aadf01e]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;
[21580dd]157 return index;
158 }
159 }
160 return EOK;
161}
162
[aadf01e]163int netif_initialize(void){
164 ipcarg_t phonehash;
[21580dd]165
[aadf01e]166 return REGISTER_ME(SERVICE_LO, &phonehash);
[21580dd]167}
168
[aadf01e]169void module_print_name(void){
170 printf("%s", NAME);
[21580dd]171}
172
[aadf01e]173int netif_probe_message(device_id_t device_id, int irq, uintptr_t io){
[21580dd]174 ERROR_DECLARE;
175
[aadf01e]176 device_ref device;
[21580dd]177
178 // create a new device
[aadf01e]179 ERROR_PROPAGATE(create(device_id, &device));
[21580dd]180 // print the settings
[aadf01e]181 printf("New device created:\n\tid\t= %d\n", device->device_id);
[21580dd]182 return EOK;
183}
184
[aadf01e]185int netif_send_message(device_id_t device_id, packet_t packet, services_t sender){
[21580dd]186 ERROR_DECLARE;
187
[aadf01e]188 device_ref device;
189 size_t length;
190 packet_t next;
191 int phone;
[21580dd]192
[aadf01e]193 ERROR_PROPAGATE(find_device(device_id, &device));
194 if(device->state != NETIF_ACTIVE){
195 netif_pq_release(packet_get_id(packet));
[21580dd]196 return EFORWARD;
197 }
198 next = packet;
199 do{
[aadf01e]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);
[21580dd]207 phone = device->nil_phone;
[aadf01e]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);
[21580dd]211 return EOK;
212}
213
[aadf01e]214int netif_start_message(device_ref device){
215 return change_state_message(device, NETIF_ACTIVE);
[21580dd]216}
217
[aadf01e]218int netif_stop_message(device_ref device){
219 return change_state_message(device, NETIF_STOPPED);
[21580dd]220}
221
222/** @}
223 */
Note: See TracBrowser for help on using the repository browser.