source: mainline/uspace/srv/ethip/ethip_nic.c@ 4f64a523

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

Need to limit iplink to a single client connection.

  • Property mode set to 100644
File size: 7.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
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
118 return nic;
119}
120
121static void ethip_nic_delete(ethip_nic_t *nic)
122{
123 if (nic->svc_name != NULL)
124 free(nic->svc_name);
125 free(nic);
126}
127
128static int ethip_nic_open(service_id_t sid)
129{
130 ethip_nic_t *nic;
131 int rc;
132 bool in_list = false;
133
134 log_msg(LVL_DEBUG, "ethip_nic_open()");
135 nic = ethip_nic_new();
136 if (nic == NULL)
137 return ENOMEM;
138
139 rc = loc_service_get_name(sid, &nic->svc_name);
140 if (rc != EOK) {
141 log_msg(LVL_ERROR, "Failed getting service name.");
142 goto error;
143 }
144
145 nic->sess = loc_service_connect(EXCHANGE_SERIALIZE, sid, 0);
146 if (nic->sess == NULL) {
147 log_msg(LVL_ERROR, "Failed connecting '%s'", nic->svc_name);
148 goto error;
149 }
150
151 nic->svc_id = sid;
152
153 rc = nic_callback_create(nic->sess, ethip_nic_cb_conn, nic);
154 if (rc != EOK) {
155 log_msg(LVL_ERROR, "Failed creating callback connection "
156 "from '%s'", nic->svc_name);
157 goto error;
158 }
159
160 log_msg(LVL_DEBUG, "Opened NIC '%s'", nic->svc_name);
161 list_append(&nic->nic_list, &ethip_nic_list);
162 in_list = true;
163
164 rc = ethip_iplink_init(nic);
165 if (rc != EOK)
166 goto error;
167
168 rc = nic_set_state(nic->sess, NIC_STATE_ACTIVE);
169 if (rc != EOK) {
170 log_msg(LVL_ERROR, "Failed activating NIC '%s'.",
171 nic->svc_name);
172 goto error;
173 }
174
175 log_msg(LVL_DEBUG, "Initialized IP link service.");
176
177 return EOK;
178
179error:
180 if (in_list)
181 list_remove(&nic->nic_list);
182 if (nic->sess != NULL)
183 async_hangup(nic->sess);
184 ethip_nic_delete(nic);
185 return rc;
186}
187
188static void ethip_nic_cat_change_cb(void)
189{
190 (void) ethip_nic_check_new();
191}
192
193static void ethip_nic_addr_changed(ethip_nic_t *nic, ipc_callid_t callid,
194 ipc_call_t *call)
195{
196 log_msg(LVL_DEBUG, "ethip_nic_addr_changed()");
197 async_answer_0(callid, ENOTSUP);
198}
199
200static void ethip_nic_received(ethip_nic_t *nic, ipc_callid_t callid,
201 ipc_call_t *call)
202{
203 int rc;
204 void *data;
205 size_t size;
206
207 log_msg(LVL_DEBUG, "ethip_nic_received() nic=%p", nic);
208
209 rc = async_data_write_accept(&data, false, 0, 0, 0, &size);
210 if (rc != EOK) {
211 log_msg(LVL_DEBUG, "data_write_accept() failed");
212 return;
213 }
214
215 log_msg(LVL_DEBUG, "call ethip_received");
216 rc = ethip_received(&nic->iplink, data, size);
217 log_msg(LVL_DEBUG, "free data");
218 free(data);
219
220 async_answer_0(callid, rc);
221}
222
223static void ethip_nic_device_state(ethip_nic_t *nic, ipc_callid_t callid,
224 ipc_call_t *call)
225{
226 log_msg(LVL_DEBUG, "ethip_nic_device_state()");
227 async_answer_0(callid, ENOTSUP);
228}
229
230static void ethip_nic_cb_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg)
231{
232 ethip_nic_t *nic = (ethip_nic_t *)arg;
233
234 log_msg(LVL_DEBUG, "ethnip_nic_cb_conn()");
235
236 while (true) {
237 ipc_call_t call;
238 ipc_callid_t callid = async_get_call(&call);
239
240 if (!IPC_GET_IMETHOD(call)) {
241 /* TODO: Handle hangup */
242 return;
243 }
244
245 switch (IPC_GET_IMETHOD(call)) {
246 case NIC_EV_ADDR_CHANGED:
247 ethip_nic_addr_changed(nic, callid, &call);
248 break;
249 case NIC_EV_RECEIVED:
250 ethip_nic_received(nic, callid, &call);
251 break;
252 case NIC_EV_DEVICE_STATE:
253 ethip_nic_device_state(nic, callid, &call);
254 break;
255 default:
256 async_answer_0(callid, ENOTSUP);
257 }
258 }
259}
260
261int ethip_nic_discovery_start(void)
262{
263 int rc;
264
265 rc = loc_register_cat_change_cb(ethip_nic_cat_change_cb);
266 if (rc != EOK) {
267 log_msg(LVL_ERROR, "Failed registering callback for NIC "
268 "discovery (%d).", rc);
269 return rc;
270 }
271
272 return ethip_nic_check_new();
273}
274
275ethip_nic_t *ethip_nic_find_by_iplink_sid(service_id_t iplink_sid)
276{
277 log_msg(LVL_DEBUG, "ethip_nic_find_by_iplink_sid(%u)",
278 (unsigned) iplink_sid);
279
280 list_foreach(ethip_nic_list, link) {
281 log_msg(LVL_DEBUG, "ethip_nic_find_by_iplink_sid - element");
282 ethip_nic_t *nic = list_get_instance(link, ethip_nic_t,
283 nic_list);
284
285 if (nic->iplink_sid == iplink_sid) {
286 log_msg(LVL_DEBUG, "ethip_nic_find_by_iplink_sid - found %p", nic);
287 return nic;
288 }
289 }
290
291 log_msg(LVL_DEBUG, "ethip_nic_find_by_iplink_sid - not found");
292 return NULL;
293}
294
295int ethip_nic_send(ethip_nic_t *nic, void *data, size_t size)
296{
297 return nic_send_frame(nic->sess, data, size);
298}
299
300/** @}
301 */
Note: See TracBrowser for help on using the repository browser.