source: mainline/uspace/srv/net/ethip/ethip_nic.c@ b1213b0

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since b1213b0 was 69a93df7, checked in by Jiri Svoboda <jiri@…>, 14 years ago

Move network-related servers to srv/net.

  • Property mode set to 100644
File size: 9.2 KB
Line 
1/*
2 * Copyright (c) 2012 Jiri Svoboda
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 ethip
30 * @{
31 */
32/**
33 * @file
34 * @brief
35 */
36
37#include <adt/list.h>
38#include <async.h>
39#include <bool.h>
40#include <errno.h>
41#include <fibril_synch.h>
42#include <inet/iplink_srv.h>
43#include <io/log.h>
44#include <loc.h>
45#include <device/nic.h>
46#include <stdlib.h>
47
48#include "ethip.h"
49#include "ethip_nic.h"
50#include "pdu.h"
51
52static int ethip_nic_open(service_id_t sid);
53static void ethip_nic_cb_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg);
54
55static LIST_INITIALIZE(ethip_nic_list);
56static FIBRIL_MUTEX_INITIALIZE(ethip_discovery_lock);
57
58static int ethip_nic_check_new(void)
59{
60 bool already_known;
61 category_id_t iplink_cat;
62 service_id_t *svcs;
63 size_t count, i;
64 int rc;
65
66 fibril_mutex_lock(&ethip_discovery_lock);
67
68 rc = loc_category_get_id("nic", &iplink_cat, IPC_FLAG_BLOCKING);
69 if (rc != EOK) {
70 log_msg(LVL_ERROR, "Failed resolving category 'nic'.");
71 fibril_mutex_unlock(&ethip_discovery_lock);
72 return ENOENT;
73 }
74
75 rc = loc_category_get_svcs(iplink_cat, &svcs, &count);
76 if (rc != EOK) {
77 log_msg(LVL_ERROR, "Failed getting list of IP links.");
78 fibril_mutex_unlock(&ethip_discovery_lock);
79 return EIO;
80 }
81
82 for (i = 0; i < count; i++) {
83 already_known = false;
84
85 list_foreach(ethip_nic_list, nic_link) {
86 ethip_nic_t *nic = list_get_instance(nic_link,
87 ethip_nic_t, nic_list);
88 if (nic->svc_id == svcs[i]) {
89 already_known = true;
90 break;
91 }
92 }
93
94 if (!already_known) {
95 log_msg(LVL_DEBUG, "Found NIC '%lu'",
96 (unsigned long) svcs[i]);
97 rc = ethip_nic_open(svcs[i]);
98 if (rc != EOK)
99 log_msg(LVL_ERROR, "Could not open NIC.");
100 }
101 }
102
103 fibril_mutex_unlock(&ethip_discovery_lock);
104 return EOK;
105}
106
107static ethip_nic_t *ethip_nic_new(void)
108{
109 ethip_nic_t *nic = calloc(1, sizeof(ethip_nic_t));
110
111 if (nic == NULL) {
112 log_msg(LVL_ERROR, "Failed allocating NIC structure. "
113 "Out of memory.");
114 return NULL;
115 }
116
117 link_initialize(&nic->nic_list);
118 list_initialize(&nic->addr_list);
119
120 return nic;
121}
122
123static ethip_link_addr_t *ethip_nic_addr_new(iplink_srv_addr_t *addr)
124{
125 ethip_link_addr_t *laddr = calloc(1, sizeof(ethip_link_addr_t));
126
127 if (laddr == NULL) {
128 log_msg(LVL_ERROR, "Failed allocating NIC address structure. "
129 "Out of memory.");
130 return NULL;
131 }
132
133 link_initialize(&laddr->addr_list);
134 laddr->addr.ipv4 = addr->ipv4;
135 return laddr;
136}
137
138static void ethip_nic_delete(ethip_nic_t *nic)
139{
140 if (nic->svc_name != NULL)
141 free(nic->svc_name);
142 free(nic);
143}
144
145static void ethip_link_addr_delete(ethip_link_addr_t *laddr)
146{
147 free(laddr);
148}
149
150static int ethip_nic_open(service_id_t sid)
151{
152 ethip_nic_t *nic;
153 int rc;
154 bool in_list = false;
155 nic_address_t nic_address;
156
157 log_msg(LVL_DEBUG, "ethip_nic_open()");
158 nic = ethip_nic_new();
159 if (nic == NULL)
160 return ENOMEM;
161
162 rc = loc_service_get_name(sid, &nic->svc_name);
163 if (rc != EOK) {
164 log_msg(LVL_ERROR, "Failed getting service name.");
165 goto error;
166 }
167
168 nic->sess = loc_service_connect(EXCHANGE_SERIALIZE, sid, 0);
169 if (nic->sess == NULL) {
170 log_msg(LVL_ERROR, "Failed connecting '%s'", nic->svc_name);
171 goto error;
172 }
173
174 nic->svc_id = sid;
175
176 rc = nic_callback_create(nic->sess, ethip_nic_cb_conn, nic);
177 if (rc != EOK) {
178 log_msg(LVL_ERROR, "Failed creating callback connection "
179 "from '%s'", nic->svc_name);
180 goto error;
181 }
182
183 log_msg(LVL_DEBUG, "Opened NIC '%s'", nic->svc_name);
184 list_append(&nic->nic_list, &ethip_nic_list);
185 in_list = true;
186
187 rc = ethip_iplink_init(nic);
188 if (rc != EOK)
189 goto error;
190
191 rc = nic_get_address(nic->sess, &nic_address);
192 if (rc != EOK) {
193 log_msg(LVL_ERROR, "Error getting MAC address of NIC '%s'.",
194 nic->svc_name);
195 goto error;
196 }
197
198 mac48_decode(nic_address.address, &nic->mac_addr);
199
200 rc = nic_set_state(nic->sess, NIC_STATE_ACTIVE);
201 if (rc != EOK) {
202 log_msg(LVL_ERROR, "Error activating NIC '%s'.",
203 nic->svc_name);
204 goto error;
205 }
206
207 log_msg(LVL_DEBUG, "Initialized IP link service, MAC = 0x%" PRIx64,
208 nic->mac_addr.addr);
209
210 return EOK;
211
212error:
213 if (in_list)
214 list_remove(&nic->nic_list);
215 if (nic->sess != NULL)
216 async_hangup(nic->sess);
217 ethip_nic_delete(nic);
218 return rc;
219}
220
221static void ethip_nic_cat_change_cb(void)
222{
223 (void) ethip_nic_check_new();
224}
225
226static void ethip_nic_addr_changed(ethip_nic_t *nic, ipc_callid_t callid,
227 ipc_call_t *call)
228{
229 log_msg(LVL_DEBUG, "ethip_nic_addr_changed()");
230 async_answer_0(callid, ENOTSUP);
231}
232
233#include <stdio.h>
234static void ethip_nic_received(ethip_nic_t *nic, ipc_callid_t callid,
235 ipc_call_t *call)
236{
237 int rc;
238 void *data;
239 size_t size;
240
241 log_msg(LVL_DEBUG, "ethip_nic_received() nic=%p", nic);
242
243 rc = async_data_write_accept(&data, false, 0, 0, 0, &size);
244 if (rc != EOK) {
245 log_msg(LVL_DEBUG, "data_write_accept() failed");
246 return;
247 }
248
249 log_msg(LVL_DEBUG, "Ethernet PDU contents (%zu bytes)",
250 size);
251 size_t i;
252 for (i = 0; i < size; i++)
253 printf("%02x ", ((uint8_t *)data)[i]);
254 printf("\n");
255
256 log_msg(LVL_DEBUG, "call ethip_received");
257 rc = ethip_received(&nic->iplink, data, size);
258 log_msg(LVL_DEBUG, "free data");
259 free(data);
260
261 log_msg(LVL_DEBUG, "ethip_nic_received() done, rc=%d", rc);
262 async_answer_0(callid, rc);
263}
264
265static void ethip_nic_device_state(ethip_nic_t *nic, ipc_callid_t callid,
266 ipc_call_t *call)
267{
268 log_msg(LVL_DEBUG, "ethip_nic_device_state()");
269 async_answer_0(callid, ENOTSUP);
270}
271
272static void ethip_nic_cb_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg)
273{
274 ethip_nic_t *nic = (ethip_nic_t *)arg;
275
276 log_msg(LVL_DEBUG, "ethnip_nic_cb_conn()");
277
278 while (true) {
279 ipc_call_t call;
280 ipc_callid_t callid = async_get_call(&call);
281
282 if (!IPC_GET_IMETHOD(call)) {
283 /* TODO: Handle hangup */
284 return;
285 }
286
287 switch (IPC_GET_IMETHOD(call)) {
288 case NIC_EV_ADDR_CHANGED:
289 ethip_nic_addr_changed(nic, callid, &call);
290 break;
291 case NIC_EV_RECEIVED:
292 ethip_nic_received(nic, callid, &call);
293 break;
294 case NIC_EV_DEVICE_STATE:
295 ethip_nic_device_state(nic, callid, &call);
296 break;
297 default:
298 async_answer_0(callid, ENOTSUP);
299 }
300 }
301}
302
303int ethip_nic_discovery_start(void)
304{
305 int rc;
306
307 rc = loc_register_cat_change_cb(ethip_nic_cat_change_cb);
308 if (rc != EOK) {
309 log_msg(LVL_ERROR, "Failed registering callback for NIC "
310 "discovery (%d).", rc);
311 return rc;
312 }
313
314 return ethip_nic_check_new();
315}
316
317ethip_nic_t *ethip_nic_find_by_iplink_sid(service_id_t iplink_sid)
318{
319 log_msg(LVL_DEBUG, "ethip_nic_find_by_iplink_sid(%u)",
320 (unsigned) iplink_sid);
321
322 list_foreach(ethip_nic_list, link) {
323 log_msg(LVL_DEBUG, "ethip_nic_find_by_iplink_sid - element");
324 ethip_nic_t *nic = list_get_instance(link, ethip_nic_t,
325 nic_list);
326
327 if (nic->iplink_sid == iplink_sid) {
328 log_msg(LVL_DEBUG, "ethip_nic_find_by_iplink_sid - found %p", nic);
329 return nic;
330 }
331 }
332
333 log_msg(LVL_DEBUG, "ethip_nic_find_by_iplink_sid - not found");
334 return NULL;
335}
336
337int ethip_nic_send(ethip_nic_t *nic, void *data, size_t size)
338{
339 int rc;
340 log_msg(LVL_DEBUG, "ethip_nic_send(size=%zu)", size);
341 rc = nic_send_frame(nic->sess, data, size);
342 log_msg(LVL_DEBUG, "nic_send_frame -> %d", rc);
343 return rc;
344}
345
346int ethip_nic_addr_add(ethip_nic_t *nic, iplink_srv_addr_t *addr)
347{
348 ethip_link_addr_t *laddr;
349
350 log_msg(LVL_DEBUG, "ethip_nic_addr_add()");
351 laddr = ethip_nic_addr_new(addr);
352 if (laddr == NULL)
353 return ENOMEM;
354
355 list_append(&laddr->addr_list, &nic->addr_list);
356 return EOK;
357}
358
359int ethip_nic_addr_remove(ethip_nic_t *nic, iplink_srv_addr_t *addr)
360{
361 ethip_link_addr_t *laddr;
362
363 log_msg(LVL_DEBUG, "ethip_nic_addr_remove()");
364
365 laddr = ethip_nic_addr_find(nic, addr);
366 if (laddr == NULL)
367 return ENOENT;
368
369 list_remove(&laddr->addr_list);
370 ethip_link_addr_delete(laddr);
371 return EOK;
372}
373
374ethip_link_addr_t *ethip_nic_addr_find(ethip_nic_t *nic,
375 iplink_srv_addr_t *addr)
376{
377 log_msg(LVL_DEBUG, "ethip_nic_addr_find()");
378
379 list_foreach(nic->addr_list, link) {
380 ethip_link_addr_t *laddr = list_get_instance(link,
381 ethip_link_addr_t, addr_list);
382
383 if (addr->ipv4 == laddr->addr.ipv4)
384 return laddr;
385 }
386
387 return NULL;
388}
389
390/** @}
391 */
Note: See TracBrowser for help on using the repository browser.