void sort_list(void *ll, void *cmp) { tommy_list_sort (ll, cmp); }
switch_status_t switch_api_packet_net_filter_tx_create( switch_device_t device, switch_packet_tx_key_t *tx_key, switch_packet_tx_action_t *tx_action) { switch_packet_tx_entry_t tx_entry; switch_packet_tx_info_t *tx_info = NULL; switch_hostif_info_t *hostif_info = NULL; switch_handle_t bd_handle = 0; switch_interface_info_t *intf_info = NULL; switch_handle_type_t handle_type = 0; switch_bd_info_t *bd_info = NULL; switch_status_t status = SWITCH_STATUS_SUCCESS; if (!tx_key || !tx_action) { SWITCH_API_ERROR("filter tx create failed. invalid params"); return SWITCH_STATUS_INVALID_PARAMETER; } if (tx_key->handle_valid) { hostif_info = switch_hostif_get(tx_key->hostif_handle); if (!hostif_info) { SWITCH_API_ERROR("invalid hostif handle"); return SWITCH_STATUS_INVALID_HANDLE; } } memset(&tx_entry, 0x0, sizeof(tx_entry)); if (tx_key->handle_valid) { tx_entry.intf_fd = hostif_info->intf_fd; } tx_entry.fd_valid = tx_key->handle_valid; tx_entry.vlan_id = tx_key->vlan_id; tx_entry.vlan_valid = tx_key->vlan_valid; tx_entry.priority = tx_key->priority; tx_info = switch_malloc(sizeof(switch_packet_tx_info_t), 0x1); if (!tx_info) { SWITCH_API_ERROR("hif %lx vlan %x malloc failure", tx_key->hostif_handle, tx_key->vlan_id); return SWITCH_STATUS_NO_MEMORY; } if (tx_action->bypass_flags != SWITCH_BYPASS_ALL) { bd_handle = tx_action->handle; handle_type = switch_handle_get_type(tx_action->handle); if (handle_type == SWITCH_HANDLE_TYPE_INTERFACE) { intf_info = switch_api_interface_get(tx_action->handle); if (!intf_info) { SWITCH_API_ERROR("intf_handle %lx is invalid", tx_action->handle); return SWITCH_STATUS_INVALID_HANDLE; } if (!SWITCH_INTF_IS_PORT_L3(intf_info)) { SWITCH_API_ERROR("intf_handle %lx is not l3", tx_action->handle); return SWITCH_STATUS_INVALID_HANDLE; } bd_handle = intf_info->bd_handle; } bd_info = switch_bd_get(bd_handle); if (!bd_info) { SWITCH_API_ERROR( "hif %lx vlan %x invalid bd", tx_key->hostif_handle, tx_key->vlan_id); return SWITCH_STATUS_INVALID_HANDLE; } } memcpy(&tx_info->tx_entry, &tx_entry, sizeof(tx_entry)); tx_info->bd = handle_to_id(bd_handle); tx_info->bypass_flags = tx_action->bypass_flags; tx_info->port = handle_to_id(tx_action->port_handle); SWITCH_API_INFO( "net_filter_tx_create: hostif 0x%lx, vlan_id = %d, fd 0x%x, bypass " "0x%x\n", tx_key->hostif_handle, tx_key->vlan_valid ? tx_key->vlan_id : 0xFFF, tx_entry.intf_fd, tx_info->bypass_flags); tommy_list_insert_head(&packet_tx_filter_list, &(tx_info->node), tx_info); tommy_list_sort(&packet_tx_filter_list, switch_packet_tx_filter_priority_compare); return status; }
switch_status_t switch_api_packet_net_filter_rx_create( switch_device_t device, switch_packet_rx_key_t *rx_key, switch_packet_rx_action_t *rx_action) { switch_hostif_info_t *hostif_info = NULL; switch_lag_info_t *lag_info = NULL; switch_port_info_t *port_info = NULL; switch_packet_rx_entry_t rx_entry; switch_packet_rx_info_t *rx_info = NULL; switch_handle_type_t handle_type = 0; switch_interface_info_t *intf_info = NULL; switch_handle_t bd_handle = 0; switch_bd_info_t *bd_info = NULL; switch_ifindex_t ifindex = 0; switch_status_t status = SWITCH_STATUS_SUCCESS; if (!rx_key || !rx_action) { SWITCH_API_ERROR("filter rx create failed. invalid params"); return SWITCH_STATUS_INVALID_PARAMETER; } memset(&rx_entry, 0x0, sizeof(rx_entry)); if (rx_key->port_lag_valid) { handle_type = switch_handle_get_type(rx_key->port_lag_handle); if (handle_type == SWITCH_HANDLE_TYPE_LAG) { lag_info = switch_api_lag_get_internal(rx_key->port_lag_handle); if (!lag_info) { SWITCH_API_ERROR("invalid lag handle"); return SWITCH_STATUS_INVALID_HANDLE; } ifindex = lag_info->ifindex; } else { port_info = switch_api_port_get_internal(rx_key->port_lag_handle); if (!port_info) { SWITCH_API_ERROR("invalid port handle"); return SWITCH_STATUS_INVALID_HANDLE; } ifindex = port_info->ifindex; } rx_entry.ifindex_valid = TRUE; } if (rx_key->handle_valid) { bd_handle = rx_key->handle; handle_type = switch_handle_get_type(rx_key->handle); if (handle_type == SWITCH_HANDLE_TYPE_INTERFACE) { intf_info = switch_api_interface_get(rx_key->handle); if (!intf_info) { SWITCH_API_ERROR("intf_handle %lx is invalid", rx_key->handle); return SWITCH_STATUS_INVALID_HANDLE; } if (!SWITCH_INTF_IS_PORT_L3(intf_info)) { SWITCH_API_ERROR("intf_handle %lx is not l3", rx_key->handle); return SWITCH_STATUS_INVALID_HANDLE; } bd_handle = intf_info->bd_handle; } bd_info = switch_bd_get(bd_handle); if (!bd_info) { SWITCH_API_ERROR("bd derivation failed %lx", rx_key->handle); return SWITCH_STATUS_INVALID_HANDLE; } rx_entry.bd_valid = TRUE; } if (rx_action->hostif_handle) { hostif_info = switch_hostif_get(rx_action->hostif_handle); if (!hostif_info) { SWITCH_API_ERROR("invalid hostif handle"); return SWITCH_STATUS_INVALID_HANDLE; } } rx_entry.bd = handle_to_id(bd_handle); rx_entry.ifindex = ifindex; rx_entry.port_valid = rx_key->port_valid; rx_entry.port = handle_to_id(rx_key->port_handle); rx_entry.reason_code_valid = rx_key->reason_code_valid; rx_entry.reason_code = rx_key->reason_code; rx_entry.reason_code_mask = rx_key->reason_code_mask; rx_entry.priority = rx_key->priority; rx_info = switch_malloc(sizeof(switch_packet_rx_info_t), 0x1); if (!rx_info) { SWITCH_API_ERROR("port %lx port_lag %lx handle %lx malloc failed", rx_key->port_handle, rx_key->port_lag_handle, rx_key->handle); return SWITCH_STATUS_NO_MEMORY; } memset(rx_info, 0x0, sizeof(switch_packet_rx_info_t)); memcpy(&rx_info->rx_entry, &rx_entry, sizeof(rx_entry)); rx_info->vlan_id = rx_action->vlan_id; rx_info->vlan_action = rx_action->vlan_action; if (hostif_info) { rx_info->intf_fd = hostif_info->intf_fd; } SWITCH_API_INFO( "net_filter_rx_create: port 0x%lx, port_lag_hdl = 0x%lx, " "if_bd_hdl 0x%lx, rcode 0x%x, rcode_mask 0x%x " "vlan_id %d, fd %d, action %d\n", rx_key->port_valid ? rx_key->port_handle : 0, rx_key->port_lag_valid ? rx_key->port_lag_handle : 0, rx_key->handle_valid ? rx_key->handle : 0, rx_key->reason_code_valid ? rx_key->reason_code : 0, rx_key->reason_code_mask, rx_info->vlan_id, rx_info->vlan_action, rx_info->intf_fd); /* * Adding an element to the list results in sorting the list. * tommy does not have a way to compare and insert the elements */ tommy_list_insert_head(&packet_rx_filter_list, &(rx_info->node), rx_info); tommy_list_sort(&packet_rx_filter_list, switch_packet_rx_filter_priority_compare); return status; }
void test_list(void) { struct object* LIST; struct object_vector* VECTOR; tommy_node* list; unsigned i; LIST = malloc(MAX * sizeof(struct object)); VECTOR = malloc(MAX * sizeof(struct object_vector)); for(i=0;i<MAX;++i) { VECTOR[i].value = LIST[i].value = 0; } list = 0; for(i=0;i<MAX;++i) { VECTOR[i].value = LIST[i].value = rnd(MAX); tommy_list_insert_tail(&list, &LIST[i].node, &LIST[i]); } START("sort random"); tommy_list_sort(&list, compare); STOP(); START("C qsort random"); qsort(VECTOR, MAX, sizeof(VECTOR[0]), compare_vector); STOP(); test_list_order(list); /* forward order with some (1%) random values */ list = 0; for(i=0;i<MAX;++i) { VECTOR[i].value = LIST[i].value = i; if (rnd(100) == 0) VECTOR[i].value = LIST[i].value = rnd(MAX); tommy_list_insert_tail(&list, &LIST[i].node, &LIST[i]); } START("sort partially ordered"); tommy_list_sort(&list, compare); STOP(); START("C qsort partially ordered"); qsort(VECTOR, MAX, sizeof(VECTOR[0]), compare_vector); STOP(); test_list_order(list); /* forward order */ list = 0; for(i=0;i<MAX;++i) { VECTOR[i].value = LIST[i].value = i; tommy_list_insert_tail(&list, &LIST[i].node, &LIST[i]); } START("sort forward"); tommy_list_sort(&list, compare); STOP(); START("C qsort forward"); qsort(VECTOR, MAX, sizeof(VECTOR[0]), compare_vector); STOP(); test_list_order(list); /* backward order */ list = 0; for(i=0;i<MAX;++i) { VECTOR[i].value = LIST[i].value = MAX - 1 - i; tommy_list_insert_tail(&list, &LIST[i].node, &LIST[i]); } START("sort backward"); tommy_list_sort(&list, compare); STOP(); START("C qsort backward"); qsort(VECTOR, MAX, sizeof(VECTOR[0]), compare_vector); STOP(); test_list_order(list); /* use a small range of random value to insert a lot of duplicates */ list = 0; for(i=0;i<MAX;++i) { VECTOR[i].value = LIST[i].value = rnd(MAX / 1000 + 2); tommy_list_insert_tail(&list, &LIST[i].node, &LIST[i]); } START("sort random duplicate"); tommy_list_sort(&list, compare); STOP(); START("C qsort random duplicate"); qsort(VECTOR, MAX, sizeof(VECTOR[0]), compare_vector); STOP(); test_list_order(list); free(LIST); free(VECTOR); }