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 start_write_scope_limit;    /**< Scope on start-write must be <= this value.  3 indicates unlimited */
00229     unsigned short seed[3];         /**< for PRNG */
00230     int running;                    /**< true while should be running */
00231     int debug;                      /**< For controlling debug output */
00232     int syncdebug;                  /**< For controlling debug output from sync */
00233     ccnr_logger logger;             /**< For debug output */
00234     void *loggerdata;               /**< Passed to logger */
00235     int logbreak;                   /**< see ccnr_msg() */
00236     unsigned long logtime;          /**< see ccnr_msg() */
00237     int logpid;                     /**< see ccnr_msg() */
00238     int flood;                      /**< Internal control for auto-reg */
00239     unsigned interest_faceid;       /**< for self_reg internal client */
00240     const char *progname;           /**< our name, for locating helpers */
00241     struct ccn *direct_client;      /**< this talks directly with ccnd */
00242     struct ccn *internal_client;    /**< internal client */
00243     struct fdholder *face0;         /**< special fdholder for internal client */
00244     struct ccn_charbuf *service_ccnb; /**< for local service discovery */
00245     struct ccn_charbuf *neighbor_ccnb; /**< for neighbor service discovery */
00246     struct ccnr_parsed_policy *parsed_policy;  /**< offsets for parsed fields of policy */
00247     struct ccn_charbuf *policy_name;
00248     struct ccn_charbuf *policy_link_cob;
00249     struct ccn_seqwriter *notice;   /**< for notices of status changes */
00250     struct ccn_indexbuf *chface;    /**< faceids w/ recent status changes */
00251     struct ccn_scheduled_event *internal_client_refresh;
00252     struct ccn_scheduled_event *direct_client_refresh;
00253     struct ccn_scheduled_event *notice_push;
00254     /* items related to sync/repo integration */
00255     struct SyncBaseStruct *sync_handle;  /**< handle to pass to the sync code */
00256     ccnr_accession notify_after;  /**< starting item for notifying sync */
00257     ccnr_accession active_enum[CCNR_MAX_ENUM]; /**< active sync enumerations */
00258     
00259     const char *directory;           /**< the repository directory */
00260 };
00261 
00262 struct content_queue {
00263     unsigned burst_nsec;             /**< nsec per KByte, limits burst rate */
00264     unsigned min_usec;               /**< minimum delay for this queue */
00265     unsigned rand_usec;              /**< randomization range */
00266     unsigned ready;                  /**< # that have waited enough */
00267     unsigned nrun;                   /**< # sent since last randomized delay */
00268     struct ccn_indexbuf *send_queue; /**< cookie numbers of pending content */
00269     struct ccn_scheduled_event *sender;
00270 };
00271 
00272 enum cq_delay_class {
00273     CCN_CQ_ASAP,
00274     CCN_CQ_NORMAL,
00275     CCN_CQ_SLOW,
00276     CCN_CQ_N
00277 };
00278 
00279 /**
00280  * fdholder meter index
00281  */
00282 enum ccnr_face_meter_index {
00283     FM_BYTI,
00284     FM_BYTO,
00285     FM_DATI,
00286     FM_INTO,
00287     FM_DATO,
00288     FM_INTI,
00289     CCNR_FACE_METER_N
00290 };
00291 
00292 /**
00293  * Each fdholder is referenced by its file descriptor.
00294  */
00295 struct fdholder {
00296     unsigned filedesc;          /**< file descriptor */
00297     int flags;                  /**< CCNR_FACE_* fdholder flags */
00298     unsigned recvcount;         /**< for activity level monitoring */
00299     struct content_queue *q[CCN_CQ_N]; /**< outgoing content, per delay class */
00300     off_t bufoffset;
00301     struct ccn_charbuf *inbuf;  /** Buffered input data */
00302     struct ccn_skeleton_decoder decoder;
00303     size_t outbufindex;         /** Buffered output data */
00304     struct ccn_charbuf *outbuf;
00305     struct ccn_charbuf *name;   /** a sockaddr or file name, depending on flags */
00306     int pending_interests;
00307     struct ccnr_meter *meter[CCNR_FACE_METER_N];
00308 };
00309 
00310 /** fdholder flags */
00311 
00312 #define CCNR_FACE_DGRAM  (1 << 1) /**< Datagram interface, respect packets */
00313 #define CCNR_FACE_GG     (1 << 2) /**< Considered friendly */
00314 #define CCNR_FACE_LOCAL  (1 << 3) /**< PF_UNIX socket */
00315 #define CCNR_FACE_INET   (1 << 4) /**< IPv4 */
00316 #define CCNR_FACE_INET6  (1 << 6) /**< IPv6 */
00317 #define CCNR_FACE_NOSEND (1 << 8) /**< Don't send anymore */
00318 #define CCNR_FACE_UNDECIDED (1 << 9) /**< Might not be talking ccn */
00319 #define CCNR_FACE_PERMANENT (1 << 10) /**< No timeout for inactivity */
00320 #define CCNR_FACE_CONNECTING (1 << 11) /**< Connect in progress */
00321 #define CCNR_FACE_LOOPBACK (1 << 12) /**< v4 or v6 loopback address */
00322 #define CCNR_FACE_CLOSING (1 << 13) /**< close stream when output is done */
00323 #define CCNR_FACE_PASSIVE (1 << 14) /**< a listener or a bound dgram socket */
00324 #define CCNR_FACE_NORECV (1 << 15) /**< use for sending only */
00325 #define CCNR_FACE_REPODATA (1 << 19) /** A repository log-structured data file */
00326 #define CCNR_FACE_CCND (1 << 20) /** A connection to our ccnd */
00327 #define CCNR_FACE_SOCKMASK (CCNR_FACE_DGRAM | CCNR_FACE_INET | CCNR_FACE_INET6 | CCNR_FACE_LOCAL)
00328 
00329 #define CCN_NOFACEID    (-1)    /** denotes no fdholder */
00330 
00331 /**
00332  *  A pointer to this is used as a handle for a content object that we
00333  *  currently care about.  Most details are private to the implementation.
00334  */
00335 struct content_entry;
00336 
00337 /**
00338  * content_entry flags
00339  */
00340 #define CCN_CONTENT_ENTRY_SLOWSEND  1
00341 #define CCN_CONTENT_ENTRY_STALE     2
00342 #define CCN_CONTENT_ENTRY_PRECIOUS  4
00343 #define CCN_CONTENT_ENTRY_STABLE    8 /**< Repository-backed */
00344 
00345 /**
00346  * The content_by_accession hash table, keyed by accession, holds
00347  * entries that have a known accession.
00348  */
00349 struct content_by_accession_entry {
00350     struct content_entry *content;
00351 };
00352 
00353 /**
00354  * The propagating interest hash table is keyed by Nonce.
00355  *
00356  * While the interest is pending, the pe is also kept in a doubly-linked
00357  * list off of a nameprefix_entry.
00358  *
00359  * When the interest is consumed, the pe is removed from the doubly-linked
00360  * list and is cleaned up by freeing unnecessary bits (including the interest
00361  * message itself).  It remains in the hash table for a time, in order to catch
00362  * duplicate nonces.
00363  */
00364 struct propagating_entry {
00365     struct propagating_entry *next;
00366     struct propagating_entry *prev;
00367     unsigned flags;             /**< CCN_PR_xxx */
00368     unsigned filedesc;            /**< origin of the interest, dest for matches */
00369     int usec;                   /**< usec until timeout */
00370     int sent;                   /**< leading faceids of outbound processed */
00371     struct ccn_indexbuf *outbound; /**< in order of use */
00372     unsigned char *interest_msg; /**< pending interest message */
00373     unsigned size;              /**< size in bytes of interest_msg */
00374     int fgen;                   /**< decide if outbound is stale */
00375 };
00376 // XXX - with new outbound/sent repr, some of these flags may not be needed.
00377 #define CCN_PR_UNSENT   0x01 /**< interest has not been sent anywhere yet */
00378 #define CCN_PR_WAIT1    0x02 /**< interest has been sent to one place */
00379 #define CCN_PR_STUFFED1 0x04 /**< was stuffed before sent anywhere else */
00380 #define CCN_PR_TAP      0x08 /**< at least one tap fdholder is present */
00381 #define CCN_PR_EQV      0x10 /**< a younger similar interest exists */
00382 #define CCN_PR_SCOPE0   0x20 /**< interest scope is 0 */
00383 #define CCN_PR_SCOPE1   0x40 /**< interest scope is 1 (this host) */
00384 #define CCN_PR_SCOPE2   0x80 /**< interest scope is 2 (immediate neighborhood) */
00385 
00386 /**
00387  * The nameprefix hash table is keyed by the Component elements of
00388  * the Name prefix.
00389  */
00390 struct nameprefix_entry {
00391     struct propagating_entry pe_head; /**< list head for propagating entries */
00392     struct ccn_indexbuf *forward_to; /**< faceids to forward to */
00393     struct ccn_indexbuf *tap;    /**< faceids to forward to as tap*/
00394     struct ccn_forwarding *forwarding; /**< detailed forwarding info */
00395     struct nameprefix_entry *parent; /**< link to next-shorter prefix */
00396     int children;                /**< number of children */
00397     unsigned flags;              /**< CCN_FORW_* flags about namespace */
00398     int fgen;                    /**< used to decide when forward_to is stale */
00399     unsigned src;                /**< filedesc of recent content source */
00400     unsigned osrc;               /**< and of older matching content */
00401     unsigned usec;               /**< response-time prediction */
00402 };
00403 
00404 /**
00405  * Keeps track of the faces that interests matching a given name prefix may be
00406  * forwarded to.
00407  */
00408 struct ccn_forwarding {
00409     unsigned filedesc;             /**< locally unique number identifying fdholder */
00410     unsigned flags;              /**< CCN_FORW_* - c.f. <ccn/reg_mgnt.h> */
00411     int expires;                 /**< time remaining, in seconds */
00412     struct ccn_forwarding *next;
00413 };
00414 
00415 /**
00416  * Keeps track of the state of running and recently completed enumerations
00417  * The enum_state hash table is keyed by the interest up to the segment id
00418  */
00419 enum es_active_state {
00420     ES_PENDING = -1,
00421     ES_INACTIVE = 0,
00422     ES_ACTIVE = 1
00423 };
00424 #define ENUM_N_COBS 9
00425 struct enum_state {
00426     struct ccn_charbuf *name;
00427     struct content_entry *content;
00428     struct ccn_charbuf *reply_body;
00429     struct ccn_charbuf *interest;
00430     struct ccn_indexbuf *interest_comps;
00431     struct ccn_charbuf *cob[ENUM_N_COBS];
00432     intmax_t next_segment;
00433     ccnr_cookie starting_cookie;
00434     enum es_active_state active;
00435     long lifetime;
00436     long lastuse_sec;
00437     unsigned lastuse_usec;
00438 };
00439 
00440 /**
00441  * @def CCN_FORW_ACTIVE         1
00442  * @def CCN_FORW_CHILD_INHERIT  2
00443  * @def CCN_FORW_ADVERTISE      4
00444  * @def CCN_FORW_LAST           8
00445  * @def CCN_FORW_CAPTURE       16
00446  * @def CCN_FORW_LOCAL         32
00447  */
00448 #define CCN_FORW_PFXO (CCN_FORW_ADVERTISE | CCN_FORW_CAPTURE | CCN_FORW_LOCAL)
00449 #define CCN_FORW_REFRESHED      (1 << 16) /**< private to ccnr */
00450 
00451  
00452 /**
00453  * Determines how frequently we age our forwarding entries
00454  */
00455 #define CCN_FWU_SECS 5
00456 
00457 /**
00458  * URIs for prefixes served by the internal client
00459  */
00460 #define CCNRID_LOCAL_URI "ccnx:/%C1.M.S.localhost/%C1.M.SRV/repository/KEY"
00461 #define CCNRID_NEIGHBOR_URI "ccnx:/%C1.M.S.neighborhood/%C1.M.SRV/repository/KEY"
00462 #define CCNRID_POLICY_URI "ccnx:/%C1.M.S.localhost/%C1.M.SRV/repository/POLICY"
00463 
00464 #define PUBLIC
00465 
00466 struct ccnr_handle *r_init_create(const char *, ccnr_logger, void *);
00467 void r_init_run(struct ccnr_handle *h);
00468 void r_init_destroy(struct ccnr_handle **);
00469 
00470 #endif

Generated on Thu Feb 16 00:43:58 2012 for Content-Centric Networking in C by  doxygen 1.5.6