src/dpdk/lib/librte_lpm/rte_lpm6.c
static inline int
add_step(struct rte_lpm6 *lpm, struct rte_lpm6_tbl_entry *tbl,
		uint32_t tbl_ind, struct rte_lpm6_tbl_entry **next_tbl,
		uint32_t *next_tbl_ind, uint8_t *ip, uint8_t bytes,
		uint8_t first_byte, uint8_t depth, uint32_t next_hop,
		uint8_t is_new_rule)
{
	uint32_t entry_ind, tbl_range, tbl8_group_start, tbl8_group_end, i;
	uint32_t tbl8_gindex;
	uint8_t bits_covered;
	int ret;

	/*
	 * Calculate index to the table based on the number and position
	 * of the bytes being inspected in this step.
	 */
	entry_ind = get_bitshift(ip, first_byte, bytes);

	/* Number of bits covered in this step */
	bits_covered = (uint8_t)((bytes+first_byte-1)*BYTE_SIZE);

	/*
	 * If depth if smaller than this number (ie this is the last step)
	 * expand the rule across the relevant positions in the table.
	 */
	if (depth <= bits_covered) {
		tbl_range = 1 << (bits_covered - depth);

		for (i = entry_ind; i < (entry_ind + tbl_range); i++) {
			if (!tbl[i].valid || (tbl[i].ext_entry == 0 &&
					tbl[i].depth <= depth)) {

				struct rte_lpm6_tbl_entry new_tbl_entry = {
					.next_hop = next_hop,
					.depth = depth,
					.valid = VALID,
					.valid_group = VALID,
					.ext_entry = 0,
				};

				tbl[i] = new_tbl_entry;

			} else if (tbl[i].ext_entry == 1) {

				/*
				 * If tbl entry is valid and extended calculate the index
				 * into next tbl8 and expand the rule across the data structure.
				 */
				tbl8_gindex = tbl[i].lpm6_tbl8_gindex *
						RTE_LPM6_TBL8_GROUP_NUM_ENTRIES;
				expand_rule(lpm, tbl8_gindex, depth, depth,
						next_hop, VALID);
			}
		}

		/* update tbl8 rule reference counter */
		if (tbl_ind != TBL24_IND && is_new_rule)
			lpm->tbl8_hdrs[tbl_ind].ref_cnt++;

		return 0;
	}
	/*
	 * If this is not the last step just fill one position
	 * and calculate the index to the next table.
	 */
	else {
		/* If it's invalid a new tbl8 is needed */
		if (!tbl[entry_ind].valid) {
			/* get a new table */
			ret = tbl8_get(lpm, &tbl8_gindex);
			if (ret != 0)
				return -ENOSPC;

			/* invalidate all new tbl8 entries */
			tbl8_group_start = tbl8_gindex *
					RTE_LPM6_TBL8_GROUP_NUM_ENTRIES;
			memset(&lpm->tbl8[tbl8_group_start], 0,
					RTE_LPM6_TBL8_GROUP_NUM_ENTRIES *
					sizeof(struct rte_lpm6_tbl_entry));

			/* init the new table's header:
			 *   save the reference to the owner table
			 */
			init_tbl8_header(lpm, tbl8_gindex, tbl_ind, entry_ind);

			/* reference to a new tbl8 */
			struct rte_lpm6_tbl_entry new_tbl_entry = {
				.lpm6_tbl8_gindex = tbl8_gindex,
				.depth = 0,
				.valid = VALID,
				.valid_group = VALID,
				.ext_entry = 1,
			};

			tbl[entry_ind] = new_tbl_entry;

			/* update the current table's reference counter */
			if (tbl_ind != TBL24_IND)
				lpm->tbl8_hdrs[tbl_ind].ref_cnt++;
		}
		/*
		 * If it's valid but not extended the rule that was stored
		 * here needs to be moved to the next table.
		 */
		else if (tbl[entry_ind].ext_entry == 0) {
			/* get a new tbl8 index */
			ret = tbl8_get(lpm, &tbl8_gindex);
			if (ret != 0)
				return -ENOSPC;

			tbl8_group_start = tbl8_gindex *
					RTE_LPM6_TBL8_GROUP_NUM_ENTRIES;
			tbl8_group_end = tbl8_group_start +
					RTE_LPM6_TBL8_GROUP_NUM_ENTRIES;

			struct rte_lpm6_tbl_entry tbl_entry = {
				.next_hop = tbl[entry_ind].next_hop,
				.depth = tbl[entry_ind].depth,
				.valid = VALID,
				.valid_group = VALID,
				.ext_entry = 0
			};

			/* Populate new tbl8 with tbl value. */
			for (i = tbl8_group_start; i < tbl8_group_end; i++)
				lpm->tbl8[i] = tbl_entry;

			/* init the new table's header:
			 *   save the reference to the owner table
			 */
			init_tbl8_header(lpm, tbl8_gindex, tbl_ind, entry_ind);

			/*
			 * Update tbl entry to point to new tbl8 entry. Note: The
			 * ext_flag and tbl8_index need to be updated simultaneously,
			 * so assign whole structure in one go.
			 */
			struct rte_lpm6_tbl_entry new_tbl_entry = {
				.lpm6_tbl8_gindex = tbl8_gindex,
				.depth = 0,
				.valid = VALID,
				.valid_group = VALID,
				.ext_entry = 1,
			};

			tbl[entry_ind] = new_tbl_entry;

			/* update the current table's reference counter */
			if (tbl_ind != TBL24_IND)
				lpm->tbl8_hdrs[tbl_ind].ref_cnt++;
		}

		*next_tbl_ind = tbl[entry_ind].lpm6_tbl8_gindex;
		*next_tbl = &(lpm->tbl8[*next_tbl_ind *
				  RTE_LPM6_TBL8_GROUP_NUM_ENTRIES]);
	}

	return 1;
}
/*
 * Partially adds a new route to the data structure (tbl24+tbl8s).
 * It returns 0 on success, a negative number on failure, or 1 if
 * the process needs to be continued by calling the function again.
 */
Partially adds a new route to the data structure (tbl24+tbl8s).
It returns 0 on success, a negative number on failure, or 1 if
the process needs to be continued by calling the function again.
Partially adds a new route to the data structure ( tbl24 + tbl8s ) . It returns 0 on success a negative number on failure or 1 if the process needs to be continued by calling the function again .
static inline int
add_step(struct rte_lpm6 *lpm, struct rte_lpm6_tbl_entry *tbl,
		uint32_t tbl_ind, struct rte_lpm6_tbl_entry **next_tbl,
		uint32_t *next_tbl_ind, uint8_t *ip, uint8_t bytes,
		uint8_t first_byte, uint8_t depth, uint32_t next_hop,
		uint8_t is_new_rule)
{
	uint32_t entry_ind, tbl_range, tbl8_group_start, tbl8_group_end, i;
	uint32_t tbl8_gindex;
	uint8_t bits_covered;
	int ret;
	entry_ind = get_bitshift(ip, first_byte, bytes);
	bits_covered = (uint8_t)((bytes+first_byte-1)*BYTE_SIZE);
	if (depth <= bits_covered) {
		tbl_range = 1 << (bits_covered - depth);
		for (i = entry_ind; i < (entry_ind + tbl_range); i++) {
			if (!tbl[i].valid || (tbl[i].ext_entry == 0 &&
					tbl[i].depth <= depth)) {
				struct rte_lpm6_tbl_entry new_tbl_entry = {
					.next_hop = next_hop,
					.depth = depth,
					.valid = VALID,
					.valid_group = VALID,
					.ext_entry = 0,
				};
				tbl[i] = new_tbl_entry;
			} else if (tbl[i].ext_entry == 1) {
				tbl8_gindex = tbl[i].lpm6_tbl8_gindex *
						RTE_LPM6_TBL8_GROUP_NUM_ENTRIES;
				expand_rule(lpm, tbl8_gindex, depth, depth,
						next_hop, VALID);
			}
		}
		if (tbl_ind != TBL24_IND && is_new_rule)
			lpm->tbl8_hdrs[tbl_ind].ref_cnt++;
		return 0;
	}
	else {
		if (!tbl[entry_ind].valid) {
			ret = tbl8_get(lpm, &tbl8_gindex);
			if (ret != 0)
				return -ENOSPC;
			tbl8_group_start = tbl8_gindex *
					RTE_LPM6_TBL8_GROUP_NUM_ENTRIES;
			memset(&lpm->tbl8[tbl8_group_start], 0,
					RTE_LPM6_TBL8_GROUP_NUM_ENTRIES *
					sizeof(struct rte_lpm6_tbl_entry));
			init_tbl8_header(lpm, tbl8_gindex, tbl_ind, entry_ind);
			struct rte_lpm6_tbl_entry new_tbl_entry = {
				.lpm6_tbl8_gindex = tbl8_gindex,
				.depth = 0,
				.valid = VALID,
				.valid_group = VALID,
				.ext_entry = 1,
			};
			tbl[entry_ind] = new_tbl_entry;
			if (tbl_ind != TBL24_IND)
				lpm->tbl8_hdrs[tbl_ind].ref_cnt++;
		}
		else if (tbl[entry_ind].ext_entry == 0) {
			ret = tbl8_get(lpm, &tbl8_gindex);
			if (ret != 0)
				return -ENOSPC;
			tbl8_group_start = tbl8_gindex *
					RTE_LPM6_TBL8_GROUP_NUM_ENTRIES;
			tbl8_group_end = tbl8_group_start +
					RTE_LPM6_TBL8_GROUP_NUM_ENTRIES;
			struct rte_lpm6_tbl_entry tbl_entry = {
				.next_hop = tbl[entry_ind].next_hop,
				.depth = tbl[entry_ind].depth,
				.valid = VALID,
				.valid_group = VALID,
				.ext_entry = 0
			};
			for (i = tbl8_group_start; i < tbl8_group_end; i++)
				lpm->tbl8[i] = tbl_entry;
			init_tbl8_header(lpm, tbl8_gindex, tbl_ind, entry_ind);
			struct rte_lpm6_tbl_entry new_tbl_entry = {
				.lpm6_tbl8_gindex = tbl8_gindex,
				.depth = 0,
				.valid = VALID,
				.valid_group = VALID,
				.ext_entry = 1,
			};
			tbl[entry_ind] = new_tbl_entry;
			if (tbl_ind != TBL24_IND)
				lpm->tbl8_hdrs[tbl_ind].ref_cnt++;
		}
		*next_tbl_ind = tbl[entry_ind].lpm6_tbl8_gindex;
		*next_tbl = &(lpm->tbl8[*next_tbl_ind *
				  RTE_LPM6_TBL8_GROUP_NUM_ENTRIES]);
	}
	return 1;
}
static inline int add_step ( struct rte_lpm6 * lpm , struct rte_lpm6_tbl_entry * tbl , uint32_t tbl_ind , struct rte_lpm6_tbl_entry * * next_tbl , uint32_t * next_tbl_ind , uint8_t * ip , uint8_t bytes , uint8_t first_byte , uint8_t depth , uint32_t next_hop , uint8_t is_new_rule ) { uint32_t entry_ind , tbl_range , tbl8_group_start , tbl8_group_end , i ; uint32_t tbl8_gindex ; uint8_t bits_covered ; int ret ; entry_ind = get_bitshift ( ip , first_byte , bytes ) ; bits_covered = ( uint8_t ) ( ( bytes + first_byte - 1 ) * BYTE_SIZE ) ; if ( depth <= bits_covered ) { tbl_range = 1 << ( bits_covered - depth ) ; for ( i = entry_ind ; i < ( entry_ind + tbl_range ) ; i ++ ) { if ( ! tbl [ i ] . valid || ( tbl [ i ] . ext_entry == 0 && tbl [ i ] . depth <= depth ) ) { struct rte_lpm6_tbl_entry new_tbl_entry = { . next_hop = next_hop , . depth = depth , . valid = VALID , . valid_group = VALID , . ext_entry = 0 , } ; tbl [ i ] = new_tbl_entry ; } else if ( tbl [ i ] . ext_entry == 1 ) { tbl8_gindex = tbl [ i ] . lpm6_tbl8_gindex * RTE_LPM6_TBL8_GROUP_NUM_ENTRIES ; expand_rule ( lpm , tbl8_gindex , depth , depth , next_hop , VALID ) ; } } if ( tbl_ind != TBL24_IND && is_new_rule ) lpm -> tbl8_hdrs [ tbl_ind ] . ref_cnt ++ ; return 0 ; } else { if ( ! tbl [ entry_ind ] . valid ) { ret = tbl8_get ( lpm , & tbl8_gindex ) ; if ( ret != 0 ) return - ENOSPC ; tbl8_group_start = tbl8_gindex * RTE_LPM6_TBL8_GROUP_NUM_ENTRIES ; memset ( & lpm -> tbl8 [ tbl8_group_start ] , 0 , RTE_LPM6_TBL8_GROUP_NUM_ENTRIES * sizeof ( struct rte_lpm6_tbl_entry ) ) ; init_tbl8_header ( lpm , tbl8_gindex , tbl_ind , entry_ind ) ; struct rte_lpm6_tbl_entry new_tbl_entry = { . lpm6_tbl8_gindex = tbl8_gindex , . depth = 0 , . valid = VALID , . valid_group = VALID , . ext_entry = 1 , } ; tbl [ entry_ind ] = new_tbl_entry ; if ( tbl_ind != TBL24_IND ) lpm -> tbl8_hdrs [ tbl_ind ] . ref_cnt ++ ; } else if ( tbl [ entry_ind ] . ext_entry == 0 ) { ret = tbl8_get ( lpm , & tbl8_gindex ) ; if ( ret != 0 ) return - ENOSPC ; tbl8_group_start = tbl8_gindex * RTE_LPM6_TBL8_GROUP_NUM_ENTRIES ; tbl8_group_end = tbl8_group_start + RTE_LPM6_TBL8_GROUP_NUM_ENTRIES ; struct rte_lpm6_tbl_entry tbl_entry = { . next_hop = tbl [ entry_ind ] . next_hop , . depth = tbl [ entry_ind ] . depth , . valid = VALID , . valid_group = VALID , . ext_entry = 0 } ; for ( i = tbl8_group_start ; i < tbl8_group_end ; i ++ ) lpm -> tbl8 [ i ] = tbl_entry ; init_tbl8_header ( lpm , tbl8_gindex , tbl_ind , entry_ind ) ; struct rte_lpm6_tbl_entry new_tbl_entry = { . lpm6_tbl8_gindex = tbl8_gindex , . depth = 0 , . valid = VALID , . valid_group = VALID , . ext_entry = 1 , } ; tbl [ entry_ind ] = new_tbl_entry ; if ( tbl_ind != TBL24_IND ) lpm -> tbl8_hdrs [ tbl_ind ] . ref_cnt ++ ; } * next_tbl_ind = tbl [ entry_ind ] . lpm6_tbl8_gindex ; * next_tbl = & ( lpm -> tbl8 [ * next_tbl_ind * RTE_LPM6_TBL8_GROUP_NUM_ENTRIES ] ) ; } return 1 ; }
Partially adds a new route to the data structure (tbl24+tbl8s).
Partially adds a new route to the data structure ( tbl24 + tbl8s ) .
/*
	 * Calculate index to the table based on the number and position
	 * of the bytes being inspected in this step.
	 */ /* Number of bits covered in this step */ /*
	 * If depth if smaller than this number (ie this is the last step)
	 * expand the rule across the relevant positions in the table.
	 */ /*
				 * If tbl entry is valid and extended calculate the index
				 * into next tbl8 and expand the rule across the data structure.
				 */ /* update tbl8 rule reference counter */ /*
	 * If this is not the last step just fill one position
	 * and calculate the index to the next table.
	 */ /* If it's invalid a new tbl8 is needed */ /* get a new table */ /* invalidate all new tbl8 entries */ /* init the new table's header:
			 *   save the reference to the owner table
			 */ /* reference to a new tbl8 */ /* update the current table's reference counter */ /*
		 * If it's valid but not extended the rule that was stored
		 * here needs to be moved to the next table.
		 */ /* get a new tbl8 index */ /* Populate new tbl8 with tbl value. */ /* init the new table's header:
			 *   save the reference to the owner table
			 */ /*
			 * Update tbl entry to point to new tbl8 entry. Note: The
			 * ext_flag and tbl8_index need to be updated simultaneously,
			 * so assign whole structure in one go.
			 */ /* update the current table's reference counter */