Changeset fc3dba14 in mainline
- Timestamp:
- 2010-12-04T20:53:01Z (14 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 8e58f94
- Parents:
- 442ebbe
- Location:
- uspace/srv/net/il/arp
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/srv/net/il/arp/arp.c
r442ebbe rfc3dba14 72 72 #define NAME "arp" 73 73 74 /** Number of microseconds to wait for an ARP reply. */ 75 #define ARP_TRANS_WAIT 1000000 76 74 77 /** ARP global data. */ 75 78 arp_globals_t arp_globals; … … 77 80 DEVICE_MAP_IMPLEMENT(arp_cache, arp_device_t); 78 81 INT_MAP_IMPLEMENT(arp_protos, arp_proto_t); 79 GENERIC_CHAR_MAP_IMPLEMENT(arp_addr, measured_string_t); 82 GENERIC_CHAR_MAP_IMPLEMENT(arp_addr, arp_trans_t); 83 84 static void arp_clear_trans(arp_trans_t *trans) 85 { 86 if (trans->hw_addr) { 87 free(trans->hw_addr); 88 trans->hw_addr = NULL; 89 } 90 fibril_condvar_broadcast(&trans->cv); 91 } 92 93 static void arp_clear_addr(arp_addr_t *addresses) 94 { 95 int count; 96 arp_trans_t *trans; 97 98 for (count = arp_addr_count(addresses) - 1; count >= 0; count--) { 99 trans = arp_addr_items_get_index(&addresses->values, count); 100 if (trans) 101 arp_clear_trans(trans); 102 } 103 } 104 80 105 81 106 /** Clears the device specific data. … … 96 121 if (proto->addr_data) 97 122 free(proto->addr_data); 123 arp_clear_addr(&proto->addresses); 98 124 arp_addr_destroy(&proto->addresses); 99 125 } … … 107 133 arp_device_t *device; 108 134 109 fibril_ rwlock_write_lock(&arp_globals.lock);135 fibril_mutex_lock(&arp_globals.lock); 110 136 for (count = arp_cache_count(&arp_globals.cache) - 1; count >= 0; 111 137 count--) { … … 120 146 } 121 147 arp_cache_clear(&arp_globals.cache); 122 fibril_ rwlock_write_unlock(&arp_globals.lock);148 fibril_mutex_unlock(&arp_globals.lock); 123 149 printf("Cache cleaned\n"); 124 150 return EOK; … … 130 156 arp_device_t *device; 131 157 arp_proto_t *proto; 132 133 fibril_rwlock_write_lock(&arp_globals.lock); 158 arp_trans_t *trans; 159 160 fibril_mutex_lock(&arp_globals.lock); 134 161 device = arp_cache_find(&arp_globals.cache, device_id); 135 162 if (!device) { 136 fibril_ rwlock_write_unlock(&arp_globals.lock);163 fibril_mutex_unlock(&arp_globals.lock); 137 164 return ENOENT; 138 165 } 139 166 proto = arp_protos_find(&device->protos, protocol); 140 167 if (!proto) { 141 fibril_ rwlock_write_unlock(&arp_globals.lock);168 fibril_mutex_unlock(&arp_globals.lock); 142 169 return ENOENT; 143 170 } 171 trans = arp_addr_find(&proto->addresses, address->value, address->length); 172 if (trans) 173 arp_clear_trans(trans); 144 174 arp_addr_exclude(&proto->addresses, address->value, address->length); 145 fibril_ rwlock_write_unlock(&arp_globals.lock);175 fibril_mutex_unlock(&arp_globals.lock); 146 176 return EOK; 147 177 } … … 152 182 arp_device_t *device; 153 183 154 fibril_ rwlock_write_lock(&arp_globals.lock);184 fibril_mutex_lock(&arp_globals.lock); 155 185 device = arp_cache_find(&arp_globals.cache, device_id); 156 186 if (!device) { 157 fibril_ rwlock_write_unlock(&arp_globals.lock);187 fibril_mutex_unlock(&arp_globals.lock); 158 188 return ENOENT; 159 189 } 160 190 arp_clear_device(device); 161 191 printf("Device %d cleared\n", device_id); 162 fibril_ rwlock_write_unlock(&arp_globals.lock);192 fibril_mutex_unlock(&arp_globals.lock); 163 193 return EOK; 164 194 } … … 221 251 int rc; 222 252 223 fibril_ rwlock_write_lock(&arp_globals.lock);253 fibril_mutex_lock(&arp_globals.lock); 224 254 225 255 /* An existing device? */ … … 229 259 if (device->service != service) { 230 260 printf("Device %d already exists\n", device->device_id); 231 fibril_ rwlock_write_unlock(&arp_globals.lock);261 fibril_mutex_unlock(&arp_globals.lock); 232 262 return EEXIST; 233 263 } … … 241 271 rc = arp_proto_create(&proto, protocol, address); 242 272 if (rc != EOK) { 243 fibril_ rwlock_write_unlock(&arp_globals.lock);273 fibril_mutex_unlock(&arp_globals.lock); 244 274 return rc; 245 275 } … … 247 277 proto); 248 278 if (index < 0) { 249 fibril_ rwlock_write_unlock(&arp_globals.lock);279 fibril_mutex_unlock(&arp_globals.lock); 250 280 free(proto); 251 281 return index; … … 262 292 device = (arp_device_t *) malloc(sizeof(arp_device_t)); 263 293 if (!device) { 264 fibril_ rwlock_write_unlock(&arp_globals.lock);294 fibril_mutex_unlock(&arp_globals.lock); 265 295 return ENOMEM; 266 296 } … … 269 299 rc = arp_protos_initialize(&device->protos); 270 300 if (rc != EOK) { 271 fibril_ rwlock_write_unlock(&arp_globals.lock);301 fibril_mutex_unlock(&arp_globals.lock); 272 302 free(device); 273 303 return rc; … … 275 305 rc = arp_proto_create(&proto, protocol, address); 276 306 if (rc != EOK) { 277 fibril_ rwlock_write_unlock(&arp_globals.lock);307 fibril_mutex_unlock(&arp_globals.lock); 278 308 free(device); 279 309 return rc; … … 281 311 index = arp_protos_add(&device->protos, proto->service, proto); 282 312 if (index < 0) { 283 fibril_ rwlock_write_unlock(&arp_globals.lock);313 fibril_mutex_unlock(&arp_globals.lock); 284 314 arp_protos_destroy(&device->protos); 285 315 free(device); … … 293 323 arp_globals.client_connection); 294 324 if (device->phone < 0) { 295 fibril_ rwlock_write_unlock(&arp_globals.lock);325 fibril_mutex_unlock(&arp_globals.lock); 296 326 arp_protos_destroy(&device->protos); 297 327 free(device); … … 303 333 &device->packet_dimension); 304 334 if (rc != EOK) { 305 fibril_ rwlock_write_unlock(&arp_globals.lock);335 fibril_mutex_unlock(&arp_globals.lock); 306 336 arp_protos_destroy(&device->protos); 307 337 free(device); … … 313 343 &device->addr_data); 314 344 if (rc != EOK) { 315 fibril_ rwlock_write_unlock(&arp_globals.lock);345 fibril_mutex_unlock(&arp_globals.lock); 316 346 arp_protos_destroy(&device->protos); 317 347 free(device); … … 323 353 &device->broadcast_addr, &device->broadcast_data); 324 354 if (rc != EOK) { 325 fibril_ rwlock_write_unlock(&arp_globals.lock);355 fibril_mutex_unlock(&arp_globals.lock); 326 356 free(device->addr); 327 357 free(device->addr_data); … … 334 364 device); 335 365 if (rc != EOK) { 336 fibril_ rwlock_write_unlock(&arp_globals.lock);366 fibril_mutex_unlock(&arp_globals.lock); 337 367 free(device->addr); 338 368 free(device->addr_data); … … 347 377 device->service, protocol); 348 378 } 349 fibril_ rwlock_write_unlock(&arp_globals.lock);379 fibril_mutex_unlock(&arp_globals.lock); 350 380 351 381 return EOK; … … 363 393 int rc; 364 394 365 fibril_ rwlock_initialize(&arp_globals.lock);366 fibril_ rwlock_write_lock(&arp_globals.lock);395 fibril_mutex_initialize(&arp_globals.lock); 396 fibril_mutex_lock(&arp_globals.lock); 367 397 arp_globals.client_connection = client_connection; 368 398 rc = arp_cache_initialize(&arp_globals.cache); 369 fibril_ rwlock_write_unlock(&arp_globals.lock);399 fibril_mutex_unlock(&arp_globals.lock); 370 400 371 401 return rc; … … 383 413 arp_device_t *device; 384 414 385 fibril_ rwlock_write_lock(&arp_globals.lock);415 fibril_mutex_lock(&arp_globals.lock); 386 416 device = arp_cache_find(&arp_globals.cache, device_id); 387 417 if (!device) { 388 fibril_ rwlock_write_unlock(&arp_globals.lock);418 fibril_mutex_unlock(&arp_globals.lock); 389 419 return ENOENT; 390 420 } 391 421 device->packet_dimension.content = mtu; 392 fibril_ rwlock_write_unlock(&arp_globals.lock);422 fibril_mutex_unlock(&arp_globals.lock); 393 423 printf("arp - device %d changed mtu to %d\n\n", device_id, mtu); 394 424 return EOK; … … 421 451 arp_device_t *device; 422 452 arp_proto_t *proto; 423 measured_string_t *hw_source;453 arp_trans_t *trans; 424 454 uint8_t *src_hw; 425 455 uint8_t *src_proto; … … 452 482 des_hw = src_proto + header->protocol_length; 453 483 des_proto = des_hw + header->hardware_length; 454 hw_source= arp_addr_find(&proto->addresses, (char *) src_proto,484 trans = arp_addr_find(&proto->addresses, (char *) src_proto, 455 485 CONVERT_SIZE(uint8_t, char, header->protocol_length)); 456 486 /* Exists? */ 457 if ( hw_source) {458 if ( hw_source->length != CONVERT_SIZE(uint8_t, char,487 if (trans && trans->hw_addr) { 488 if (trans->hw_addr->length != CONVERT_SIZE(uint8_t, char, 459 489 header->hardware_length)) { 460 490 return EINVAL; 461 491 } 462 memcpy( hw_source->value, src_hw, hw_source->length);492 memcpy(trans->hw_addr->value, src_hw, trans->hw_addr->length); 463 493 } 464 494 /* Is my protocol address? */ … … 470 500 proto->addr->length)) { 471 501 /* Not already updated? */ 472 if (!hw_source) { 473 hw_source = measured_string_create_bulk((char *) src_hw, 474 CONVERT_SIZE(uint8_t, char, 502 if (!trans) { 503 trans = (arp_trans_t *) malloc(sizeof(arp_trans_t)); 504 if (!trans) 505 return ENOMEM; 506 trans->hw_addr = NULL; 507 fibril_condvar_initialize(&trans->cv); 508 rc = arp_addr_add(&proto->addresses, (char *) src_proto, 509 CONVERT_SIZE(uint8_t, char, header->protocol_length), 510 trans); 511 if (rc != EOK) { 512 /* The generic char map has already freed trans! */ 513 return rc; 514 } 515 } 516 if (!trans->hw_addr) { 517 trans->hw_addr = measured_string_create_bulk( 518 (char *) src_hw, CONVERT_SIZE(uint8_t, char, 475 519 header->hardware_length)); 476 if (! hw_source)520 if (!trans->hw_addr) 477 521 return ENOMEM; 478 522 479 rc = arp_addr_add(&proto->addresses, (char *) src_proto, 480 CONVERT_SIZE(uint8_t, char, 481 header->protocol_length), hw_source); 482 if (rc != EOK) 483 return rc; 523 /* Notify the fibrils that wait for the translation. */ 524 fibril_condvar_broadcast(&trans->cv); 484 525 } 485 526 if (ntohs(header->operation) == ARPOP_REQUEST) { … … 490 531 memcpy(src_hw, device->addr->value, 491 532 device->packet_dimension.addr_len); 492 memcpy(des_hw, hw_source->value,533 memcpy(des_hw, trans->hw_addr->value, 493 534 header->hardware_length); 494 535 … … 527 568 arp_device_t *device; 528 569 arp_proto_t *proto; 529 measured_string_t *addr;570 arp_trans_t *trans; 530 571 size_t length; 531 572 packet_t *packet; 532 573 arp_header_t *header; 574 bool retry = false; 533 575 int rc; 534 576 577 restart: 535 578 if (!target || !translation) 536 579 return EBADMEM; … … 544 587 return ENOENT; 545 588 546 addr = arp_addr_find(&proto->addresses, target->value, target->length); 547 if (addr) { 548 *translation = addr; 549 return EOK; 550 } 589 trans = arp_addr_find(&proto->addresses, target->value, target->length); 590 if (trans) { 591 if (trans->hw_addr) { 592 *translation = trans->hw_addr; 593 return EOK; 594 } 595 if (retry) 596 return EAGAIN; 597 rc = fibril_condvar_wait_timeout(&trans->cv, &arp_globals.lock, 598 ARP_TRANS_WAIT); 599 if (rc == ETIMEOUT) 600 return ENOENT; 601 retry = true; 602 goto restart; 603 } 604 if (retry) 605 return EAGAIN; 551 606 552 607 /* ARP packet content size = header + (address + translation) * 2 */ … … 593 648 594 649 nil_send_msg(device->phone, device_id, packet, SERVICE_ARP); 595 return EAGAIN; 650 651 trans = (arp_trans_t *) malloc(sizeof(arp_trans_t)); 652 if (!trans) 653 return ENOMEM; 654 trans->hw_addr = NULL; 655 fibril_condvar_initialize(&trans->cv); 656 rc = arp_addr_add(&proto->addresses, target->value, target->length, 657 trans); 658 if (rc != EOK) { 659 /* The generic char map has already freed trans! */ 660 return rc; 661 } 662 663 rc = fibril_condvar_wait_timeout(&trans->cv, &arp_globals.lock, 664 ARP_TRANS_WAIT); 665 if (rc == ETIMEOUT) 666 return ENOENT; 667 retry = true; 668 goto restart; 596 669 } 597 670 … … 644 717 return rc; 645 718 646 fibril_ rwlock_read_lock(&arp_globals.lock);719 fibril_mutex_lock(&arp_globals.lock); 647 720 rc = arp_translate_message(IPC_GET_DEVICE(call), 648 721 IPC_GET_SERVICE(call), address, &translation); … … 650 723 free(data); 651 724 if (rc != EOK) { 652 fibril_ rwlock_read_unlock(&arp_globals.lock);725 fibril_mutex_unlock(&arp_globals.lock); 653 726 return rc; 654 727 } 655 728 if (!translation) { 656 fibril_ rwlock_read_unlock(&arp_globals.lock);729 fibril_mutex_unlock(&arp_globals.lock); 657 730 return ENOENT; 658 731 } 659 732 rc = measured_strings_reply(translation, 1); 660 fibril_ rwlock_read_unlock(&arp_globals.lock);733 fibril_mutex_unlock(&arp_globals.lock); 661 734 return rc; 662 735 … … 688 761 return rc; 689 762 690 fibril_ rwlock_read_lock(&arp_globals.lock);763 fibril_mutex_lock(&arp_globals.lock); 691 764 do { 692 765 next = pq_detach(packet); … … 698 771 packet = next; 699 772 } while (packet); 700 fibril_ rwlock_read_unlock(&arp_globals.lock);773 fibril_mutex_unlock(&arp_globals.lock); 701 774 702 775 return EOK; -
uspace/srv/net/il/arp/arp.h
r442ebbe rfc3dba14 65 65 typedef struct arp_proto arp_proto_t; 66 66 67 /** Type definition of the ARP address translation record. 68 * @see arp_trans 69 */ 70 typedef struct arp_trans arp_trans_t; 71 67 72 /** ARP address map. 68 73 * … … 70 75 * @see generic_char_map.h 71 76 */ 72 GENERIC_CHAR_MAP_DECLARE(arp_addr, measured_string_t);77 GENERIC_CHAR_MAP_DECLARE(arp_addr, arp_trans_t); 73 78 74 79 /** ARP address cache. … … 89 94 struct arp_device { 90 95 /** Actual device hardware address. */ 91 measured_string_t * 96 measured_string_t *addr; 92 97 /** Actual device hardware address data. */ 93 98 char *addr_data; 94 99 /** Broadcast device hardware address. */ 95 measured_string_t * 100 measured_string_t *broadcast_addr; 96 101 /** Broadcast device hardware address data. */ 97 102 char *broadcast_data; … … 129 134 int net_phone; 130 135 /** Safety lock. */ 131 fibril_ rwlock_t lock;136 fibril_mutex_t lock; 132 137 }; 133 138 … … 144 149 }; 145 150 151 /** ARP address translation record. */ 152 struct arp_trans { 153 /** 154 * Hardware address for the translation. NULL denotes an incomplete 155 * record with possible waiters. 156 */ 157 measured_string_t *hw_addr; 158 /** Condition variable used for waiting for completion of the record. */ 159 fibril_condvar_t cv; 160 }; 161 146 162 #endif 147 163 148 164 /** @} 149 165 */ 166
Note:
See TracChangeset
for help on using the changeset viewer.