/* Copyright (c) 2012, Matthias Schiffer All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #ifndef _FFD_FFD_H_ #define _FFD_FFD_H_ #include "util.h" #include "netif.h" #include #define FFD_PACKET_MAX 1000 #define FFD_PROTO 0xffd #define FFD_HELLO_INTERVAL 400 #define FFD_IHU_INTERVAL (3*FFD_HELLO_INTERVAL) #define FFD_UPDATE_INTERVAL 3000 #define FFD_HELLO_TIMEOUT(interval) (16*(interval)) #define FFD_IHU_TIMEOUT(interval) ((interval)*7/2) #define FFD_UPDATE_TIMEOUT(interval) ((interval)*7/2) /* 3.5 intervals */ #define FFD_UPDATE_REQUEST_TIMEOUT(interval) ((interval)*13/4) /* 3.25 intervals */ #define FFD_SEQNO_REQUEST_TIMEOUT 50 #define FFD_MAINTENANCE_INTERVAL FFD_HELLO_INTERVAL #define FFD_DELAY (FFD_HELLO_INTERVAL/2) #define FFD_URGENT_DELAY 20 #define FFD_ACK_INTERVAL FFD_URGENT_DELAY #define FFD_UPDATE_WITH_DATA 0x01 typedef struct __attribute__((packed)) _ffd_node_id_t { uint8_t id[8]; } ffd_node_id_t; typedef struct _ffd_metric_seqno_t { uint16_t metric; uint16_t seqno; } ffd_metric_seqno_t; typedef struct _ffd_nexthop_t { struct _ffd_nexthop_t *next; struct _ffd_neigh_t *neigh; ffd_metric_seqno_t metric_seqno; struct timespec last_update; uint16_t interval; bool requested_update; } ffd_nexthop_t; typedef struct _ffd_seqno_req_t { struct _ffd_seqno_req_t *next; struct timespec last_req; struct _ffd_neigh_t *neigh; uint16_t seqno; } ffd_seqno_req_t; typedef struct _ffd_announce_t { struct _ffd_announce_t *next; ffd_node_id_t node; uint16_t type; uint16_t key; ffd_metric_seqno_t metric; uint16_t last_metric; ffd_metric_seqno_t feasibility_distance; ffd_seqno_req_t *seqno_req_list; ffd_nexthop_t *selected; ffd_nexthop_t *nexthop_list; /* an incomplete announcement is specified by a len value of 0xff with NULL data */ uint8_t len; uint8_t *data; } ffd_announce_t; typedef struct _ffd_neigh_t { struct _ffd_neigh_t *next; unsigned ref; struct _ffd_iface_t *iface; eth_addr_t addr; uint16_t hello_log; uint16_t hello_interval; uint16_t last_seqno; struct timespec last_hello; uint16_t ihu_interval; struct timespec last_ihu; uint16_t txcost; } ffd_neigh_t; typedef struct _ffd_iface_t { struct _ffd_iface_t *next; unsigned ifindex; char name[IF_NAMESIZE]; eth_addr_t addr; netif_type_t type; uint16_t seqno; ffd_neigh_t *neigh_list; } ffd_iface_t; extern const eth_addr_t ffd_addr; extern ffd_node_id_t self; extern ffd_iface_t *iface_list; extern ffd_announce_t *announce_list; extern int sockfd; #define FFD_NODE_ID_UNSPEC ((ffd_node_id_t){}) static inline bool ffd_is_node_id_unspec(const ffd_node_id_t *node) { const uint8_t *id = node->id; if (id[0]||id[1]||id[2]||id[3]||id[4]||id[5]||id[6]||id[7]) return false; else return true; } static inline bool ffd_are_node_ids_equal(const ffd_node_id_t *id1, const ffd_node_id_t *id2) { const uint8_t *a = id1->id; const uint8_t *b = id2->id; return (a[0]==b[0] && a[1]==b[1] && a[2]==b[2] && a[3]==b[3] && a[4]==b[4] && a[5]==b[5] && a[6]==b[6] && a[7]==b[7]); } #define FFD_IS_INFINITY(m) ((m).metric == 0xffff) /* returns true if ms1 is better than ms2 */ static inline bool ffd_is_metric_better(ffd_metric_seqno_t ms1, ffd_metric_seqno_t ms2) { if (FFD_IS_INFINITY(ms1)) return false; if (FFD_IS_INFINITY(ms2)) return true; int16_t seqno_diff = ms2.seqno - ms1.seqno; if (seqno_diff < 0) return true; if (seqno_diff == 0 && ms1.metric < ms2.metric) return true; return false; } static inline bool ffd_is_feasible(const ffd_announce_t *announce, ffd_metric_seqno_t ms) { if (FFD_IS_INFINITY(ms)) return true; return ffd_is_metric_better(ms, announce->feasibility_distance); } void ffd_announce_update(ffd_announce_t *announce); void ffd_announce_update_nexthop(ffd_announce_t *announce, ffd_nexthop_t *nexthop, ffd_metric_seqno_t ms, uint16_t interval); ffd_announce_t* ffd_announce_new(void); ffd_announce_t* ffd_announce_find(const ffd_node_id_t *node, uint16_t type, uint16_t key); ffd_announce_t* ffd_announce_get(const ffd_node_id_t *node, uint16_t type, uint16_t key); ffd_nexthop_t* ffd_announce_nexthop_find(const ffd_announce_t *announce, ffd_neigh_t *neigh); ffd_nexthop_t* ffd_announce_nexthop_new(ffd_announce_t *announce, ffd_neigh_t *neigh); bool ffd_announce_seqno_request(ffd_announce_t *announce, ffd_neigh_t *neigh, uint16_t seqno); void ffd_announce_free(ffd_announce_t *announce); void ffd_ack_handle(uint16_t n); void ffd_ack_request(void (*cb)(uint16_t nonce, void *arg), void (*free_cb)(uint16_t nonce, void *arg), unsigned interval, unsigned retries, void *arg); int ffd_ack_timeout(void); void ffd_ack_run(void); void ffd_update_enqueue(const ffd_node_id_t *node, uint16_t type, uint16_t key, ffd_neigh_t *neigh, bool urgent); int ffd_update_timeout(void); void ffd_update_run(void); #endif /* _FFD_FFD_H_ */