In gated, AS paths (actually a struct containing all transitive attributes) are stored in a hash table and accessed by a hash combination of the length of the path data, the origin attribute and other fields. If two routes have the same transitive attributes, gated stores only one copy of the attributes in the hash tables. The functions in this module are concerned with manipulating the hash table and parsing the contents of BGP messages with attributes.

AS Path module type declarations

type as_path

This structure, misnamed because it stores the origin attribute and unrecognized attributes as well as the AS path information. This block is really the fixed header part of a variable-sized block. This structure contains the AS numbers of the peer as well as the lengths of the path attribute and other attributes.

type as_local

Each instance of gated is allowed to run in upto 16 ASs. Gated maintains an array of the as_local structs. Each such struct contains pertinent about each local AS that the router is running in.

type as_path_data

This structure is merely a template for a variable sized as_path block. That is, routines that manipulate as_path structs that have path attributes would cast that struct into a as_path_data struct.

type as_path_info

When a BGP update message is parsed for attributes, the transitive attributes are stored in the as_path structure. The non-transitive attributes are returned (and passed around) in this structure.

type as_path_list

When aggregating routes to destinations, the different AS paths that should be aggregated together are kept in a list. This structure forms an element of that list.

AS Path module macros

macro PATH_GET_*

Retrieve different attributes (metric, nexthop) from a byte stream containing attributes.

macro PATH_PUT_*

Put different attributes (metric, nexthop) to a byte stream containing attributes.

macro AS_PATH_LIST

Traverse the entire hash table, visiting each hash bucket sequentially, and, within each bucket, visiting the overflow chain of elements. This is typically used at the beginning of a loop.

macro AS_PATH_LIST_END

The corresponding end of loop traversing each element in the hash table.

macro AS_LOCAL_FINDBIT

Local ASs are stored in the as_local_info array. Given a local AS, we find the position of that AS in this array and convert that position into a 16-bit number with the bit corresponding to that position set.

macro PATH_FREE_PATH

The as_path structs are allocated in 32 byte and 64 byte blocks for the common cases when the structure fits entirely into one of these blocks. Otherwise, it is malloc'd. path_size_list is an array, the first element of which contains the number of 32 byte blocks allocated and the second the number of 64 byte blocks allocated. This macro frees the storage associated with an as_path.

macro PATH_FIND_PATH

Traverse an overflow chain on a given hash bucket to find the as_path struct matching the given as_path.

macro PATH_ADD

Add an as_path struct to the beginning of an overflow chain on the hash bucket.

macro PATH_REMOVE

Remove the specified as_path entry from the overflow chain. To do this, scan the chain to find the previous element and then unlink this element.

AS Path module function definitions

function aslocal_set

The array as_local_infostores all the local ASs sorted by AS number. Insert a new AS into this array. Called at configuration time.

function aslocal_bit

Find the bit pattern corresponding to the position of the given AS number in the as_local_info array.

function aslocal_cleanup

Called when reconfiguring. Saves the array of local AS numbers in a secondary array. This is used later in aslocal_reinit.

function aslocal_reinit

Called after the configuration file has been re-read. If the new configuration continues to contain some of the old ASs, then we need to revisit the AS paths we already have and adjust the bitmask identifying the local AS.

function aspath_family_init

Allocate a task to dump AS path information. Set up its handlers.

function aspath_alloc

Allocate an as_path struct of the required length. If it fits into the common-case lengths, allocate a block out of one of those. Otherwise, malloc some memory of the requested length.

function aspath_free

De-allocate an allocated as_path structure, irrespective of whether it is in the hash table or not.

function aspath_unlink

De-allocate an allocated as_path structure, assuming it is in the hash table or not.

function aspath_find

If an existing as_path struct matches the one we are given, return that instead. Otherwise, insert the one we are given and increment the hop count.

function aspath_insert_aspath

Called from aspath_attr when parsing an incoming attribute list to store into an as_path structure. This moves the already parsed optional attributes to the end of the as_path structure creating room for the AS path itself.

function aspath_insert

Given an attribute, we find or create place in the as_path structure at which to insert the attribute. Attributes are stored sorted by code, so we insert the new attribute after moving south any existing attributes.

function aspath_attr

One of the core functions of the AS path module, this parses path attributes and stores them in an appropriately sized as_path struct. Basically, it allocates an as_path struct of the same size as the buffer we are handed (we could do a better job). Then we basically look at each attribute (making sure there are no duplicates and that there is enough data for the attribute). Most of the attributes are processed in a straightforward manner: transitive attributes are stored in the as_path structure, and non-transitive attributes are stored in an as_path_info structure. The only complicated processing is performed for the AS path.

The AS path attribute, in the incoming message, is a list of type/length/list of ASs triples. In the as_path struct, this is stored as a list of all ASs followed by type/length tuples. During the processing, successive triples which are AS sequences are collapsed into a single AS sequences. AS sets are kept the same but are ordered for ease of later processing.

function aspath_format_v4

The converse of the aspath_attr function. This is similar to the above in that the most complicated portion is the formatting of the AS path attribute. Prior to walking down the list of ASs, we first find out if we have to prepend our own AS number. If so, we do that to every route and then appropriately create the type/length/value triplets that we so carefully undid in the last message.

function aspath_do_aggregation

This function is called from rt_aggregate_flash(). Given a list of AS paths, we are required to find an AS path that represents the aggregate. The idea behind the algorithm may be found in Appendix A of RFC 1654.

function aspath_aggregate_changed

The list of AS paths contributing an aggregate has changed. In particular, a old AS path has gone away and a new one has come in. Scan the list of AS paths and remove the old one, re-insert the new one.

function aspath_aggregate_free

While computing aggregate AS paths, the AS paths contributing to an aggregate are stored in a linked list of as_path_list structs (each such struct points to and AS path). This routine frees the storage associated with the as_path_list structs.

function aspath_prefer

Find out which of the two AS paths is more preferable. Select the one with either the numerically lower origin AS number, failing which the one with the smaller path length.