Changes in uspace/srv/net/nil/nildummy/nildummy.c [49bd793b:ffa2c8ef] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/srv/net/nil/nildummy/nildummy.c
r49bd793b rffa2c8ef 1 1 /* 2 2 * Copyright (c) 2009 Lukas Mejdrech 3 * Copyright (c) 2011 Radim Vansa4 3 * All rights reserved. 5 4 * … … 37 36 */ 38 37 39 #include <assert.h>40 38 #include <async.h> 41 39 #include <malloc.h> … … 46 44 #include <ipc/net.h> 47 45 #include <ipc/services.h> 46 48 47 #include <net/modules.h> 49 48 #include <net/device.h> … … 52 51 #include <net/packet.h> 53 52 #include <packet_remote.h> 54 #include <packet_client.h> 55 #include <devman.h> 56 #include <device/nic.h> 53 #include <netif_remote.h> 57 54 #include <nil_skel.h> 55 58 56 #include "nildummy.h" 59 57 … … 69 67 DEVICE_MAP_IMPLEMENT(nildummy_devices, nildummy_device_t); 70 68 71 int nil_device_state_msg_local( nic_device_id_t device_id, sysarg_t state)69 int nil_device_state_msg_local(int nil_phone, device_id_t device_id, int state) 72 70 { 73 71 fibril_rwlock_read_lock(&nildummy_globals.protos_lock); 74 if (nildummy_globals.proto. sess)75 il_device_state_msg(nildummy_globals.proto. sess, device_id,72 if (nildummy_globals.proto.phone) 73 il_device_state_msg(nildummy_globals.proto.phone, device_id, 76 74 state, nildummy_globals.proto.service); 77 75 fibril_rwlock_read_unlock(&nildummy_globals.protos_lock); … … 80 78 } 81 79 82 int nil_initialize( async_sess_t *sess)80 int nil_initialize(int net_phone) 83 81 { 84 82 fibril_rwlock_initialize(&nildummy_globals.devices_lock); … … 87 85 fibril_rwlock_write_lock(&nildummy_globals.protos_lock); 88 86 89 nildummy_globals.net_ sess = sess;90 nildummy_globals.proto. sess = NULL;87 nildummy_globals.net_phone = net_phone; 88 nildummy_globals.proto.phone = 0; 91 89 int rc = nildummy_devices_initialize(&nildummy_globals.devices); 92 90 … … 95 93 96 94 return rc; 95 } 96 97 /** Process IPC messages from the registered device driver modules 98 * 99 * @param[in] iid Message identifier. 100 * @param[in,out] icall Message parameters. 101 * 102 */ 103 static void nildummy_receiver(ipc_callid_t iid, ipc_call_t *icall) 104 { 105 packet_t *packet; 106 int rc; 107 108 while (true) { 109 switch (IPC_GET_IMETHOD(*icall)) { 110 case NET_NIL_DEVICE_STATE: 111 rc = nil_device_state_msg_local(0, 112 IPC_GET_DEVICE(*icall), IPC_GET_STATE(*icall)); 113 async_answer_0(iid, (sysarg_t) rc); 114 break; 115 116 case NET_NIL_RECEIVED: 117 rc = packet_translate_remote(nildummy_globals.net_phone, 118 &packet, IPC_GET_PACKET(*icall)); 119 if (rc == EOK) 120 rc = nil_received_msg_local(0, 121 IPC_GET_DEVICE(*icall), packet, 0); 122 123 async_answer_0(iid, (sysarg_t) rc); 124 break; 125 126 default: 127 async_answer_0(iid, (sysarg_t) ENOTSUP); 128 } 129 130 iid = async_get_call(icall); 131 } 97 132 } 98 133 … … 114 149 * 115 150 */ 116 static int nildummy_device_message( nic_device_id_t device_id,117 devman_handle_t handle,size_t mtu)151 static int nildummy_device_message(device_id_t device_id, services_t service, 152 size_t mtu) 118 153 { 119 154 fibril_rwlock_write_lock(&nildummy_globals.devices_lock); … … 123 158 nildummy_devices_find(&nildummy_globals.devices, device_id); 124 159 if (device) { 125 if (device-> handle != handle) {126 printf("Device %d exists, handles do not match\n",127 device->device_id);128 fibril_rwlock_write_unlock(&nildummy_globals.devices_lock);160 if (device->service != service) { 161 printf("Device %d already exists\n", device->device_id); 162 fibril_rwlock_write_unlock( 163 &nildummy_globals.devices_lock); 129 164 return EEXIST; 130 165 } … … 136 171 device->mtu = NET_DEFAULT_MTU; 137 172 138 printf("Device %d already exists (mtu: %zu)\n", device->device_id,139 device-> mtu);173 printf("Device %d already exists:\tMTU\t= %zu\n", 174 device->device_id, device->mtu); 140 175 fibril_rwlock_write_unlock(&nildummy_globals.devices_lock); 141 176 142 177 /* Notify the upper layer module */ 143 178 fibril_rwlock_read_lock(&nildummy_globals.protos_lock); 144 if (nildummy_globals.proto. sess) {145 il_mtu_changed_msg(nildummy_globals.proto. sess,179 if (nildummy_globals.proto.phone) { 180 il_mtu_changed_msg(nildummy_globals.proto.phone, 146 181 device->device_id, device->mtu, 147 182 nildummy_globals.proto.service); … … 158 193 159 194 device->device_id = device_id; 160 device-> handle = handle;195 device->service = service; 161 196 if (mtu > 0) 162 197 device->mtu = mtu; 163 198 else 164 199 device->mtu = NET_DEFAULT_MTU; 165 200 166 201 /* Bind the device driver */ 167 device-> sess = devman_device_connect(EXCHANGE_SERIALIZE, handle,168 IPC_FLAG_BLOCKING);169 if (device-> sess == NULL) {202 device->phone = netif_bind_service(device->service, device->device_id, 203 SERVICE_ETHERNET, nildummy_receiver); 204 if (device->phone < 0) { 170 205 fibril_rwlock_write_unlock(&nildummy_globals.devices_lock); 171 206 free(device); 172 return ENOENT; 173 } 174 175 nic_connect_to_nil(device->sess, SERVICE_NILDUMMY, device_id); 207 return device->phone; 208 } 176 209 177 210 /* Get hardware address */ 178 int rc = nic_get_address(device->sess, &device->addr); 211 int rc = netif_get_addr_req(device->phone, device->device_id, 212 &device->addr, &device->addr_data); 179 213 if (rc != EOK) { 180 214 fibril_rwlock_write_unlock(&nildummy_globals.devices_lock); … … 182 216 return rc; 183 217 } 184 185 device->addr_len = ETH_ADDR;186 218 187 219 /* Add to the cache */ … … 190 222 if (index < 0) { 191 223 fibril_rwlock_write_unlock(&nildummy_globals.devices_lock); 224 free(device->addr); 225 free(device->addr_data); 192 226 free(device); 193 227 return index; 194 228 } 195 229 196 printf("%s: Device registered (id: %d, mtu: %zu)\n", NAME,197 device->device_id, device->mtu);230 printf("%s: Device registered (id: %d, service: %d, mtu: %zu)\n", 231 NAME, device->device_id, device->service, device->mtu); 198 232 fibril_rwlock_write_unlock(&nildummy_globals.devices_lock); 199 233 return EOK; … … 210 244 * 211 245 */ 212 static int nildummy_addr_message(nic_device_id_t device_id, size_t *addrlen) 213 { 214 if (!addrlen) 246 static int nildummy_addr_message(device_id_t device_id, 247 measured_string_t **address) 248 { 249 if (!address) 215 250 return EBADMEM; 216 251 … … 224 259 } 225 260 226 ipc_callid_t callid; 227 size_t max_len; 228 if (!async_data_read_receive(&callid, &max_len)) { 229 fibril_rwlock_read_unlock(&nildummy_globals.devices_lock); 230 return EREFUSED; 231 } 232 233 if (max_len < device->addr_len) { 234 fibril_rwlock_read_unlock(&nildummy_globals.devices_lock); 235 async_data_read_finalize(callid, NULL, 0); 236 return ELIMIT; 237 } 238 239 int rc = async_data_read_finalize(callid, 240 (uint8_t *) &device->addr.address, device->addr_len); 241 if (rc != EOK) { 242 fibril_rwlock_read_unlock(&nildummy_globals.devices_lock); 243 return rc; 244 } 245 246 *addrlen = device->addr_len; 261 *address = device->addr; 247 262 248 263 fibril_rwlock_read_unlock(&nildummy_globals.devices_lock); 249 return EOK; 264 265 return (*address) ? EOK : ENOENT; 250 266 } 251 267 … … 263 279 * 264 280 */ 265 static int nildummy_packet_space_message( nic_device_id_t device_id,266 size_t * addr_len, size_t *prefix, size_t *content, size_t *suffix)281 static int nildummy_packet_space_message(device_id_t device_id, size_t *addr_len, 282 size_t *prefix, size_t *content, size_t *suffix) 267 283 { 268 284 if ((!addr_len) || (!prefix) || (!content) || (!suffix)) … … 288 304 } 289 305 290 int nil_received_msg_local(nic_device_id_t device_id, packet_t *packet) 306 int nil_received_msg_local(int nil_phone, device_id_t device_id, 307 packet_t *packet, services_t target) 291 308 { 292 309 fibril_rwlock_read_lock(&nildummy_globals.protos_lock); 293 310 294 if (nildummy_globals.proto. sess) {311 if (nildummy_globals.proto.phone) { 295 312 do { 296 313 packet_t *next = pq_detach(packet); 297 il_received_msg(nildummy_globals.proto. sess, device_id,314 il_received_msg(nildummy_globals.proto.phone, device_id, 298 315 packet, nildummy_globals.proto.service); 299 316 packet = next; … … 311 328 * 312 329 * @param[in] service Module service. 313 * @param[in] sess Service session.330 * @param[in] phone Service phone. 314 331 * 315 332 * @return EOK on success. … … 318 335 * 319 336 */ 320 static int nildummy_register_message(services_t service, async_sess_t *sess)337 static int nildummy_register_message(services_t service, int phone) 321 338 { 322 339 fibril_rwlock_write_lock(&nildummy_globals.protos_lock); 323 340 nildummy_globals.proto.service = service; 324 nildummy_globals.proto. sess = sess;325 326 printf("%s: Protocol registered (service: % #x)\n",327 NAME, nildummy_globals.proto.service );341 nildummy_globals.proto.phone = phone; 342 343 printf("%s: Protocol registered (service: %d, phone: %d)\n", 344 NAME, nildummy_globals.proto.service, nildummy_globals.proto.phone); 328 345 329 346 fibril_rwlock_write_unlock(&nildummy_globals.protos_lock); … … 342 359 * 343 360 */ 344 static int nildummy_send_message( nic_device_id_t device_id, packet_t *packet,361 static int nildummy_send_message(device_id_t device_id, packet_t *packet, 345 362 services_t sender) 346 363 { … … 356 373 /* Send packet queue */ 357 374 if (packet) 358 nic_send_message(device->sess, packet_get_id(packet)); 375 netif_send_msg(device->phone, device_id, packet, 376 SERVICE_NILDUMMY); 359 377 360 378 fibril_rwlock_read_unlock(&nildummy_globals.devices_lock); … … 366 384 ipc_call_t *answer, size_t *answer_count) 367 385 { 386 measured_string_t *address; 368 387 packet_t *packet; 369 388 size_t addrlen; … … 374 393 375 394 *answer_count = 0; 376 377 if (!IPC_GET_IMETHOD(*call))395 switch (IPC_GET_IMETHOD(*call)) { 396 case IPC_M_PHONE_HUNGUP: 378 397 return EOK; 379 398 380 async_sess_t *callback =381 async_callback_receive_start(EXCHANGE_SERIALIZE, call);382 if (callback)383 return nildummy_register_message(NIL_GET_PROTO(*call), callback);384 385 switch (IPC_GET_IMETHOD(*call)) {386 399 case NET_NIL_DEVICE: 387 400 return nildummy_device_message(IPC_GET_DEVICE(*call), 388 IPC_GET_ DEVICE_HANDLE(*call), IPC_GET_MTU(*call));401 IPC_GET_SERVICE(*call), IPC_GET_MTU(*call)); 389 402 390 403 case NET_NIL_SEND: 391 rc = packet_translate_remote(nildummy_globals.net_ sess,404 rc = packet_translate_remote(nildummy_globals.net_phone, 392 405 &packet, IPC_GET_PACKET(*call)); 393 406 if (rc != EOK) … … 409 422 410 423 case NET_NIL_ADDR: 411 case NET_NIL_BROADCAST_ADDR: 412 rc = nildummy_addr_message(IPC_GET_DEVICE(*call), &addrlen); 424 rc = nildummy_addr_message(IPC_GET_DEVICE(*call), &address); 413 425 if (rc != EOK) 414 426 return rc; 415 416 IPC_SET_ADDR(*answer, addrlen); 417 *answer_count = 1; 418 return rc; 419 case NET_NIL_DEVICE_STATE: 420 rc = nil_device_state_msg_local(IPC_GET_DEVICE(*call), 421 IPC_GET_STATE(*call)); 422 async_answer_0(callid, (sysarg_t) rc); 423 return rc; 424 425 case NET_NIL_RECEIVED: 426 rc = packet_translate_remote(nildummy_globals.net_sess, &packet, 427 IPC_GET_ARG2(*call)); 428 if (rc == EOK) 429 rc = nil_received_msg_local(IPC_GET_ARG1(*call), packet); 430 431 async_answer_0(callid, (sysarg_t) rc); 432 return rc; 427 return measured_strings_reply(address, 1); 428 429 case NET_NIL_BROADCAST_ADDR: 430 rc = nildummy_addr_message(IPC_GET_DEVICE(*call), &address); 431 if (rc != EOK) 432 return rc; 433 return measured_strings_reply(address, 1); 434 435 case IPC_M_CONNECT_TO_ME: 436 return nildummy_register_message(NIL_GET_PROTO(*call), 437 IPC_GET_PHONE(*call)); 433 438 } 434 439
Note:
See TracChangeset
for help on using the changeset viewer.