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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since a347a11 was ee2fa30a, checked in by Martin Decky <martin@…>, 15 years ago

undo development effort which was neither finished nor properly debugged yet
(this unbreaks networking once again)

  • Property mode set to 100644
File size: 5.7 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#include <ns.h>
42#include <ipc/services.h>
43#include <ipc/nil.h>
44#include <net/modules.h>
45#include <adt/measured_strings.h>
46#include <packet_client.h>
47#include <net/device.h>
48#include <netif_skel.h>
49#include <nil_remote.h>
50
51/** Default address length. */
52#define DEFAULT_ADDR_LEN 6
53
54/** Loopback module name. */
55#define NAME "lo"
56
57static uint8_t default_addr[DEFAULT_ADDR_LEN] =
58 {0, 0, 0, 0, 0, 0};
59
60int netif_specific_message(ipc_callid_t callid, ipc_call_t *call,
61 ipc_call_t *answer, size_t *count)
62{
63 return ENOTSUP;
64}
65
66int netif_get_addr_message(device_id_t device_id, measured_string_t *address)
67{
68 if (!address)
69 return EBADMEM;
70
71 address->value = default_addr;
72 address->length = DEFAULT_ADDR_LEN;
73
74 return EOK;
75}
76
77int netif_get_device_stats(device_id_t device_id, device_stats_t *stats)
78{
79 if (!stats)
80 return EBADMEM;
81
82 netif_device_t *device;
83 int rc = find_device(device_id, &device);
84 if (rc != EOK)
85 return rc;
86
87 memcpy(stats, (device_stats_t *) device->specific,
88 sizeof(device_stats_t));
89
90 return EOK;
91}
92
93/** Change the loopback state.
94 *
95 * @param[in] device The device structure.
96 * @param[in] state The new device state.
97 *
98 * @return New state if changed.
99 * @return EOK otherwise.
100 *
101 */
102static void change_state_message(netif_device_t *device, device_state_t state)
103{
104 if (device->state != state) {
105 device->state = state;
106
107 const char *desc;
108 switch (state) {
109 case NETIF_ACTIVE:
110 desc = "active";
111 break;
112 case NETIF_STOPPED:
113 desc = "stopped";
114 break;
115 default:
116 desc = "unknown";
117 }
118
119 printf("%s: State changed to %s\n", NAME, desc);
120 }
121}
122
123/** Create and return the loopback network interface structure.
124 *
125 * @param[in] device_id New devce identifier.
126 * @param[out] device Device structure.
127 *
128 * @return EOK on success.
129 * @return EXDEV if one loopback network interface already exists.
130 * @return ENOMEM if there is not enough memory left.
131 *
132 */
133static int lo_create(device_id_t device_id, netif_device_t **device)
134{
135 if (netif_device_map_count(&netif_globals.device_map) > 0)
136 return EXDEV;
137
138 *device = (netif_device_t *) malloc(sizeof(netif_device_t));
139 if (!*device)
140 return ENOMEM;
141
142 (*device)->specific = (device_stats_t *) malloc(sizeof(device_stats_t));
143 if (!(*device)->specific) {
144 free(*device);
145 return ENOMEM;
146 }
147
148 null_device_stats((device_stats_t *) (*device)->specific);
149 (*device)->device_id = device_id;
150 (*device)->state = NETIF_STOPPED;
151 int index = netif_device_map_add(&netif_globals.device_map,
152 (*device)->device_id, *device);
153
154 if (index < 0) {
155 free(*device);
156 free((*device)->specific);
157 *device = NULL;
158 return index;
159 }
160
161 return EOK;
162}
163
164int netif_initialize(void)
165{
166 return service_register(SERVICE_LO);
167}
168
169int netif_probe_message(device_id_t device_id, int irq, void *io)
170{
171 /* Create a new device */
172 netif_device_t *device;
173 int rc = lo_create(device_id, &device);
174 if (rc != EOK)
175 return rc;
176
177 printf("%s: Device created (id: %d)\n", NAME, device->device_id);
178 return EOK;
179}
180
181int netif_send_message(device_id_t device_id, packet_t *packet, services_t sender)
182{
183 netif_device_t *device;
184 int rc = find_device(device_id, &device);
185 if (rc != EOK)
186 return EOK;
187
188 if (device->state != NETIF_ACTIVE) {
189 netif_pq_release(packet_get_id(packet));
190 return EFORWARD;
191 }
192
193 packet_t *next = packet;
194 do {
195 ((device_stats_t *) device->specific)->send_packets++;
196 ((device_stats_t *) device->specific)->receive_packets++;
197 size_t length = packet_get_data_length(next);
198 ((device_stats_t *) device->specific)->send_bytes += length;
199 ((device_stats_t *) device->specific)->receive_bytes += length;
200 next = pq_next(next);
201 } while (next);
202
203 async_sess_t *nil_sess = netif_globals.nil_sess;
204 fibril_rwlock_write_unlock(&netif_globals.lock);
205
206 nil_received_msg(nil_sess, device_id, packet, sender);
207
208 fibril_rwlock_write_lock(&netif_globals.lock);
209 return EOK;
210}
211
212int netif_start_message(netif_device_t *device)
213{
214 change_state_message(device, NETIF_ACTIVE);
215 return device->state;
216}
217
218int netif_stop_message(netif_device_t *device)
219{
220 change_state_message(device, NETIF_STOPPED);
221 return device->state;
222}
223
224int main(int argc, char *argv[])
225{
226 /* Start the module */
227 return netif_module_start();
228}
229
230/** @}
231 */
Note: See TracBrowser for help on using the repository browser.