ccnr_private.h

Go to the documentation of this file.
00001 /**
00002  * @file ccnr_private.h
00003  *
00004  * Private definitions for ccnr - the CCNx daemon.
00005  * Data structures are described here so that logging and status
00006  * routines can be compiled separately.
00007  *
00008  * Part of ccnr - the CCNx Repository Daemon.
00009  *
00010  * Copyright (C) 2008-2011 Palo Alto Research Center, Inc.
00011  *
00012  * This work is free software; you can redistribute it and/or modify it under
00013  * the terms of the GNU General Public License version 2 as published by the
00014  * Free Software Foundation.
00015  * This work is distributed in the hope that it will be useful, but WITHOUT ANY
00016  * WARRANTY; without even the implied warranty of MERCHANTABILITY or
00017  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
00018  * for more details. You should have received a copy of the GNU General Public
00019  * License along with this program; if not, write to the
00020  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00021  * Boston, MA 02110-1301, USA.
00022  */
00023  
00024 #ifndef CCNR_PRIVATE_DEFINED
00025 #define CCNR_PRIVATE_DEFINED
00026 
00027 #include <poll.h>
00028 #include <stdarg.h>
00029 #include <stddef.h>
00030 #include <stdint.h>
00031 #include <sys/socket.h>
00032 #include <sys/types.h>
00033 
00034 #include <ccn/ccn_private.h>
00035 #include <ccn/coding.h>
00036 #include <ccn/reg_mgmt.h>
00037 #include <ccn/schedule.h>
00038 #include <ccn/seqwriter.h>
00039 
00040 /*
00041  * These are defined in other ccn headers, but the incomplete types suffice
00042  * for the purposes of this header.
00043  */
00044 struct ccn_charbuf;
00045 struct ccn_indexbuf;
00046 struct hashtb;
00047 struct ccnr_meter;
00048 struct ccn_btree;
00049 
00050 struct SyncBaseStruct;
00051 /*
00052  * These are defined in this header.
00053  */
00054 struct ccnr_handle;
00055 struct fdholder;
00056 struct content_entry;
00057 struct nameprefix_entry;
00058 struct propagating_entry;
00059 struct content_tree_node;
00060 struct ccn_forwarding;
00061 struct enum_state;
00062 struct ccnr_parsed_policy;
00063 
00064 /* Repository-specific content identifiers */
00065 
00066 #if (defined(CCNLINT) && (CCNLINT == 1))
00067 /* This is where we probably want to end up for declaring this type */
00068 typedef uint_least64_t ccnr_accession;
00069 #define CCNR_NULL_ACCESSION ((ccnr_accession)(0))
00070 #define CCNR_MIN_ACCESSION ((ccnr_accession)(1))
00071 #define CCNR_MAX_ACCESSION ((ccnr_accession)(~CCNR_NULL_ACCESSION))
00072 #elif (defined(CCNLINT) && (CCNLINT == 2))
00073 #error "Not expected to work - this is for detecting illegitimate comparisons"
00074 struct intentionally_incomplete;
00075 typedef struct intentionally_incomplete *ccnr_accession;
00076 #define CCNR_NULL_ACCESSION ((ccnr_accession)(0))
00077 #define CCNR_MIN_ACCESSION ((ccnr_accession)(0x10000000))
00078 #define CCNR_MAX_ACCESSION ((ccnr_accession)(0x7fffffff))
00079 #elif (defined(CCNLINT) && (CCNLINT == 3))
00080 #error "Not expected to work - this is for detecting illegitimate casts"
00081 typedef struct ccnr_accession_rep {unsigned a; unsigned b;} ccnr_accession;
00082 struct ccnr_accession_rep ccnr_null_accession;
00083 struct ccnr_accession_rep ccnr_min_accession;
00084 struct ccnr_accession_rep ccnr_max_accession;
00085 #define CCNR_NULL_ACCESSION ccnr_null_accession
00086 #define CCNR_MIN_ACCESSION ccnr_min_accession
00087 #define CCNR_MAX_ACCESSION ccnr_max_accession
00088 #else
00089 typedef uint_least64_t ccnr_accession;
00090 #define CCNR_NULL_ACCESSION ((ccnr_accession)(0))
00091 #define CCNR_MIN_ACCESSION ((ccnr_accession)(1))
00092 #define CCNR_MAX_ACCESSION ((ccnr_accession)(~CCNR_NULL_ACCESSION))
00093 #endif
00094 
00095 #define CCNR_NOT_COMPARABLE (-2)
00096  
00097 /* Encode/decode a ccnr_accession as an unsigned number. */
00098 uintmax_t ccnr_accession_encode(struct ccnr_handle *, ccnr_accession);
00099 ccnr_accession ccnr_accession_decode(struct ccnr_handle *, uintmax_t);
00100 
00101 /* Return 1 if x dominates (is newer than) y, 0 if x equals y, -1 if y dominates x,
00102  * and CCNR_NOT_COMPARABLE if the ordering is not determined
00103  */
00104 int ccnr_accession_compare(struct ccnr_handle *ccnr, ccnr_accession x, ccnr_accession y);
00105 
00106 /* Repository-specific high water marks */
00107 
00108 /* XXX - ccnr_hwm should be a distinct type */
00109 typedef uintmax_t ccnr_hwm;
00110 #define CCNR_NULL_HWM ((ccnr_hwm)(0))
00111 
00112 /* Encode a high water mark as an unsigned number */
00113 uintmax_t ccnr_hwm_encode(struct ccnr_handle *, ccnr_hwm);
00114 ccnr_hwm ccnr_hwm_decode(struct ccnr_handle *, uintmax_t);
00115 
00116 /* Return 1 if a is in the hwm set, 0 if not, -1 if unknown. */
00117 int ccnr_acc_in_hwm(struct ccnr_handle *, ccnr_accession a, ccnr_hwm hwm);
00118 
00119 /* Produce a new high water mark that includes the given content */
00120 ccnr_hwm ccnr_hwm_update(struct ccnr_handle *, ccnr_hwm, ccnr_accession);
00121 ccnr_hwm ccnr_hwm_merge(struct ccnr_handle *, ccnr_hwm, ccnr_hwm);
00122 
00123 /* Return 1 if x dominates y, 0 if x equals y, -1 if y dominates x,
00124  * and CCNR_NOT_COMPARABLE if the ordering is not determined
00125  */
00126 int ccnr_hwm_compare(struct ccnr_handle *ccnr, ccnr_hwm x, ccnr_hwm y);
00127 
00128 /**
00129  * A cookie is used as a more ephemeral way of holding a reference to a
00130  * content object, without the danger of an undetected dangling reference
00131  * when the in-memory content handle is destroyed.  This is for internal
00132  * data structures such as queues or enumeration states, but should not
00133  * be stored in any long-term way.  Use a ccnr_accession, content name, or
00134  * digest for that.
00135  *
00136  * Holding a cookie does not prevent the in-memory content handle from being
00137  * destroyed, either explicitly or to conserve resources.
00138  */
00139 typedef unsigned ccnr_cookie;
00140 
00141 
00142 /** Logger type (ccnr_logger) */
00143 typedef int (*ccnr_logger)(void *loggerdata, const char *format, va_list ap);
00144 
00145 /**
00146  * This is true if we should log at the given level.
00147  * 
00148  */
00149 #define CCNSHOULDLOG(h, who, level) (((h)->debug >= (level)) != 0)
00150 
00151 /* XXX - these are the historical bitfields. */
00152 #define LM_2    2
00153 #define LM_4    4
00154 #define LM_8    8
00155 #define LM_16    16
00156 #define LM_32    32
00157 #define LM_64    64
00158 #define LM_128    128
00159 
00160 /**
00161  * Limit on how many active sync enumerations we are willing to have going.
00162  */
00163 #define CCNR_MAX_ENUM 64
00164 
00165 /**
00166  * We pass this handle almost everywhere within ccnr
00167  */
00168 struct ccnr_handle {
00169     unsigned char ccnr_id[32];      /**< sha256 digest of our public key */
00170     struct ccn_charbuf *ccnr_keyid; /**< public key digest in keyid format %C1.M.K.%00... */
00171     struct hashtb *nameprefix_tab;  /**< keyed by name prefix components */
00172     struct hashtb *propagating_tab; /**< keyed by nonce */
00173     struct hashtb *enum_state_tab;  /**< keyed by enumeration interest */
00174     struct ccn_indexbuf *skiplinks; /**< skiplist for content-ordered ops */
00175     struct ccn_btree *btree;        /**< btree index of content */
00176     unsigned forward_to_gen;        /**< for forward_to updates */
00177     unsigned face_gen;              /**< filedesc generation number */
00178     unsigned face_rover;            /**< for filedesc allocation */
00179     unsigned face_limit;            /**< current number of fdholder slots */
00180     struct fdholder **fdholder_by_fd;  /**< array with face_limit elements */
00181     int active_in_fd;               /**< data currently being indexed */
00182     int active_out_fd;              /**< repo file we will write to */
00183     int repofile1_fd;               /**< read-only access to repoFile1 */
00184     off_t startupbytes;             /**< repoFile1 size at startup */
00185     off_t stable;                   /**< repoFile1 size at shutdown */
00186     struct ccn_scheduled_event *reaper;
00187     struct ccn_scheduled_event *age;
00188     struct ccn_scheduled_event *clean;
00189     struct ccn_scheduled_event *age_forwarding;
00190     struct ccn_scheduled_event *reap_enumerations; /**< cleans out old enumeration state */
00191     struct ccn_scheduled_event *index_cleaner; /**< writes out btree nodes */
00192     struct ccn_indexbuf *toclean;   /**< for index_cleaner use */
00193     const char *portstr;            /**< port number for status display */
00194     nfds_t nfds;                    /**< number of entries in fds array */
00195     struct pollfd *fds;             /**< used for poll system call */
00196     struct ccn_gettime ticktock;    /**< our time generator */
00197     long sec;                       /**< cached gettime seconds */
00198     unsigned usec;                  /**< cached gettime microseconds */
00199     long starttime;                 /**< ccnr start time, in seconds */
00200     unsigned starttime_usec;        /**< ccnr start time fractional part */
00201     struct ccn_schedule *sched;     /**< our schedule */
00202     struct ccn_charbuf *scratch_charbuf; /**< one-slot scratch cache */
00203     struct ccn_indexbuf *scratch_indexbuf; /**< one-slot scratch cache */
00204     /** Next two fields are used for direct cookie-to-content table */
00205     unsigned cookie_limit;          /**< content_by_cookie size(power of 2)*/
00206     struct content_entry **content_by_cookie; /**< cookie-to-content table */
00207     struct hashtb *content_by_accession_tab; /**< keyed by accession */
00208     ccnr_cookie cookie;      /**< newest used cookie number */
00209     ccnr_cookie min_stale;      /**< smallest cookie of stale content */
00210     ccnr_cookie max_stale;      /**< largest cookie of stale content */
00211     ccnr_cookie trim_rover;     /**< where we left off trimming */
00212     unsigned long n_stale;          /**< Number of stale content objects */
00213     struct ccn_indexbuf *unsol;     /**< unsolicited content */
00214     unsigned long cob_count;  /**< count of accessioned content objects in memory */
00215     unsigned long cob_limit;  /**< trim when we get beyond this */
00216     unsigned long oldformatcontent;
00217     unsigned long oldformatcontentgrumble;
00218     unsigned long oldformatinterests;
00219     unsigned long oldformatinterestgrumble;
00220     unsigned long content_dups_recvd;
00221     unsigned long content_items_sent;
00222     unsigned long interests_accepted;
00223     unsigned long interests_dropped;
00224     unsigned long interests_sent;
00225     unsigned long interests_stuffed;
00226     unsigned long content_from_accession_hits;
00227     unsigned long content_from_accession_misses;
00228     unsigned long count_lmc_found;
00229     unsigned long count_lmc_found_iters;
00230     unsigned long count_lmc_notfound;
00231     unsigned long count_lmc_notfound_iters;
00232     unsigned long count_rmc_found;
00233     unsigned long count_rmc_found_iters;
00234     unsigned long count_rmc_notfound;
00235     unsigned long count_rmc_notfound_iters;
00236     /* Control switches and knobs */
00237     unsigned start_write_scope_limit;    /**< Scope on start-write must be <= this value.  3 indicates unlimited */
00238     unsigned short seed[3];         /**< for PRNG */
00239     int running;                    /**< true while should be running */
00240     int debug;                      /**< For controlling debug output */
00241     int syncdebug;                  /**< For controlling debug output from sync */
00242     ccnr_logger logger;             /**< For debug output */
00243     void *loggerdata;               /**< Passed to logger */
00244     int logbreak;                   /**< see ccnr_msg() */
00245     unsigned long logtime;          /**< see ccnr_msg() */
00246     int logpid;                     /**< see ccnr_msg() */
00247     int flood;                      /**< Internal control for auto-reg */
00248     unsigned interest_faceid;       /**< for self_reg internal client */
00249     const char *progname;           /**< our name, for locating helpers */
00250     struct ccn *direct_client;      /**< this talks directly with ccnd */
00251     struct ccn *internal_client;    /**< internal client */
00252     struct fdholder *face0;         /**< special fdholder for internal client */
00253     struct ccn_charbuf *service_ccnb; /**< for local service discovery */
00254     struct ccn_charbuf *neighbor_ccnb; /**< for neighbor service discovery */
00255     struct ccnr_parsed_policy *parsed_policy;  /**< offsets for parsed fields of policy */
00256     struct ccn_charbuf *policy_name;
00257     struct ccn_charbuf *policy_link_cob;
00258     struct ccn_seqwriter *notice;   /**< for notices of status changes */
00259     struct ccn_indexbuf *chface;    /**< faceids w/ recent status changes */
00260     struct ccn_scheduled_event *internal_client_refresh;
00261     struct ccn_scheduled_event *direct_client_refresh;
00262     struct ccn_scheduled_event *notice_push;
00263     /* items related to sync/repo integration */
00264     struct SyncBaseStruct *sync_handle;  /**< handle to pass to the sync code */
00265     ccnr_accession notify_after;  /**< starting item for notifying sync */
00266     ccnr_accession active_enum[CCNR_MAX_ENUM]; /**< active sync enumerations */
00267     
00268     const char *directory;           /**< the repository directory */
00269 };
00270 
00271 struct content_queue {
00272     unsigned burst_nsec;             /**< nsec per KByte, limits burst rate */
00273     unsigned min_usec;               /**< minimum delay for this queue */
00274     unsigned rand_usec;              /**< randomization range */
00275     unsigned ready;                  /**< # that have waited enough */
00276     unsigned nrun;                   /**< # sent since last randomized delay */
00277     struct ccn_indexbuf *send_queue; /**< cookie numbers of pending content */
00278     struct ccn_scheduled_event *sender;
00279 };
00280 
00281 enum cq_delay_class {
00282     CCN_CQ_ASAP,
00283     CCN_CQ_NORMAL,
00284     CCN_CQ_SLOW,
00285     CCN_CQ_N
00286 };
00287 
00288 /**
00289  * fdholder meter index
00290  */
00291 enum ccnr_face_meter_index {
00292     FM_BYTI,
00293     FM_BYTO,
00294     FM_DATI,
00295     FM_INTO,
00296     FM_DATO,
00297     FM_INTI,
00298     CCNR_FACE_METER_N
00299 };
00300 
00301 /**
00302  * Each fdholder is referenced by its file descriptor.
00303  */
00304 struct fdholder {
00305     unsigned filedesc;          /**< file descriptor */
00306     int flags;                  /**< CCNR_FACE_* fdholder flags */
00307     unsigned recvcount;         /**< for activity level monitoring */
00308     struct content_queue *q[CCN_CQ_N]; /**< outgoing content, per delay class */
00309     off_t bufoffset;
00310     struct ccn_charbuf *inbuf;  /** Buffered input data */
00311     struct ccn_skeleton_decoder decoder;
00312     size_t outbufindex;         /** Buffered output data */
00313     struct ccn_charbuf *outbuf;
00314     struct ccn_charbuf *name;   /** a sockaddr or file name, depending on flags */
00315     int pending_interests;
00316     struct ccnr_meter *meter[CCNR_FACE_METER_N];
00317 };
00318 
00319 /** fdholder flags */
00320 
00321 #define CCNR_FACE_DGRAM  (1 << 1) /**< Datagram interface, respect packets */
00322 #define CCNR_FACE_GG     (1 << 2) /**< Considered friendly */
00323 #define CCNR_FACE_LOCAL  (1 << 3) /**< PF_UNIX socket */
00324 #define CCNR_FACE_INET   (1 << 4) /**< IPv4 */
00325 #define CCNR_FACE_INET6  (1 << 6) /**< IPv6 */
00326 #define CCNR_FACE_NOSEND (1 << 8) /**< Don't send anymore */
00327 #define CCNR_FACE_UNDECIDED (1 << 9) /**< Might not be talking ccn */
00328 #define CCNR_FACE_PERMANENT (1 << 10) /**< No timeout for inactivity */
00329 #define CCNR_FACE_CONNECTING (1 << 11) /**< Connect in progress */
00330 #define CCNR_FACE_LOOPBACK (1 << 12) /**< v4 or v6 loopback address */
00331 #define CCNR_FACE_CLOSING (1 << 13) /**< close stream when output is done */
00332 #define CCNR_FACE_PASSIVE (1 << 14) /**< a listener or a bound dgram socket */
00333 #define CCNR_FACE_NORECV (1 << 15) /**< use for sending only */
00334 #define CCNR_FACE_REPODATA (1 << 19) /** A repository log-structured data file */
00335 #define CCNR_FACE_CCND (1 << 20) /** A connection to our ccnd */
00336 #define CCNR_FACE_SOCKMASK (CCNR_FACE_DGRAM | CCNR_FACE_INET | CCNR_FACE_INET6 | CCNR_FACE_LOCAL)
00337 
00338 #define CCN_NOFACEID    (-1)    /** denotes no fdholder */
00339 
00340 /**
00341  *  A pointer to this is used as a handle for a content object that we
00342  *  currently care about.  Most details are private to the implementation.
00343  */
00344 struct content_entry;
00345 
00346 /**
00347  * content_entry flags
00348  */
00349 #define CCN_CONTENT_ENTRY_SLOWSEND  1
00350 #define CCN_CONTENT_ENTRY_STALE     2
00351 #define CCN_CONTENT_ENTRY_PRECIOUS  4
00352 #define CCN_CONTENT_ENTRY_STABLE    8 /**< Repository-backed */
00353 
00354 /**
00355  * The content_by_accession hash table, keyed by accession, holds
00356  * entries that have a known accession.
00357  */
00358 struct content_by_accession_entry {
00359     struct content_entry *content;
00360 };
00361 
00362 /**
00363  * The propagating interest hash table is keyed by Nonce.
00364  *
00365  * While the interest is pending, the pe is also kept in a doubly-linked
00366  * list off of a nameprefix_entry.
00367  *
00368  * When the interest is consumed, the pe is removed from the doubly-linked
00369  * list and is cleaned up by freeing unnecessary bits (including the interest
00370  * message itself).  It remains in the hash table for a time, in order to catch
00371  * duplicate nonces.
00372  */
00373 struct propagating_entry {
00374     struct propagating_entry *next;
00375     struct propagating_entry *prev;
00376     unsigned flags;             /**< CCN_PR_xxx */
00377     unsigned filedesc;            /**< origin of the interest, dest for matches */
00378     int usec;                   /**< usec until timeout */
00379     int sent;                   /**< leading faceids of outbound processed */
00380     struct ccn_indexbuf *outbound; /**< in order of use */
00381     unsigned char *interest_msg; /**< pending interest message */
00382     unsigned size;              /**< size in bytes of interest_msg */
00383     int fgen;                   /**< decide if outbound is stale */
00384 };
00385 // XXX - with new outbound/sent repr, some of these flags may not be needed.
00386 #define CCN_PR_UNSENT   0x01 /**< interest has not been sent anywhere yet */
00387 #define CCN_PR_WAIT1    0x02 /**< interest has been sent to one place */
00388 #define CCN_PR_STUFFED1 0x04 /**< was stuffed before sent anywhere else */
00389 #define CCN_PR_TAP      0x08 /**< at least one tap fdholder is present */
00390 #define CCN_PR_EQV      0x10 /**< a younger similar interest exists */
00391 #define CCN_PR_SCOPE0   0x20 /**< interest scope is 0 */
00392 #define CCN_PR_SCOPE1   0x40 /**< interest scope is 1 (this host) */
00393 #define CCN_PR_SCOPE2   0x80 /**< interest scope is 2 (immediate neighborhood) */
00394 
00395 /**
00396  * The nameprefix hash table is keyed by the Component elements of
00397  * the Name prefix.
00398  */
00399 struct nameprefix_entry {
00400     struct propagating_entry pe_head; /**< list head for propagating entries */
00401     struct ccn_indexbuf *forward_to; /**< faceids to forward to */
00402     struct ccn_indexbuf *tap;    /**< faceids to forward to as tap*/
00403     struct ccn_forwarding *forwarding; /**< detailed forwarding info */
00404     struct nameprefix_entry *parent; /**< link to next-shorter prefix */
00405     int children;                /**< number of children */
00406     unsigned flags;              /**< CCN_FORW_* flags about namespace */
00407     int fgen;                    /**< used to decide when forward_to is stale */
00408     unsigned src;                /**< filedesc of recent content source */
00409     unsigned osrc;               /**< and of older matching content */
00410     unsigned usec;               /**< response-time prediction */
00411 };
00412 
00413 /**
00414  * Keeps track of the faces that interests matching a given name prefix may be
00415  * forwarded to.
00416  */
00417 struct ccn_forwarding {
00418     unsigned filedesc;             /**< locally unique number identifying fdholder */
00419     unsigned flags;              /**< CCN_FORW_* - c.f. <ccn/reg_mgnt.h> */
00420     int expires;                 /**< time remaining, in seconds */
00421     struct ccn_forwarding *next;
00422 };
00423 
00424 /**
00425  * Keeps track of the state of running and recently completed enumerations
00426  * The enum_state hash table is keyed by the interest up to the segment id
00427  */
00428 enum es_active_state {
00429     ES_PENDING = -1,
00430     ES_INACTIVE = 0,
00431     ES_ACTIVE = 1,
00432     ES_ACTIVE_PENDING_INACTIVE = 2
00433 };
00434 #define ENUM_N_COBS 9
00435 struct enum_state {
00436     struct ccn_charbuf *name;
00437     struct content_entry *content;
00438     struct ccn_charbuf *reply_body;
00439     struct ccn_charbuf *interest;
00440     struct ccn_indexbuf *interest_comps;
00441     struct ccn_charbuf *cob[ENUM_N_COBS];
00442     int cob_deferred[ENUM_N_COBS];
00443     intmax_t next_segment;
00444     ccnr_cookie starting_cookie;
00445     enum es_active_state active;
00446     long lifetime;
00447     long lastuse_sec;
00448     unsigned lastuse_usec;
00449 };
00450 
00451 /**
00452  * @def CCN_FORW_ACTIVE         1
00453  * @def CCN_FORW_CHILD_INHERIT  2
00454  * @def CCN_FORW_ADVERTISE      4
00455  * @def CCN_FORW_LAST           8
00456  * @def CCN_FORW_CAPTURE       16
00457  * @def CCN_FORW_LOCAL         32
00458  */
00459 #define CCN_FORW_PFXO (CCN_FORW_ADVERTISE | CCN_FORW_CAPTURE | CCN_FORW_LOCAL)
00460 #define CCN_FORW_REFRESHED      (1 << 16) /**< private to ccnr */
00461 
00462  
00463 /**
00464  * Determines how frequently we age our forwarding entries
00465  */
00466 #define CCN_FWU_SECS 5
00467 
00468 /**
00469  * URIs for prefixes served by the internal client
00470  */
00471 #define CCNRID_LOCAL_URI "ccnx:/%C1.M.S.localhost/%C1.M.SRV/repository/KEY"
00472 #define CCNRID_NEIGHBOR_URI "ccnx:/%C1.M.S.neighborhood/%C1.M.SRV/repository/KEY"
00473 #define CCNRID_POLICY_URI "ccnx:/%C1.M.S.localhost/%C1.M.SRV/repository/POLICY"
00474 
00475 #define PUBLIC
00476 
00477 struct ccnr_handle *r_init_create(const char *, ccnr_logger, void *);
00478 void r_init_run(struct ccnr_handle *h);
00479 void r_init_destroy(struct ccnr_handle **);
00480 
00481 #endif
Generated on Tue Aug 21 14:54:15 2012 for Content-Centric Networking in C by  doxygen 1.6.3