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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 11bb813 was 124c061, checked in by Jakub Jermar <jakub@…>, 14 years ago

ipc_connect_to_me() now takes one extra argument to store the client task hash.

  • 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 return ipc_connect_to_me(PHONE_NS, SERVICE_LO, 0, 0, NULL, NULL);
170}
171
172int netif_probe_message(device_id_t device_id, int irq, void *io)
173{
174 /* Create a new device */
175 netif_device_t *device;
176 int rc = lo_create(device_id, &device);
177 if (rc != EOK)
178 return rc;
179
180 printf("%s: Device created (id: %d)\n", NAME, device->device_id);
181 return EOK;
182}
183
184int netif_send_message(device_id_t device_id, packet_t *packet, services_t sender)
185{
186 netif_device_t *device;
187 int rc = find_device(device_id, &device);
188 if (rc != EOK)
189 return EOK;
190
191 if (device->state != NETIF_ACTIVE) {
192 netif_pq_release(packet_get_id(packet));
193 return EFORWARD;
194 }
195
196 packet_t *next = packet;
197 do {
198 ((device_stats_t *) device->specific)->send_packets++;
199 ((device_stats_t *) device->specific)->receive_packets++;
200 size_t length = packet_get_data_length(next);
201 ((device_stats_t *) device->specific)->send_bytes += length;
202 ((device_stats_t *) device->specific)->receive_bytes += length;
203 next = pq_next(next);
204 } while (next);
205
206 int phone = device->nil_phone;
207 fibril_rwlock_write_unlock(&netif_globals.lock);
208
209 nil_received_msg(phone, device_id, packet, sender);
210
211 fibril_rwlock_write_lock(&netif_globals.lock);
212 return EOK;
213}
214
215int netif_start_message(netif_device_t *device)
216{
217 change_state_message(device, NETIF_ACTIVE);
218 return device->state;
219}
220
221int netif_stop_message(netif_device_t *device)
222{
223 change_state_message(device, NETIF_STOPPED);
224 return device->state;
225}
226
227int main(int argc, char *argv[])
228{
229 /* Start the module */
230 return netif_module_start();
231}
232
233/** @}
234 */
Note: See TracBrowser for help on using the repository browser.