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

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

do not intermix low-level IPC methods with async framework methods

  • 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 <ipc/services.h>
42#include <ipc/nil.h>
43#include <net/modules.h>
44#include <adt/measured_strings.h>
45#include <packet_client.h>
46#include <net/device.h>
47#include <netif_skel.h>
48#include <nil_remote.h>
49
50/** Default address length. */
51#define DEFAULT_ADDR_LEN 6
52
53/** Loopback module name. */
54#define NAME "lo"
55
56static uint8_t default_addr[DEFAULT_ADDR_LEN] =
57 {0, 0, 0, 0, 0, 0};
58
59int netif_specific_message(ipc_callid_t callid, ipc_call_t *call,
60 ipc_call_t *answer, size_t *count)
61{
62 return ENOTSUP;
63}
64
65int netif_get_addr_message(device_id_t device_id, measured_string_t *address)
66{
67 if (!address)
68 return EBADMEM;
69
70 address->value = default_addr;
71 address->length = DEFAULT_ADDR_LEN;
72
73 return EOK;
74}
75
76int netif_get_device_stats(device_id_t device_id, device_stats_t *stats)
77{
78 if (!stats)
79 return EBADMEM;
80
81 netif_device_t *device;
82 int rc = find_device(device_id, &device);
83 if (rc != EOK)
84 return rc;
85
86 memcpy(stats, (device_stats_t *) device->specific,
87 sizeof(device_stats_t));
88
89 return EOK;
90}
91
92/** Change the loopback state.
93 *
94 * @param[in] device The device structure.
95 * @param[in] state The new device state.
96 *
97 * @return New state if changed.
98 * @return EOK otherwise.
99 *
100 */
101static void change_state_message(netif_device_t *device, device_state_t state)
102{
103 if (device->state != state) {
104 device->state = state;
105
106 const char *desc;
107 switch (state) {
108 case NETIF_ACTIVE:
109 desc = "active";
110 break;
111 case NETIF_STOPPED:
112 desc = "stopped";
113 break;
114 default:
115 desc = "unknown";
116 }
117
118 printf("%s: State changed to %s\n", NAME, desc);
119 }
120}
121
122/** Create and return the loopback network interface structure.
123 *
124 * @param[in] device_id New devce identifier.
125 * @param[out] device Device structure.
126 *
127 * @return EOK on success.
128 * @return EXDEV if one loopback network interface already exists.
129 * @return ENOMEM if there is not enough memory left.
130 *
131 */
132static int lo_create(device_id_t device_id, netif_device_t **device)
133{
134 if (netif_device_map_count(&netif_globals.device_map) > 0)
135 return EXDEV;
136
137 *device = (netif_device_t *) malloc(sizeof(netif_device_t));
138 if (!*device)
139 return ENOMEM;
140
141 (*device)->specific = (device_stats_t *) malloc(sizeof(device_stats_t));
142 if (!(*device)->specific) {
143 free(*device);
144 return ENOMEM;
145 }
146
147 null_device_stats((device_stats_t *) (*device)->specific);
148 (*device)->device_id = device_id;
149 (*device)->nil_phone = -1;
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 async_connect_to_me(PHONE_NS, SERVICE_LO, 0, 0, NULL);
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 int phone = device->nil_phone;
204 fibril_rwlock_write_unlock(&netif_globals.lock);
205
206 nil_received_msg(phone, 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.