1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671
|
/*
* "Copyright (c) 2008 The Regents of the University of California.
* All rights reserved."
*
* Permission to use, copy, modify, and distribute this software and its
* documentation for any purpose, without fee, and without written agreement is
* hereby granted, provided that the above copyright notice, the following
* two paragraphs and the author appear in all copies of this software.
*
* IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
* DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
* OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
* CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
* ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
* PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
*
*/
#include <lib6lowpan/blip-tinyos-includes.h>
#include <lib6lowpan/6lowpan.h>
#include <lib6lowpan/lib6lowpan.h>
#include <lib6lowpan/ip.h>
#include <lib6lowpan/in_cksum.h>
#include <lib6lowpan/ip_malloc.h>
#include "blip_printf.h"
#include "IPDispatch.h"
#include "BlipStatistics.h"
#include "table.h"
/*
* Provides IP layer reception to applications on motes.
*
* @author Stephen Dawson-Haggerty <stevedh@cs.berkeley.edu>
*/
module IPDispatchP {
provides {
interface SplitControl;
// interface for protocols not requiring special hand-holding
interface IPLower;
interface BlipStatistics<ip_statistics_t>;
}
uses {
interface Boot;
/* link-layer wiring */
interface SplitControl as RadioControl;
interface Packet as BarePacket;
interface Send as Ieee154Send;
interface Receive as Ieee154Receive;
/* context lookup */
interface NeighborDiscovery;
interface ReadLqi;
interface PacketLink;
interface LowPowerListening;
/* buffers for outgoing fragments */
interface Pool<message_t> as FragPool;
interface Pool<struct send_info> as SendInfoPool;
interface Pool<struct send_entry> as SendEntryPool;
interface Queue<struct send_entry *> as SendQueue;
/* expire reconstruction */
interface Timer<TMilli> as ExpireTimer;
interface Leds;
}
provides interface Init;
} implementation {
#define HAVE_LOWPAN_EXTERN_MATCH_CONTEXT
int lowpan_extern_read_context(struct in6_addr *addr, int context) {
return call NeighborDiscovery.getContext(context, addr);
}
int lowpan_extern_match_context(struct in6_addr *addr, uint8_t *ctx_id) {
return call NeighborDiscovery.matchContext(addr, ctx_id);
}
// generally including source files like this is a no-no. I'm doing
// this in the hope that the optimizer will do a better job when
// they're part of a component.
#include <lib6lowpan/ieee154_header.c>
#include <lib6lowpan/lib6lowpan.c>
#include <lib6lowpan/lib6lowpan_4944.c>
#include <lib6lowpan/lib6lowpan_frag.c>
enum {
S_RUNNING,
S_STOPPED,
S_STOPPING,
};
uint8_t state = S_STOPPED;
bool radioBusy;
uint8_t current_local_label = 0;
ip_statistics_t stats;
// this in theory could be arbitrarily large; however, it needs to
// be large enough to hold all active reconstructions, and any tags
// which we are dropping. It's important to keep dropped tags
// around for a while, or else there are pathological situations
// where you continually allocate buffers for packets which will
// never complete.
////////////////////////////////////////
//
//
table_t recon_cache;
// table of packets we are currently receiving fragments from, that
// are destined to us
struct lowpan_reconstruct recon_data[N_RECONSTRUCTIONS];
//
//
////////////////////////////////////////
// task void sendTask();
void reconstruct_clear(void *ent) {
struct lowpan_reconstruct *recon = (struct lowpan_reconstruct *)ent;
memclr((uint8_t *)&recon->r_meta, sizeof(struct ip6_metadata));
recon->r_timeout = T_UNUSED;
recon->r_buf = NULL;
}
struct send_info *getSendInfo() {
struct send_info *ret = call SendInfoPool.get();
if (ret == NULL) return ret;
ret->_refcount = 1;
ret->upper_data = NULL;
ret->failed = FALSE;
ret->link_transmissions = 0;
ret->link_fragments = 0;
ret->link_fragment_attempts = 0;
return ret;
}
#define SENDINFO_INCR(X) ((X)->_refcount)++
void SENDINFO_DECR(struct send_info *si) {
if (--(si->_refcount) == 0) {
call SendInfoPool.put(si);
}
}
command error_t SplitControl.start() {
return call RadioControl.start();
}
command error_t SplitControl.stop() {
if (!radioBusy) {
state = S_STOPPED;
return call RadioControl.stop();
} else {
// if there's a packet in the radio, wait for it to exit before
// stopping
state = S_STOPPING;
return SUCCESS;
}
}
event void RadioControl.startDone(error_t error) {
#ifdef LPL_SLEEP_INTERVAL
call LowPowerListening.setLocalWakeupInterval(LPL_SLEEP_INTERVAL);
#endif
if (error == SUCCESS) {
call Leds.led2Toggle();
call ExpireTimer.startPeriodic(FRAG_EXPIRE_TIME);
state = S_RUNNING;
radioBusy = FALSE;
}
signal SplitControl.startDone(error);
}
event void RadioControl.stopDone(error_t error) {
signal SplitControl.stopDone(error);
}
command error_t Init.init() {
// ip_malloc_init needs to be in init, not booted, because
// context for coap is initialised in init
ip_malloc_init();
return SUCCESS;
}
event void Boot.booted() {
call BlipStatistics.clear();
/* set up our reconstruction cache */
table_init(&recon_cache, recon_data, sizeof(struct lowpan_reconstruct), N_RECONSTRUCTIONS);
table_map(&recon_cache, reconstruct_clear);
call SplitControl.start();
}
/*
* Receive-side code.
*/
void deliver(struct lowpan_reconstruct *recon) {
struct ip6_hdr *iph = (struct ip6_hdr *)recon->r_buf;
// printf("deliver [%i]: ", recon->r_bytes_rcvd);
// printf_buf(recon->r_buf, recon->r_bytes_rcvd);
/* the payload length field is always compressed, have to put it back here */
iph->ip6_plen = htons(recon->r_bytes_rcvd - sizeof(struct ip6_hdr));
signal IPLower.recv(iph, (void *)(iph + 1), &recon->r_meta);
// printf("ip_free(%p)\n", recon->r_buf);
ip_free(recon->r_buf);
recon->r_timeout = T_UNUSED;
recon->r_buf = NULL;
}
/*
* Bulletproof recovery logic is very important to make sure we
* don't get wedged with no free buffers.
*
* The table is managed as follows:
* - unused entries are marked T_UNUSED
* - entries which
* o have a buffer allocated
* o have had a fragment reception before we fired
* are marked T_ACTIVE
* - entries which have not had a fragment reception during the last timer period
* and were active are marked T_ZOMBIE
* - zombie receptions are deleted: their buffer is freed and table entry marked unused.
* - when a fragment is dropped, it is entered into the table as T_FAILED1.
* no buffer is allocated
* - when the timer fires, T_FAILED1 entries are aged to T_FAILED2.
* - T_FAILED2 entries are deleted. Incomming fragments with tags
* that are marked either FAILED1 or FAILED2 are dropped; this
* prevents us from allocating a buffer for a packet which we
* have already dropped fragments from.
*
*/
void reconstruct_age(void *elt) {
struct lowpan_reconstruct *recon = (struct lowpan_reconstruct *)elt;
if (recon->r_timeout != T_UNUSED)
printf("recon src: 0x%x tag: 0x%x buf: %p recvd: %i/%i\n",
recon->r_source_key, recon->r_tag, recon->r_buf,
recon->r_bytes_rcvd, recon->r_size);
switch (recon->r_timeout) {
case T_ACTIVE:
recon->r_timeout = T_ZOMBIE; break; // age existing receptions
case T_FAILED1:
recon->r_timeout = T_FAILED2; break; // age existing receptions
case T_ZOMBIE:
case T_FAILED2:
// deallocate the space for reconstruction
printf("timing out buffer: src: %i tag: %i\n", recon->r_source_key, recon->r_tag);
if (recon->r_buf != NULL) {
printf("ip_free(%p)\n", recon->r_buf);
ip_free(recon->r_buf);
}
recon->r_timeout = T_UNUSED;
recon->r_buf = NULL;
break;
}
}
void ip_print_heap() {
#ifdef PRINTFUART_ENABLED
bndrt_t *cur = (bndrt_t *)heap;
while (((uint8_t *)cur) - heap < IP_MALLOC_HEAP_SIZE) {
printf ("heap region start: %p length: %u used: %u\n",
cur, (*cur & IP_MALLOC_LEN), (*cur & IP_MALLOC_INUSE) >> 15);
cur = (bndrt_t *)(((uint8_t *)cur) + ((*cur) & IP_MALLOC_LEN));
}
#endif
}
event void ExpireTimer.fired() {
table_map(&recon_cache, reconstruct_age);
printf("Frag pool size: %i\n", call FragPool.size());
printf("SendInfo pool size: %i\n", call SendInfoPool.size());
printf("SendEntry pool size: %i\n", call SendEntryPool.size());
printf("Forward queue length: %i\n", call SendQueue.size());
ip_print_heap();
printfflush();
}
/*
* allocate a structure for recording information about incomming fragments.
*/
struct lowpan_reconstruct *get_reconstruct(uint16_t key, uint16_t tag) {
struct lowpan_reconstruct *ret = NULL;
int i;
// printf("get_reconstruct: %x %i\n", key, tag);
for (i = 0; i < N_RECONSTRUCTIONS; i++) {
struct lowpan_reconstruct *recon = (struct lowpan_reconstruct *)&recon_data[i];
if (recon->r_tag == tag &&
recon->r_source_key == key) {
if (recon->r_timeout > T_UNUSED) {
recon->r_timeout = T_ACTIVE;
ret = recon;
goto done;
} else if (recon->r_timeout < T_UNUSED) {
// if we have already tried and failed to get a buffer, we
// need to drop remaining fragments.
ret = NULL;
goto done;
}
}
if (recon->r_timeout == T_UNUSED)
ret = recon;
}
done:
// printf("got%p\n", ret);
return ret;
}
event message_t *Ieee154Receive.receive(message_t *msg, void *msg_payload, uint8_t len) {
struct packed_lowmsg lowmsg;
struct ieee154_frame_addr frame_address;
uint8_t *buf = msg_payload;
// printf(" -- RECEIVE -- len : %i\n", len);
BLIP_STATS_INCR(stats.rx_total);
/* unpack the 802.15.4 address fields */
buf = unpack_ieee154_hdr(msg_payload, &frame_address);
len -= buf - (uint8_t *)msg_payload;
/* unpack and 6lowpan headers */
lowmsg.data = buf;
lowmsg.len = len;
lowmsg.headers = getHeaderBitmap(&lowmsg);
if (lowmsg.headers == LOWMSG_NALP) {
goto fail;
}
if (hasFrag1Header(&lowmsg) || hasFragNHeader(&lowmsg)) {
// start reassembly
int rv;
struct lowpan_reconstruct *recon;
uint16_t tag, source_key;
source_key = ieee154_hashaddr(&frame_address.ieee_src);
getFragDgramTag(&lowmsg, &tag);
recon = get_reconstruct(source_key, tag);
if (!recon) {
goto fail;
}
/* fill in metadata: on fragmented packets, it applies to the
first fragment only */
memcpy(&recon->r_meta.sender, &frame_address.ieee_src,
sizeof(ieee154_addr_t));
recon->r_meta.lqi = call ReadLqi.readLqi(msg);
recon->r_meta.rssi = call ReadLqi.readRssi(msg);
if (hasFrag1Header(&lowmsg)) {
if (recon->r_buf != NULL) goto fail;
rv = lowpan_recon_start(&frame_address, recon, buf, len);
} else {
rv = lowpan_recon_add(recon, buf, len);
}
if (rv < 0) {
recon->r_timeout = T_FAILED1;
goto fail;
} else {
// printf("start recon buf: %p\n", recon->r_buf);
recon->r_timeout = T_ACTIVE;
recon->r_source_key = source_key;
recon->r_tag = tag;
}
if (recon->r_size == recon->r_bytes_rcvd) {
deliver(recon);
}
} else {
/* no fragmentation, just deliver it */
int rv;
struct lowpan_reconstruct recon;
/* fill in metadata */
memcpy(&recon.r_meta.sender, &frame_address.ieee_src,
sizeof(ieee154_addr_t));
recon.r_meta.lqi = call ReadLqi.readLqi(msg);
recon.r_meta.rssi = call ReadLqi.readRssi(msg);
buf = getLowpanPayload(&lowmsg);
if ((rv = lowpan_recon_start(&frame_address, &recon, buf, len)) < 0) {
goto fail;
}
if (recon.r_size == recon.r_bytes_rcvd) {
deliver(&recon);
} else {
// printf("ip_free(%p)\n", recon.r_buf);
ip_free(recon.r_buf);
}
}
goto done;
fail:
BLIP_STATS_INCR(stats.rx_drop);
done:
return msg;
}
/*
* Send-side functionality
*/
task void sendTask() {
struct send_entry *s_entry;
// printf("sendTask() - sending\n");
if (radioBusy || state != S_RUNNING) return;
if (call SendQueue.empty()) return;
// this does not dequeue
s_entry = call SendQueue.head();
#ifdef LPL_SLEEP_INTERVAL
call LowPowerListening.setRemoteWakeupInterval(s_entry->msg,
call LowPowerListening.getLocalWakeupInterval());
#endif
if (s_entry->info->failed) {
dbg("Drops", "drops: sendTask: dropping failed fragment\n");
goto fail;
}
if ((call Ieee154Send.send(s_entry->msg,
call BarePacket.payloadLength(s_entry->msg))) != SUCCESS) {
dbg("Drops", "drops: sendTask: send failed\n");
goto fail;
} else {
radioBusy = TRUE;
}
return;
fail:
printf("SEND FAIL\n");
post sendTask();
BLIP_STATS_INCR(stats.tx_drop);
// deallocate the memory associated with this request.
// other fragments associated with this packet will get dropped.
s_entry->info->failed = TRUE;
SENDINFO_DECR(s_entry->info);
call FragPool.put(s_entry->msg);
call SendEntryPool.put(s_entry);
call SendQueue.dequeue();
}
/*
* it will pack the message into the fragment pool and enqueue
* those fragments for sending
*
* it will set
* - payload length
* - version, traffic class and flow label
*
* the source and destination IP addresses must be set by higher
* layers.
*/
command error_t IPLower.send(struct ieee154_frame_addr *frame_addr,
struct ip6_packet *msg,
void *data) {
struct lowpan_ctx ctx;
struct send_info *s_info;
struct send_entry *s_entry;
message_t *outgoing;
int frag_len = 1;
error_t rc = SUCCESS;
if (state != S_RUNNING) {
return EOFF;
}
/* set version to 6 in case upper layers forgot */
msg->ip6_hdr.ip6_vfc &= ~IPV6_VERSION_MASK;
msg->ip6_hdr.ip6_vfc |= IPV6_VERSION;
ctx.tag = current_local_label++;
ctx.offset = 0;
s_info = getSendInfo();
if (s_info == NULL) {
rc = ERETRY;
goto cleanup_outer;
}
s_info->upper_data = data;
while (frag_len > 0) {
s_entry = call SendEntryPool.get();
outgoing = call FragPool.get();
if (s_entry == NULL || outgoing == NULL) {
if (s_entry != NULL)
call SendEntryPool.put(s_entry);
if (outgoing != NULL)
call FragPool.put(outgoing);
// this will cause any fragments we have already enqueued to
// be dropped by the send task.
s_info->failed = TRUE;
printf("drops: IP send: no fragments\n");
rc = ERETRY;
goto done;
}
call BarePacket.clear(outgoing);
frag_len = lowpan_frag_get(call Ieee154Send.getPayload(outgoing, 0),
call BarePacket.maxPayloadLength(),
msg,
frame_addr,
&ctx);
if (frag_len < 0) {
printf(" get frag error: %i\n", frag_len);
}
printf("fragment length: %i offset: %i\n", frag_len, ctx.offset);
call BarePacket.setPayloadLength(outgoing, frag_len);
if (frag_len <= 0) {
call FragPool.put(outgoing);
call SendEntryPool.put(s_entry);
goto done;
}
if (call SendQueue.enqueue(s_entry) != SUCCESS) {
BLIP_STATS_INCR(stats.encfail);
s_info->failed = TRUE;
printf("drops: IP send: enqueue failed\n");
goto done;
}
s_info->link_fragments++;
s_entry->msg = outgoing;
s_entry->info = s_info;
/* configure the L2 */
if (frame_addr->ieee_dst.ieee_mode == IEEE154_ADDR_SHORT &&
frame_addr->ieee_dst.i_saddr == IEEE154_BROADCAST_ADDR) {
call PacketLink.setRetries(s_entry->msg, 0);
} else {
call PacketLink.setRetries(s_entry->msg, BLIP_L2_RETRIES);
}
call PacketLink.setRetryDelay(s_entry->msg, BLIP_L2_DELAY);
SENDINFO_INCR(s_info);}
// printf("got %i frags\n", s_info->link_fragments);
done:
BLIP_STATS_INCR(stats.sent);
SENDINFO_DECR(s_info);
post sendTask();
cleanup_outer:
return rc;
}
event void Ieee154Send.sendDone(message_t *msg, error_t error) {
struct send_entry *s_entry = call SendQueue.head();
radioBusy = FALSE;
// printf("sendDone: %p %i\n", msg, error);
if (state == S_STOPPING) {
call RadioControl.stop();
state = S_STOPPED;
goto done;
}
s_entry->info->link_transmissions += (call PacketLink.getRetries(msg));
s_entry->info->link_fragment_attempts++;
if (!call PacketLink.wasDelivered(msg)) {
printf("sendDone: was not delivered! (%i tries)\n",
call PacketLink.getRetries(msg));
s_entry->info->failed = TRUE;
signal IPLower.sendDone(s_entry->info);
/* if (s_entry->info->policy.dest[0] != 0xffff) */
/* dbg("Drops", "drops: sendDone: frag was not delivered\n"); */
// need to check for broadcast frames
// BLIP_STATS_INCR(stats.tx_drop);
} else if (s_entry->info->link_fragment_attempts ==
s_entry->info->link_fragments) {
signal IPLower.sendDone(s_entry->info);
}
done:
// kill off any pending fragments
SENDINFO_DECR(s_entry->info);
call FragPool.put(s_entry->msg);
call SendEntryPool.put(s_entry);
call SendQueue.dequeue();
post sendTask();
}
#if 0
command struct tlv_hdr *IPExtensions.findTlv(struct ip6_ext *ext, uint8_t tlv_val) {
int len = ext->len - sizeof(struct ip6_ext);
struct tlv_hdr *tlv = (struct tlv_hdr *)(ext + 1);
while (len > 0) {
if (tlv->type == tlv_val) return tlv;
if (tlv->len == 0) return NULL;
tlv = (struct tlv_hdr *)(((uint8_t *)tlv) + tlv->len);
len -= tlv->len;
}
return NULL;
}
#endif
/*
* BlipStatistics interface
*/
command void BlipStatistics.get(ip_statistics_t *statistics) {
#ifdef BLIP_STATS_IP_MEM
stats.fragpool = call FragPool.size();
stats.sendinfo = call SendInfoPool.size();
stats.sendentry= call SendEntryPool.size();
stats.sndqueue = call SendQueue.size();
stats.heapfree = ip_malloc_freespace();
printf("frag: %i sendinfo: %i sendentry: %i sendqueue: %i heap: %i\n",
stats.fragpool,
stats.sendinfo,
stats.sendentry,
stats.sndqueue,
stats.heapfree);
#endif
memcpy(statistics, &stats, sizeof(ip_statistics_t));
}
command void BlipStatistics.clear() {
memclr((uint8_t *)&stats, sizeof(ip_statistics_t));
}
/* default event void IP.recv[uint8_t nxt_hdr](struct ip6_hdr *iph, */
/* void *payload, */
/* struct ip_metadata *meta) { */
/* } */
/* default event void Multicast.recv[uint8_t scope](struct ip6_hdr *iph, */
/* void *payload, */
/* struct ip_metadata *meta) { */
/* } */
}
|