hdr_sr.h

Go to the documentation of this file.
00001 
00002 /*
00003  * hdr_sr.h
00004  * Copyright (C) 2000 by the University of Southern California
00005  * $Id: hdr_sr.h,v 1.8 2005/08/25 18:58:04 johnh Exp $
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License,
00009  * version 2, as published by the Free Software Foundation.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License along
00017  * with this program; if not, write to the Free Software Foundation, Inc.,
00018  * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
00019  *
00020  *
00021  * The copyright of this module includes the following
00022  * linking-with-specific-other-licenses addition:
00023  *
00024  * In addition, as a special exception, the copyright holders of
00025  * this module give you permission to combine (via static or
00026  * dynamic linking) this module with free software programs or
00027  * libraries that are released under the GNU LGPL and with code
00028  * included in the standard release of ns-2 under the Apache 2.0
00029  * license or under otherwise-compatible licenses with advertising
00030  * requirements (or modified versions of such code, with unchanged
00031  * license).  You may copy and distribute such a system following the
00032  * terms of the GNU GPL for this module and the licenses of the
00033  * other code concerned, provided that you include the source code of
00034  * that other code when and as the GNU GPL requires distribution of
00035  * source code.
00036  *
00037  * Note that people who make modified versions of this module
00038  * are not obligated to grant this special exception for their
00039  * modified versions; it is their choice whether to do so.  The GNU
00040  * General Public License gives permission to release a modified
00041  * version without this exception; this exception also makes it
00042  * possible to release a modified version which carries forward this
00043  * exception.
00044  *
00045  */
00046 
00047 // Other copyrights might apply to parts of this software and are so
00048 // noted when applicable.
00049 //
00050 // Ported from CMU/Monarch's code, appropriate copyright applies.  
00051 
00052 /* -*- c++ -*-
00053    hdr_sr.h
00054 
00055    source route header
00056    
00057 */
00058 #ifndef sr_hdr_h
00059 #define sr_hdr_h
00060 
00061 #include <assert.h>
00062 
00063 #include <packet.h>
00064 
00065 #define SR_HDR_SZ 4     // size of constant part of hdr
00066 
00067 #define MAX_SR_LEN 16       // longest source route we can handle
00068 #define MAX_ROUTE_ERRORS 3  // how many route errors can fit in one pkt?
00069 
00070 struct sr_addr {
00071         int addr_type;      /* same as hdr_cmn in packet.h */
00072     nsaddr_t addr;
00073 
00074     /*
00075      * Metrics that I want to collect at each node
00076      */
00077     double  Pt_;
00078 };
00079 
00080 struct link_down {
00081     int addr_type;      /* same as hdr_cmn in packet.h */
00082     nsaddr_t tell_addr; // tell this host
00083     nsaddr_t from_addr; // that from_addr host can no longer
00084     nsaddr_t to_addr;   // get packets to to_addr host
00085 };
00086 
00087 
00088 /* ======================================================================
00089    DSR Packet Types
00090    ====================================================================== */
00091 struct route_request {
00092     int req_valid_; /* request header is valid? */
00093     int req_id_;    /* unique request identifier */
00094     int req_ttl_;   /* max propagation */
00095 };
00096 
00097 struct route_reply {
00098     int rep_valid_; /* reply header is valid? */
00099     int rep_rtlen_; /* # hops in route reply */
00100     struct sr_addr  rep_addrs_[MAX_SR_LEN];
00101 };
00102 
00103 struct route_error {
00104     int err_valid_; /* error header is valid? */
00105     int err_count_; /* number of route errors */
00106     struct link_down err_links_[MAX_ROUTE_ERRORS];
00107 };
00108 
00109 /* ======================================================================
00110    DSR Flow State Draft Stuff
00111    ====================================================================== */
00112 
00113 struct flow_error {
00114     nsaddr_t  flow_src;
00115     nsaddr_t  flow_dst;
00116     u_int16_t flow_id;  /* not valid w/ default flow stuff */
00117 };
00118 
00119 struct flow_header {
00120     int flow_valid_;
00121     int hopCount_;
00122     unsigned short flow_id_;
00123 };
00124 
00125 struct flow_timeout {
00126     int flow_timeout_valid_;
00127     unsigned long timeout_;  // timeout in seconds...
00128 };
00129 
00130 struct flow_unknown {
00131     int flow_unknown_valid_;
00132     int err_count_;
00133     struct flow_error err_flows_[MAX_ROUTE_ERRORS];
00134 };
00135 
00136 // default flow unknown errors
00137 struct flow_default_err {
00138     int flow_default_valid_;
00139     int err_count_;
00140     struct flow_error err_flows_[MAX_ROUTE_ERRORS];
00141 };
00142 
00143 /* ======================================================================
00144    DSR Header
00145    ====================================================================== */
00146 class hdr_sr {
00147 private:
00148     int valid_;     /* is this header actually in the packet? 
00149                    and initialized? */
00150     int salvaged_;  /* packet has been salvaged? */
00151 
00152     int num_addrs_;
00153     int cur_addr_;
00154     struct sr_addr addrs_[MAX_SR_LEN];
00155 
00156     struct route_request    sr_request_;
00157     struct route_reply  sr_reply_;
00158     struct route_error  sr_error_;
00159 
00160     struct flow_header      sr_flow_;
00161     struct flow_timeout sr_ftime_;
00162     struct flow_unknown sr_funk_;
00163     struct flow_default_err sr_fdef_unk;
00164 
00165 public:
00166     static int offset_;     /* offset for this header */
00167     inline int& offset() { return offset_; }
00168         inline static hdr_sr* access(const Packet* p) {
00169             return (hdr_sr*)p->access(offset_);
00170     }
00171     inline int& valid() { return valid_; }
00172     inline int& salvaged() { return salvaged_; }
00173     inline int& num_addrs() { return num_addrs_; }
00174     inline int& cur_addr() { return cur_addr_; }
00175 
00176     inline int valid() const { return valid_; }
00177     inline int salvaged() const { return salvaged_; }
00178     inline int num_addrs() const { return num_addrs_; }
00179     inline int cur_addr() const { return cur_addr_; }
00180     inline struct sr_addr* addrs() { return addrs_; }
00181 
00182     inline int& route_request() {return sr_request_.req_valid_; }
00183     inline int& rtreq_seq() {return sr_request_.req_id_; }
00184     inline int& max_propagation() {return sr_request_.req_ttl_; }
00185 
00186     inline int& route_reply() {return sr_reply_.rep_valid_; }
00187     inline int& route_reply_len() {return sr_reply_.rep_rtlen_; }
00188     inline struct sr_addr* reply_addrs() {return sr_reply_.rep_addrs_; }
00189 
00190     inline int& route_error() {return sr_error_.err_valid_; }
00191     inline int& num_route_errors() {return sr_error_.err_count_; }
00192     inline struct link_down* down_links() {return sr_error_.err_links_; }
00193 
00194     // Flow state stuff, ych 5/2/01
00195     inline int &flow_header() { return sr_flow_.flow_valid_; }
00196     inline u_int16_t &flow_id() { return sr_flow_.flow_id_; }
00197     inline int &hopCount() { return sr_flow_.hopCount_; }
00198 
00199     inline int &flow_timeout() { return sr_ftime_.flow_timeout_valid_; }
00200     inline unsigned long &flow_timeout_time() { return sr_ftime_.timeout_; }
00201 
00202     inline int &flow_unknown() { return sr_funk_.flow_unknown_valid_; }
00203     inline int &num_flow_unknown() { return sr_funk_.err_count_; }
00204     inline struct flow_error *unknown_flows() { return sr_funk_.err_flows_; }
00205 
00206     inline int &flow_default_unknown() { return sr_fdef_unk.flow_default_valid_; }
00207     inline int &num_default_unknown() { return sr_fdef_unk.err_count_; }
00208     inline struct flow_error *unknown_defaults() { return sr_fdef_unk.err_flows_; }
00209 
00210     inline int size() {
00211         int sz = 0;
00212         if (num_addrs_ || route_request() || 
00213             route_reply() || route_error() ||
00214             flow_timeout() || flow_unknown() || flow_default_unknown())
00215             sz += SR_HDR_SZ;
00216 
00217         if (num_addrs_)         sz += 4 * (num_addrs_ - 1);
00218         if (route_reply())      sz += 5 + 4 * route_reply_len();
00219         if (route_request())        sz += 8;
00220         if (route_error())      sz += 16 * num_route_errors();
00221         if (flow_timeout())     sz += 4;
00222         if (flow_unknown())     sz += 14 * num_flow_unknown();
00223         if (flow_default_unknown()) sz += 12 * num_default_unknown();
00224 
00225         if (flow_header())      sz += 4;
00226 
00227         sz = ((sz+3)&(~3)); // align...
00228         assert(sz >= 0);
00229 #if 0
00230         printf("Size: %d (%d %d %d %d %d %d %d %d %d)\n", sz,
00231             (num_addrs_ || route_request() ||
00232             route_reply() || route_error() ||
00233             flow_timeout() || flow_unknown() || 
00234             flow_default_unknown()) ? SR_HDR_SZ : 0,
00235             num_addrs_ ? 4 * (num_addrs_ - 1) : 0,
00236             route_reply() ? 5 + 4 * route_reply_len() : 0,
00237             route_request() ? 8 : 0,
00238             route_error() ? 16 * num_route_errors() : 0,
00239             flow_timeout() ? 4 : 0,
00240             flow_unknown() ? 14 * num_flow_unknown() : 0,
00241             flow_default_unknown() ? 12 * num_default_unknown() : 0,
00242             flow_header() ? 4 : 0);
00243 #endif
00244 
00245         return sz;
00246     }
00247 
00248     // End Flow State stuff
00249 
00250     inline nsaddr_t& get_next_addr() { 
00251         assert(cur_addr_ < num_addrs_);
00252         return (addrs_[cur_addr_ + 1].addr);
00253     }
00254 
00255     inline int& get_next_type() {
00256         assert(cur_addr_ < num_addrs_);
00257         return (addrs_[cur_addr_ + 1].addr_type);
00258     }
00259 
00260     inline void append_addr(nsaddr_t a, int type) {
00261         assert(num_addrs_ < MAX_SR_LEN-1);
00262         addrs_[num_addrs_].addr_type = type;
00263         addrs_[num_addrs_++].addr = a;
00264     }
00265 
00266     inline void init() {
00267         valid_ = 1;
00268         salvaged_ = 0;
00269         num_addrs_ = 0;
00270         cur_addr_ = 0;
00271 
00272         route_request() = 0;
00273         route_reply() = 0;
00274         route_reply_len() = 0;
00275         route_error() = 0;
00276         num_route_errors() = 0;
00277 
00278         flow_timeout() = 0;
00279         flow_unknown() = 0;
00280         flow_default_unknown() = 0;
00281         flow_header() = 0;
00282     }
00283 
00284 #if 0
00285 #ifdef DSR_CONST_HDR_SZ
00286   /* used to estimate the potential benefit of removing the 
00287      src route in every packet */
00288     inline int size() { 
00289         return SR_HDR_SZ;
00290     }
00291 #else
00292     inline int size() { 
00293         int sz = SR_HDR_SZ +
00294             4 * (num_addrs_ - 1) +
00295             4 * (route_reply() ? route_reply_len() : 0) +
00296             8 * (route_error() ? num_route_errors() : 0);
00297         assert(sz >= 0);
00298         return sz;
00299     }
00300 #endif // DSR_CONST_HDR_SZ
00301 #endif // 0
00302 
00303   void dump(char *);
00304   char* dump();
00305 };
00306 
00307 
00308 #endif // sr_hdr_h

Generated on Tue Mar 6 16:47:45 2007 for ns2 Network Simulator 2.29 by  doxygen 1.4.6