source: mainline/uspace/srv/ethip/ethip_nic.c@ 962f03b

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

IP links need to be made aware of configured IP addresses.

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