Changes in uspace/srv/net/nil/nildummy/nildummy.c [ffa2c8ef:3cd95ef] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/srv/net/nil/nildummy/nildummy.c
rffa2c8ef r3cd95ef 41 41 #include <stdio.h> 42 42 #include <str.h> 43 #include <ipc/nil.h> 43 #include <err.h> 44 #include <ipc/ipc.h> 44 45 #include <ipc/net.h> 45 46 #include <ipc/services.h> … … 47 48 #include <net/modules.h> 48 49 #include <net/device.h> 49 #include <il_remote.h> 50 #include <netif_interface.h> 51 #include <nil_interface.h> 52 #include <il_interface.h> 50 53 #include <adt/measured_strings.h> 51 54 #include <net/packet.h> 52 55 #include <packet_remote.h> 53 #include <netif_remote.h> 54 #include <nil_skel.h> 56 #include <nil_local.h> 55 57 56 58 #include "nildummy.h" … … 80 82 int nil_initialize(int net_phone) 81 83 { 84 ERROR_DECLARE; 85 82 86 fibril_rwlock_initialize(&nildummy_globals.devices_lock); 83 87 fibril_rwlock_initialize(&nildummy_globals.protos_lock); … … 87 91 nildummy_globals.net_phone = net_phone; 88 92 nildummy_globals.proto.phone = 0; 89 int rc= nildummy_devices_initialize(&nildummy_globals.devices);93 ERROR_CODE = nildummy_devices_initialize(&nildummy_globals.devices); 90 94 91 95 fibril_rwlock_write_unlock(&nildummy_globals.protos_lock); 92 96 fibril_rwlock_write_unlock(&nildummy_globals.devices_lock); 93 97 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 * 98 return ERROR_CODE; 99 } 100 101 /** Process IPC messages from the registered device driver modules in an 102 * infinite loop. 103 * 104 * @param[in] iid The message identifier. 105 * @param[in,out] icall The message parameters. 102 106 */ 103 107 static void nildummy_receiver(ipc_callid_t iid, ipc_call_t *icall) 104 108 { 105 packet_t *packet; 106 int rc; 107 109 ERROR_DECLARE; 110 111 packet_t packet; 112 108 113 while (true) { 109 switch (IPC_GET_ IMETHOD(*icall)) {114 switch (IPC_GET_METHOD(*icall)) { 110 115 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);116 ERROR_CODE = nil_device_state_msg_local(0, 117 IPC_GET_DEVICE(icall), IPC_GET_STATE(icall)); 118 ipc_answer_0(iid, (ipcarg_t) ERROR_CODE); 114 119 break; 115 120 116 121 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);122 if (ERROR_NONE(packet_translate_remote( 123 nildummy_globals.net_phone, &packet, 124 IPC_GET_PACKET(icall)))) { 125 ERROR_CODE = nil_received_msg_local(0, 126 IPC_GET_DEVICE(icall), packet, 0); 127 } 128 ipc_answer_0(iid, (ipcarg_t) ERROR_CODE); 124 129 break; 125 130 126 131 default: 127 async_answer_0(iid, (sysarg_t) ENOTSUP);132 ipc_answer_0(iid, (ipcarg_t) ENOTSUP); 128 133 } 129 134 … … 136 141 * Determine the device local hardware address. 137 142 * 138 * @param[in] device_id New device identifier. 139 * @param[in] service Device driver service. 140 * @param[in] mtu Device maximum transmission unit. 141 * 142 * @return EOK on success. 143 * @return EEXIST if the device with the different service exists. 144 * @return ENOMEM if there is not enough memory left. 145 * @return Other error codes as defined for the 146 * netif_bind_service() function. 147 * @return Other error codes as defined for the 148 * netif_get_addr_req() function. 149 * 150 */ 151 static int nildummy_device_message(device_id_t device_id, services_t service, 152 size_t mtu) 153 { 143 * @param[in] device_id The new device identifier. 144 * @param[in] service The device driver service. 145 * @param[in] mtu The device maximum transmission unit. 146 * @returns EOK on success. 147 * @returns EEXIST if the device with the different service exists. 148 * @returns ENOMEM if there is not enough memory left. 149 * @returns Other error codes as defined for the 150 * netif_bind_service() function. 151 * @returns Other error codes as defined for the 152 * netif_get_addr_req() function. 153 */ 154 static int 155 nildummy_device_message(device_id_t device_id, services_t service, size_t mtu) 156 { 157 ERROR_DECLARE; 158 159 nildummy_device_ref device; 160 int index; 161 154 162 fibril_rwlock_write_lock(&nildummy_globals.devices_lock); 155 156 /* An existing device? */ 157 nildummy_device_t *device = 158 nildummy_devices_find(&nildummy_globals.devices, device_id); 163 164 // an existing device? 165 device = nildummy_devices_find(&nildummy_globals.devices, device_id); 159 166 if (device) { 160 167 if (device->service != service) { … … 165 172 } 166 173 167 / * Update MTU */174 // update mtu 168 175 if (mtu > 0) 169 176 device->mtu = mtu; … … 171 178 device->mtu = NET_DEFAULT_MTU; 172 179 173 printf("Device %d already exists:\tMTU\t= % zu\n",180 printf("Device %d already exists:\tMTU\t= %d\n", 174 181 device->device_id, device->mtu); 175 182 fibril_rwlock_write_unlock(&nildummy_globals.devices_lock); 176 183 177 / * Notify the upper layer module */184 // notify the upper layer module 178 185 fibril_rwlock_read_lock(&nildummy_globals.protos_lock); 179 186 if (nildummy_globals.proto.phone) { … … 187 194 } 188 195 189 / * Create a new device */190 device = (nildummy_device_ t *) malloc(sizeof(nildummy_device_t));196 // create a new device 197 device = (nildummy_device_ref) malloc(sizeof(nildummy_device_t)); 191 198 if (!device) 192 199 return ENOMEM; … … 199 206 device->mtu = NET_DEFAULT_MTU; 200 207 201 / * Bind the device driver */208 // bind the device driver 202 209 device->phone = netif_bind_service(device->service, device->device_id, 203 210 SERVICE_ETHERNET, nildummy_receiver); … … 208 215 } 209 216 210 /* Get hardware address */ 211 int rc = netif_get_addr_req(device->phone, device->device_id, 212 &device->addr, &device->addr_data); 213 if (rc != EOK) { 217 // get hardware address 218 if (ERROR_OCCURRED(netif_get_addr_req(device->phone, device->device_id, 219 &device->addr, &device->addr_data))) { 214 220 fibril_rwlock_write_unlock(&nildummy_globals.devices_lock); 215 221 free(device); 216 return rc;217 } 218 219 / * Add to the cache */220 in t index = nildummy_devices_add(&nildummy_globals.devices,222 return ERROR_CODE; 223 } 224 225 // add to the cache 226 index = nildummy_devices_add(&nildummy_globals.devices, 221 227 device->device_id, device); 222 228 if (index < 0) { … … 228 234 } 229 235 230 printf("%s: Device registered (id: %d, service: %d, mtu: % zu)\n",236 printf("%s: Device registered (id: %d, service: %d, mtu: %d)\n", 231 237 NAME, device->device_id, device->service, device->mtu); 232 238 fibril_rwlock_write_unlock(&nildummy_globals.devices_lock); … … 236 242 /** Return the device hardware address. 237 243 * 238 * @param[in] device_id Device identifier. 239 * @param[out] address Device hardware address. 240 * 241 * @return EOK on success. 242 * @return EBADMEM if the address parameter is NULL. 243 * @return ENOENT if there no such device. 244 * 245 */ 246 static int nildummy_addr_message(device_id_t device_id, 247 measured_string_t **address) 248 { 244 * @param[in] device_id The device identifier. 245 * @param[out] address The device hardware address. 246 * @return EOK on success. 247 * @return EBADMEM if the address parameter is NULL. 248 * @return ENOENT if there no such device. 249 * 250 */ 251 static int 252 nildummy_addr_message(device_id_t device_id, measured_string_ref *address) 253 { 254 nildummy_device_ref device; 255 249 256 if (!address) 250 257 return EBADMEM; 251 258 252 259 fibril_rwlock_read_lock(&nildummy_globals.devices_lock); 253 254 nildummy_device_t *device = 255 nildummy_devices_find(&nildummy_globals.devices, device_id); 260 device = nildummy_devices_find(&nildummy_globals.devices, device_id); 256 261 if (!device) { 257 262 fibril_rwlock_read_unlock(&nildummy_globals.devices_lock); 258 263 return ENOENT; 259 264 } 260 261 265 *address = device->addr; 262 263 266 fibril_rwlock_read_unlock(&nildummy_globals.devices_lock); 264 267 … … 268 271 /** Return the device packet dimensions for sending. 269 272 * 270 * @param[in] device_id Device identifier.271 * @param[out] addr_len Minimum reserved address length.272 * @param[out] prefix Minimum reserved prefix size.273 * @param[out] content Maximum content size.274 * @param[out] suffix Minimum reserved suffix size.275 * 276 * @return EOK on success.277 * @return EBADMEM if either one of the parameters is NULL.278 * @return ENOENT if there is no such device.279 * 280 */ 281 static intnildummy_packet_space_message(device_id_t device_id, size_t *addr_len,273 * @param[in] device_id The device identifier. 274 * @param[out] addr_len The minimum reserved address length. 275 * @param[out] prefix The minimum reserved prefix size. 276 * @param[out] content The maximum content size. 277 * @param[out] suffix The minimum reserved suffix size. 278 * @return EOK on success. 279 * @return EBADMEM if either one of the parameters is NULL. 280 * @return ENOENT if there is no such device. 281 * 282 */ 283 static int 284 nildummy_packet_space_message(device_id_t device_id, size_t *addr_len, 282 285 size_t *prefix, size_t *content, size_t *suffix) 283 286 { 284 if ((!addr_len) || (!prefix) || (!content) || (!suffix)) 287 nildummy_device_ref device; 288 289 if (!addr_len || !prefix || !content || !suffix) 285 290 return EBADMEM; 286 291 287 292 fibril_rwlock_read_lock(&nildummy_globals.devices_lock); 288 289 nildummy_device_t *device = 290 nildummy_devices_find(&nildummy_globals.devices, device_id); 293 device = nildummy_devices_find(&nildummy_globals.devices, device_id); 291 294 if (!device) { 292 295 fibril_rwlock_read_unlock(&nildummy_globals.devices_lock); 293 296 return ENOENT; 294 297 } 295 296 298 *content = device->mtu; 297 298 299 fibril_rwlock_read_unlock(&nildummy_globals.devices_lock); 299 300 … … 304 305 } 305 306 306 int nil_received_msg_local(int nil_phone, device_id_t device_id, 307 packet_t *packet, services_t target) 308 { 307 int 308 nil_received_msg_local(int nil_phone, device_id_t device_id, packet_t packet, 309 services_t target) 310 { 311 packet_t next; 312 309 313 fibril_rwlock_read_lock(&nildummy_globals.protos_lock); 310 311 314 if (nildummy_globals.proto.phone) { 312 315 do { 313 packet_t *next = pq_detach(packet);316 next = pq_detach(packet); 314 317 il_received_msg(nildummy_globals.proto.phone, device_id, 315 318 packet, nildummy_globals.proto.service); 316 319 packet = next; 317 } while (packet); 318 } 319 320 } while(packet); 321 } 320 322 fibril_rwlock_read_unlock(&nildummy_globals.protos_lock); 321 323 … … 327 329 * Pass received packets for this service. 328 330 * 329 * @param[in] service Module service. 330 * @param[in] phone Service phone. 331 * 332 * @return EOK on success. 333 * @return ENOENT if the service is not known. 334 * @return ENOMEM if there is not enough memory left. 335 * 331 * @param[in] service The module service. 332 * @param[in] phone The service phone. 333 * @return EOK on success. 334 * @return ENOENT if the service is not known. 335 * @return ENOMEM if there is not enough memory left. 336 336 */ 337 337 static int nildummy_register_message(services_t service, int phone) … … 350 350 /** Send the packet queue. 351 351 * 352 * @param[in] device_id Device identifier.353 * @param[in] packet Packet queue.354 * @param[in] sender Sending module service.355 * 356 * @return EOK on success.357 * @return ENOENT if there no such device.358 * @return EINVAL if the service parameter is not known.359 * 360 */ 361 static int nildummy_send_message(device_id_t device_id, packet_t *packet, 362 services_t sender) 363 { 352 * @param[in] device_id The device identifier. 353 * @param[in] packet The packet queue. 354 * @param[in] sender The sending module service. 355 * @return EOK on success. 356 * @return ENOENT if there no such device. 357 * @return EINVAL if the service parameter is not known. 358 */ 359 static int 360 nildummy_send_message(device_id_t device_id, packet_t packet, services_t sender) 361 { 362 nildummy_device_ref device; 363 364 364 fibril_rwlock_read_lock(&nildummy_globals.devices_lock); 365 366 nildummy_device_t *device = 367 nildummy_devices_find(&nildummy_globals.devices, device_id); 365 device = nildummy_devices_find(&nildummy_globals.devices, device_id); 368 366 if (!device) { 369 367 fibril_rwlock_read_unlock(&nildummy_globals.devices_lock); 370 368 return ENOENT; 371 369 } 372 373 /* Send packet queue */ 370 // send packet queue 374 371 if (packet) 375 372 netif_send_msg(device->phone, device_id, packet, 376 373 SERVICE_NILDUMMY); 377 378 374 fibril_rwlock_read_unlock(&nildummy_globals.devices_lock); 379 380 return EOK; 381 } 382 383 int nil_module_message(ipc_callid_t callid, ipc_call_t *call, 384 ipc_call_t *answer, size_t *answer_count) 385 { 386 measured_string_t *address; 387 packet_t *packet; 375 return EOK; 376 } 377 378 int 379 nil_message_standalone(const char *name, ipc_callid_t callid, ipc_call_t *call, 380 ipc_call_t *answer, int *answer_count) 381 { 382 ERROR_DECLARE; 383 384 measured_string_ref address; 385 packet_t packet; 388 386 size_t addrlen; 389 387 size_t prefix; 390 388 size_t suffix; 391 389 size_t content; 392 int rc;393 390 394 391 *answer_count = 0; 395 switch (IPC_GET_ IMETHOD(*call)) {392 switch (IPC_GET_METHOD(*call)) { 396 393 case IPC_M_PHONE_HUNGUP: 397 394 return EOK; 398 395 399 396 case NET_NIL_DEVICE: 400 return nildummy_device_message(IPC_GET_DEVICE( *call),401 IPC_GET_SERVICE( *call), IPC_GET_MTU(*call));397 return nildummy_device_message(IPC_GET_DEVICE(call), 398 IPC_GET_SERVICE(call), IPC_GET_MTU(call)); 402 399 403 400 case NET_NIL_SEND: 404 rc = packet_translate_remote(nildummy_globals.net_phone, 405 &packet, IPC_GET_PACKET(*call)); 406 if (rc != EOK) 407 return rc; 408 return nildummy_send_message(IPC_GET_DEVICE(*call), packet, 409 IPC_GET_SERVICE(*call)); 401 ERROR_PROPAGATE(packet_translate_remote( 402 nildummy_globals.net_phone, &packet, IPC_GET_PACKET(call))); 403 return nildummy_send_message(IPC_GET_DEVICE(call), packet, 404 IPC_GET_SERVICE(call)); 410 405 411 406 case NET_NIL_PACKET_SPACE: 412 rc = nildummy_packet_space_message(IPC_GET_DEVICE(*call), 413 &addrlen, &prefix, &content, &suffix); 414 if (rc != EOK) 415 return rc; 416 IPC_SET_ADDR(*answer, addrlen); 417 IPC_SET_PREFIX(*answer, prefix); 418 IPC_SET_CONTENT(*answer, content); 419 IPC_SET_SUFFIX(*answer, suffix); 407 ERROR_PROPAGATE(nildummy_packet_space_message( 408 IPC_GET_DEVICE(call), &addrlen, &prefix, &content, 409 &suffix)); 410 IPC_SET_ADDR(answer, addrlen); 411 IPC_SET_PREFIX(answer, prefix); 412 IPC_SET_CONTENT(answer, content); 413 IPC_SET_SUFFIX(answer, suffix); 420 414 *answer_count = 4; 421 415 return EOK; 422 416 423 417 case NET_NIL_ADDR: 424 rc = nildummy_addr_message(IPC_GET_DEVICE(*call), &address); 425 if (rc != EOK) 426 return rc; 418 ERROR_PROPAGATE(nildummy_addr_message(IPC_GET_DEVICE(call), 419 &address)); 427 420 return measured_strings_reply(address, 1); 428 421 429 422 case NET_NIL_BROADCAST_ADDR: 430 rc = nildummy_addr_message(IPC_GET_DEVICE(*call), &address); 431 if (rc != EOK) 432 return rc; 423 ERROR_PROPAGATE(nildummy_addr_message(IPC_GET_DEVICE(call), 424 &address)); 433 425 return measured_strings_reply(address, 1); 434 426 435 427 case IPC_M_CONNECT_TO_ME: 436 return nildummy_register_message(NIL_GET_PROTO( *call),437 IPC_GET_PHONE( *call));428 return nildummy_register_message(NIL_GET_PROTO(call), 429 IPC_GET_PHONE(call)); 438 430 } 439 431 … … 441 433 } 442 434 435 /** Default thread for new connections. 436 * 437 * @param[in] iid The initial message identifier. 438 * @param[in] icall The initial message call structure. 439 */ 440 static void nil_client_connection(ipc_callid_t iid, ipc_call_t *icall) 441 { 442 /* 443 * Accept the connection 444 * - Answer the first IPC_M_CONNECT_ME_TO call. 445 */ 446 ipc_answer_0(iid, EOK); 447 448 while (true) { 449 ipc_call_t answer; 450 int answer_count; 451 452 /* Clear the answer structure */ 453 refresh_answer(&answer, &answer_count); 454 455 /* Fetch the next message */ 456 ipc_call_t call; 457 ipc_callid_t callid = async_get_call(&call); 458 459 /* Process the message */ 460 int res = nil_module_message_standalone(NAME, callid, &call, 461 &answer, &answer_count); 462 463 /* 464 * End if told to either by the message or the processing 465 * result. 466 */ 467 if ((IPC_GET_METHOD(call) == IPC_M_PHONE_HUNGUP) || 468 (res == EHANGUP)) 469 return; 470 471 /* Answer the message */ 472 answer_call(callid, res, &answer, answer_count); 473 } 474 } 475 443 476 int main(int argc, char *argv[]) 444 477 { 478 ERROR_DECLARE; 479 445 480 /* Start the module */ 446 return nil_module_start(SERVICE_NILDUMMY); 481 ERROR_PROPAGATE(nil_module_start_standalone(nil_client_connection)); 482 return EOK; 447 483 } 448 484
Note:
See TracChangeset
for help on using the changeset viewer.