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

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

networking: streamline NIL skeleton and NIL modules (nildummy, eth)

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