static void test_brick_core_simple_lifecycle(void) { struct pg_error *error = NULL; struct pg_brick *brick; struct pg_brick_config *config = pg_brick_config_new("mybrick", 2, 2, PG_MULTIPOLE); brick = pg_brick_new("foo", config, &error); g_assert(!brick); g_assert(error); g_assert(error->message); g_assert_cmpstr(error->message, ==, "Brick 'foo' not found"); pg_error_free(error); error = NULL; brick = pg_brick_new("nop", config, &error); g_assert(brick); g_assert(!error); pg_brick_decref(brick, &error); g_assert(!error); brick = pg_brick_decref(NULL, &error); g_assert(!brick); g_assert(error); g_assert(error->message); g_assert_cmpstr(error->message, ==, "NULL brick"); pg_error_free(error); error = NULL; pg_brick_config_free(config); }
int main(int argc, char **argv) { struct pg_brick *nic = NULL; struct pg_error *error = NULL; uint32_t rx, tx; int port_count; pg_start(argc, argv, &error); if (pg_error_is_set(&error)) { pg_error_print(error); pg_error_free(error); return 1; } port_count = pg_nic_port_count(); if (port_count == 0) { printf("Error: you need at least one DPDK port\n"); return 1; } for (int i = 0; i < port_count; i++) { nic = pg_nic_new_by_id("nic", i, &error); if (!nic) { printf("Error on nic creation (port %i)\n", i); pg_error_print(error); pg_error_free(error); pg_stop(); return 1; } pg_nic_capabilities(nic, &rx, &tx); printf("====== Port %i capabilities ======\nRX:\n", i); #define print_capa(R, capa) \ printf(#capa ":\t\t%s\n", (R) & (capa) ? "yes" : "no"); print_capa(rx, PG_NIC_RX_OFFLOAD_VLAN_STRIP); print_capa(rx, PG_NIC_RX_OFFLOAD_IPV4_CKSUM); print_capa(rx, PG_NIC_RX_OFFLOAD_UDP_CKSUM); print_capa(rx, PG_NIC_RX_OFFLOAD_TCP_CKSUM); print_capa(rx, PG_NIC_RX_OFFLOAD_TCP_LRO); print_capa(rx, PG_NIC_RX_OFFLOAD_QINQ_STRIP); print_capa(rx, PG_NIC_RX_OFFLOAD_OUTER_IPV4_CKSUM); printf("TX:\n"); print_capa(tx, PG_NIC_TX_OFFLOAD_VLAN_INSERT); print_capa(tx, PG_NIC_TX_OFFLOAD_IPV4_CKSUM); print_capa(tx, PG_NIC_TX_OFFLOAD_UDP_CKSUM); print_capa(tx, PG_NIC_TX_OFFLOAD_TCP_CKSUM); print_capa(tx, PG_NIC_TX_OFFLOAD_SCTP_CKSUM); print_capa(tx, PG_NIC_TX_OFFLOAD_TCP_TSO); print_capa(tx, PG_NIC_TX_OFFLOAD_UDP_TSO); print_capa(tx, PG_NIC_TX_OFFLOAD_OUTER_IPV4_CKSUM); print_capa(tx, PG_NIC_TX_OFFLOAD_QINQ_INSERT); #undef print_capa pg_brick_destroy(nic); } pg_stop(); return 0; }
int main(int argc, char **argv) { struct pg_error *error = NULL; struct pg_brick *nic, *app; struct pg_graph *graph; struct mydata pd; pd.pkt_count = 0; pg_start(argc, argv, &error); start_time = g_get_real_time(); if (pg_nic_port_count() < 1) { g_printf("No NIC port has been found, try adding --vdev=eth_pcap0,iface=eth0 arguments\n"); return 1; } nic = pg_nic_new_by_id("port 0", 0, &error); app = pg_rxtx_new("myapp", &rx_callback, &tx_callback, (void *) &pd); pg_brick_link(nic, app, &error); graph = pg_graph_new("example", nic, &error); while (42) if (pg_graph_poll(graph, &error) < 0) break; pg_error_print(error); pg_error_free(error); pg_graph_destroy(graph); pg_stop(); return 1; }
int main(int argc, char **argv) { /** * Simple example of firewall between two NICs. * Print brick simply show packets. * * [nic-west]--[print-west]--[firewall]--[print-east]--[nic_east] */ struct pg_error *error = NULL; struct pg_brick *nic_west, *nic_east; struct pg_brick *print_west, *print_east; struct pg_brick *fw; struct pg_graph *graph; /* init packetgraph and nics */ pg_start(argc, argv, &error); if (pg_nic_port_count() < 2) { printf("You need two DPDK ports to run this example\n"); return 1; } /* create bricks */ nic_west = pg_nic_new_by_id("nic-west", 0, &error); fw = pg_firewall_new("fw", PG_NO_CONN_WORKER, &error); print_west = pg_print_new("print-west", 0, PG_PRINT_FLAG_MAX, 0, &error); print_east = pg_print_new("print-east", 0, PG_PRINT_FLAG_MAX, 0, &error); nic_east = pg_nic_new_by_id("nic_east", 1, &error); /* link bricks */ pg_brick_chained_links(&error, nic_west, print_west, fw, print_east, nic_east); /* add some rules to firewall */ pg_firewall_rule_add(fw, "tcp portrange 1-1024", PG_MAX_SIDE, 1, &error); pg_firewall_rule_add(fw, "icmp", PG_MAX_SIDE, 1, &error); pg_firewall_reload(fw, &error); /* create a graph with all bricks inside */ graph = pg_graph_new("graph", fw, &error); printf("let's pool 1000*1000 times ...\n"); for (int i = 0; i < 1000000; i++) { if (pg_graph_poll(graph, &error) < 0) { pg_error_print(error); pg_error_free(error); } } pg_graph_destroy(graph); pg_stop(); return 0; }
static void test_queue_friend(void) { struct pg_error *error = NULL; struct pg_brick *q1, *q2, *q3; q1 = pg_queue_new("q1", 1, &error); CHECK_ERROR(error); q2 = pg_queue_new("q2", 1, &error); CHECK_ERROR(error); q3 = pg_queue_new("q3", 1, &error); CHECK_ERROR(error); /* classic scenario */ g_assert(!pg_queue_get_friend(q1)); g_assert(!pg_queue_get_friend(q2)); g_assert(!pg_queue_friend(q1, q2, &error)); CHECK_ERROR(error); g_assert(pg_queue_get_friend(q1) == q2); g_assert(pg_queue_are_friend(q1, q2)); g_assert(pg_queue_get_friend(q2) == q1); g_assert(pg_queue_are_friend(q2, q1)); pg_queue_unfriend(q1); g_assert(!pg_queue_get_friend(q1)); g_assert(!pg_queue_are_friend(q1, q2)); g_assert(!pg_queue_get_friend(q2)); g_assert(!pg_queue_are_friend(q2, q1)); /* same but unfriend with second brick */ g_assert(!pg_queue_friend(q1, q2, &error)); CHECK_ERROR(error); g_assert(pg_queue_are_friend(q1, q2)); g_assert(pg_queue_are_friend(q2, q1)); pg_queue_unfriend(q2); g_assert(!pg_queue_get_friend(q1)); g_assert(!pg_queue_get_friend(q2)); /* friend with itself */ g_assert(!pg_queue_friend(q1, q1, &error)); CHECK_ERROR(error); g_assert(pg_queue_get_friend(q1) == q1); g_assert(pg_queue_are_friend(q1, q1)); pg_queue_unfriend(q1); g_assert(!pg_queue_get_friend(q1)); /* several unfriend ok ? (test should die if not) */ pg_queue_unfriend(q1); pg_queue_unfriend(q1); pg_queue_unfriend(q1); /* error if already friend */ g_assert(!pg_queue_friend(q1, q2, &error)); CHECK_ERROR(error); g_assert(pg_queue_friend(q1, q2, &error) == -1); g_assert(pg_error_is_set(&error)); pg_error_free(error); error = NULL; g_assert(pg_queue_friend(q2, q1, &error) == -1); g_assert(pg_error_is_set(&error)); pg_error_free(error); error = NULL; g_assert(pg_queue_friend(q1, q3, &error) == -1); g_assert(pg_error_is_set(&error)); pg_error_free(error); error = NULL; g_assert(pg_queue_friend(q2, q3, &error) == -1); g_assert(pg_error_is_set(&error)); pg_error_free(error); error = NULL; g_assert(pg_queue_friend(q3, q1, &error) == -1); g_assert(pg_error_is_set(&error)); pg_error_free(error); error = NULL; g_assert(pg_queue_friend(q3, q2, &error) == -1); g_assert(pg_error_is_set(&error)); pg_error_free(error); error = NULL; /* check that death break friendship */ pg_brick_decref(q1, &error); CHECK_ERROR(error); g_assert(!pg_queue_get_friend(q2)); /* ... and can be friend again */ g_assert(!pg_queue_friend(q2, q3, &error)); g_assert(pg_queue_are_friend(q2, q3)); g_assert(pg_queue_are_friend(q3, q2)); pg_brick_decref(q2, &error); CHECK_ERROR(error); pg_brick_decref(q3, &error); CHECK_ERROR(error); }
static void test_brick_core_multiple_unlink_edge_same(void) { struct pg_brick *west_brick, *middle_brick, *east_brick; struct pg_brick_config *config = pg_brick_config_new("mybrick", 4, 4, PG_MULTIPOLE); struct pg_error *error = NULL; int64_t refcount; int ret; west_brick = pg_brick_new("nop", config, &error); g_assert(west_brick); g_assert(!error); middle_brick = pg_brick_new("nop", config, &error); g_assert(middle_brick); g_assert(!error); east_brick = pg_brick_new("nop", config, &error); g_assert(east_brick); g_assert(!error); ret = pg_brick_link(west_brick, middle_brick, &error); g_assert(ret == 0); g_assert(!error); ret = pg_brick_link(west_brick, middle_brick, &error); g_assert(ret == 0); g_assert(!error); ret = pg_brick_link(middle_brick, east_brick, &error); g_assert(ret == 0); g_assert(!error); ret = pg_brick_link(middle_brick, east_brick, &error); g_assert(ret == 0); g_assert(!error); ret = pg_brick_link(middle_brick, east_brick, &error); g_assert(ret == 0); g_assert(!error); refcount = pg_brick_refcount(west_brick); g_assert(refcount == 3); refcount = pg_brick_refcount(middle_brick); g_assert(refcount == 6); refcount = pg_brick_refcount(east_brick); g_assert(refcount == 4); g_assert(pg_brick_unlink_edge(west_brick, east_brick, &error) == -1); g_assert(error); pg_error_free(error); error = NULL; g_assert(pg_brick_unlink_edge(east_brick, west_brick, &error) == -1); g_assert(error); pg_error_free(error); error = NULL; g_assert(!pg_brick_unlink_edge(west_brick, middle_brick, &error)); g_assert(!error); refcount = pg_brick_refcount(west_brick); g_assert(refcount == 2); refcount = pg_brick_refcount(middle_brick); g_assert(refcount == 5); refcount = pg_brick_refcount(east_brick); g_assert(refcount == 4); g_assert(!pg_brick_unlink_edge(west_brick, middle_brick, &error)); g_assert(!error); refcount = pg_brick_refcount(west_brick); g_assert(refcount == 1); refcount = pg_brick_refcount(middle_brick); g_assert(refcount == 4); refcount = pg_brick_refcount(east_brick); g_assert(refcount == 4); g_assert(!pg_brick_unlink_edge(middle_brick, east_brick, &error)); g_assert(!error); refcount = pg_brick_refcount(west_brick); g_assert(refcount == 1); refcount = pg_brick_refcount(middle_brick); g_assert(refcount == 3); refcount = pg_brick_refcount(east_brick); g_assert(refcount == 3); g_assert(!pg_brick_unlink_edge(middle_brick, east_brick, &error)); g_assert(!error); refcount = pg_brick_refcount(west_brick); g_assert(refcount == 1); refcount = pg_brick_refcount(middle_brick); g_assert(refcount == 2); refcount = pg_brick_refcount(east_brick); g_assert(refcount == 2); g_assert(!pg_brick_unlink_edge(middle_brick, east_brick, &error)); g_assert(!error); refcount = pg_brick_refcount(west_brick); g_assert(refcount == 1); refcount = pg_brick_refcount(middle_brick); g_assert(refcount == 1); refcount = pg_brick_refcount(east_brick); g_assert(refcount == 1); /* destroy */ pg_brick_decref(west_brick, &error); g_assert(!error); pg_brick_decref(middle_brick, &error); g_assert(!error); pg_brick_decref(east_brick, &error); g_assert(!error); pg_brick_config_free(config); }
static void test_brick_core_multiple_unlink_edge_(struct pg_brick *west[4], struct pg_brick *middle, struct pg_brick *east[4]) { struct pg_error *error = NULL; int ret; int i; for (i = 0; i < 4; i++) { ret = pg_brick_chained_links(&error, west[i], middle, east[i]); g_assert(ret == 0); g_assert(!error); } g_assert(pg_brick_refcount(middle) == 9); for (i = 0; i < 4; i++) { g_assert(pg_brick_refcount(west[i]) == 2); g_assert(pg_brick_refcount(east[i]) == 2); } g_assert(pg_brick_unlink_edge(west[0], east[0], &error) == -1); g_assert(error); pg_error_free(error); error = NULL; g_assert(pg_brick_unlink_edge(east[0], west[0], &error) == -1); g_assert(error); pg_error_free(error); error = NULL; g_assert(!pg_brick_unlink_edge(west[0], middle, &error)); g_assert(!error); g_assert(pg_brick_refcount(west[0]) == 1); for (i = 1; i < 4; i++) g_assert(pg_brick_refcount(west[i]) == 2); g_assert(pg_brick_refcount(middle) == 8); for (i = 0; i < 4; i++) g_assert(pg_brick_refcount(east[i]) == 2); g_assert(!pg_brick_unlink_edge(west[1], middle, &error)); g_assert(!error); for (i = 0; i < 2; i++) g_assert(pg_brick_refcount(west[i]) == 1); for (i = 2; i < 4; i++) g_assert(pg_brick_refcount(west[i]) == 2); g_assert(pg_brick_refcount(middle) == 7); for (i = 0; i < 4; i++) g_assert(pg_brick_refcount(east[i]) == 2); g_assert(!pg_brick_unlink_edge(middle, east[0], &error)); g_assert(!error); for (i = 0; i < 2; i++) g_assert(pg_brick_refcount(west[i]) == 1); for (i = 2; i < 4; i++) g_assert(pg_brick_refcount(west[i]) == 2); g_assert(pg_brick_refcount(middle) == 6); g_assert(pg_brick_refcount(east[0]) == 1); for (i = 1; i < 4; i++) g_assert(pg_brick_refcount(east[i]) == 2); g_assert(!pg_brick_unlink_edge(middle, east[1], &error)); g_assert(!error); for (i = 0; i < 2; i++) g_assert(pg_brick_refcount(west[i]) == 1); for (i = 2; i < 4; i++) g_assert(pg_brick_refcount(west[i]) == 2); g_assert(pg_brick_refcount(middle) == 5); for (i = 0; i < 2; i++) g_assert(pg_brick_refcount(east[i]) == 1); for (i = 2; i < 4; i++) g_assert(pg_brick_refcount(east[i]) == 2); g_assert(!pg_brick_unlink_edge(west[2], middle, &error)); g_assert(!error); for (i = 0; i < 3; i++) g_assert(pg_brick_refcount(west[i]) == 1); g_assert(pg_brick_refcount(west[3]) == 2); g_assert(pg_brick_refcount(middle) == 4); for (i = 0; i < 2; i++) g_assert(pg_brick_refcount(east[i]) == 1); for (i = 2; i < 4; i++) g_assert(pg_brick_refcount(east[i]) == 2); g_assert(!pg_brick_unlink_edge(middle, east[2], &error)); g_assert(!error); for (i = 0; i < 3; i++) g_assert(pg_brick_refcount(west[i]) == 1); g_assert(pg_brick_refcount(west[3]) == 2); g_assert(pg_brick_refcount(middle) == 3); for (i = 0; i < 3; i++) g_assert(pg_brick_refcount(east[i]) == 1); g_assert(pg_brick_refcount(east[3]) == 2); g_assert(!pg_brick_unlink_edge(west[3], middle, &error)); g_assert(!error); for (i = 0; i < 4; i++) g_assert(pg_brick_refcount(west[i]) == 1); g_assert(pg_brick_refcount(middle) == 2); for (i = 0; i < 3; i++) g_assert(pg_brick_refcount(east[i]) == 1); g_assert(pg_brick_refcount(east[3]) == 2); g_assert(!pg_brick_unlink_edge(middle, east[3], &error)); g_assert(!error); for (i = 0; i < 4; i++) g_assert(pg_brick_refcount(west[i]) == 1); g_assert(pg_brick_refcount(middle) == 1); for (i = 0; i < 4; i++) g_assert(pg_brick_refcount(east[i]) == 1); }