void xml_delete(xmlnode* node) { if(node->children) { xmlnode* child = node->children; do { xml_delete(child); } while((child = child->next)); } if(node->name) free(node->name); if(node->attributes) hmap_destroy(&node->attributes); if(node->parent) { xmlnode** nptr = &node->parent->children; do { if(*nptr == node) { *nptr = node->next; break; } nptr = &(*nptr)->next; } while(*nptr); } free(node); }
static void resize(struct hmap *hmap, size_t new_mask) { struct hmap tmp; size_t i; assert(!(new_mask & (new_mask + 1))); assert(new_mask != SIZE_MAX); hmap_init(&tmp); if (new_mask) { tmp.buckets = xmalloc(sizeof *tmp.buckets * (new_mask + 1)); tmp.mask = new_mask; for (i = 0; i <= tmp.mask; i++) { tmp.buckets[i] = NULL; } } for (i = 0; i <= hmap->mask; i++) { struct hmap_node *node, *next; for (node = hmap->buckets[i]; node; node = next) { next = node->next; hmap_insert_fast(&tmp, node, node->hash); } } hmap_swap(hmap, &tmp); hmap_destroy(&tmp); }
/** * repad_net_free - Do a free in Repa net module structures * * @param signo The signal to send to threads * @return <0 for errors */ int repad_net_free(int signo) { /* Destroy pc_prod_cons packets receiver */ pc_destroy(repad_attr.pc_receive_packets); repad_attr.pc_receive_packets = NULL; /* Destroy list of all ifaces */ dll_destroy_all(repad_attr.list_all_ifaces); repad_attr.list_all_ifaces = NULL; /* Delete packets list */ dll_destroy_all(repad_attr.list_pkt_without_match); repad_attr.list_pkt_without_match = NULL; /* Delete hashmap memory message */ hmap_destroy(repad_attr.hm_memory_message); repad_attr.hm_memory_message = NULL; /* Stop Raw socket module */ if (repad_attr.bl_raw_socket_enable) { repad_net_free_raw_socket(signo); } /* Stop UDP socket module */ if (repad_attr.bl_udp_socket_enable) { repad_net_free_udp_socket(signo); } /* Stop Overlay module */ if (repad_attr.bl_overlay_enable) { repad_net_free_overlay(signo); } return 0; }
void packet_handle_std_validate(struct packet_handle_std *handle) { if(handle->valid) return; struct ofl_match_tlv * iter, *next; HMAP_FOR_EACH_SAFE(iter, next, struct ofl_match_tlv, hmap_node, &handle->match.match_fields){ free(iter->value); free(iter); } // hsh hmap_destroy(&handle->match.match_fields); //end ofl_structs_match_init(&handle->match); if (nblink_packet_parse(handle->pkt->buffer,&handle->match, handle->proto) < 0) return; handle->valid = true; /* Add in_port value to the hash_map */ ofl_structs_match_put32(&handle->match, OXM_OF_IN_PORT, handle->pkt->in_port); /*Add metadata value to the hash_map */ ofl_structs_match_put64(&handle->match, OXM_OF_METADATA, 0xffffffffffffffff); return; }
static void resize(struct hmap *hmap, size_t new_mask, const char *where) { struct hmap tmp; size_t i; ovs_assert(is_pow2(new_mask + 1)); hmap_init(&tmp); if (new_mask) { tmp.buckets = xmalloc(sizeof *tmp.buckets * (new_mask + 1)); tmp.mask = new_mask; for (i = 0; i <= tmp.mask; i++) { tmp.buckets[i] = NULL; } } for (i = 0; i <= hmap->mask; i++) { struct hmap_node *node, *next; int count = 0; for (node = hmap->buckets[i]; node; node = next) { next = node->next; hmap_insert_fast(&tmp, node, node->hash); count++; } if (count > 5) { static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10); COVERAGE_INC(hmap_pathological); VLOG_DBG_RL(&rl, "%s: %d nodes in bucket (%"PRIuSIZE" nodes, %"PRIuSIZE" buckets)", where, count, hmap->n, hmap->mask + 1); } } hmap_swap(hmap, &tmp); hmap_destroy(&tmp); }
/* Frees all the data that 'simap' contains. */ void simap_destroy(struct simap *simap) { if (simap) { simap_clear(simap); hmap_destroy(&simap->map); } }
/* Like shash_destroy(), but also free() each node's 'data'. */ void shash_destroy_free_data(struct shash *sh) { if (sh) { shash_clear_free_data(sh); hmap_destroy(&sh->map); } }
void shash_destroy(struct shash *sh) { if (sh) { shash_clear(sh); hmap_destroy(&sh->map); } }
/* Frees SET and its nodes and strings. */ void string_set_destroy (struct string_set *set) { if (set != NULL) { string_set_clear (set); hmap_destroy (&set->hmap); } }
/* Frees the storage associated with SET, if SET is nonnull. (Does not free SET itself.) */ void attrset_destroy (struct attrset *set) { if (set != NULL) { struct attribute *attr, *next; HMAP_FOR_EACH_SAFE (attr, next, struct attribute, node, &set->map) attribute_destroy (attr); hmap_destroy (&set->map); } }
int main(int argc, char **argv) { struct hmap *map = malloc(sizeof(*map)); hmap_init(map); struct hmap_node node2 = {1, NULL}; hmap_insert_fast(map, &nodes[0].node, strlen(nodes[0].key)); hmap_insert_fast(map, &nodes[1].node, strlen(nodes[1].key)); struct nd *q; //HMAP_FOR_EACH(q, node, map) { // printf("key= %s",q->key); //} printf("map size %lu, capacity %lu\n", hmap_count(map), hmap_capacity(map)); hmap_destroy(map); }
/* Handles meter_mod messages with DELETE command. */ static ofl_err meter_table_delete(struct meter_table *table, struct ofl_msg_meter_mod *mod) { if (mod->meter_id == OFPM_ALL) { struct meter_entry *entry, *next; HMAP_FOR_EACH_SAFE(entry, next, struct meter_entry, node, &table->meter_entries) { meter_entry_destroy(entry); } hmap_destroy(&table->meter_entries); hmap_init(&table->meter_entries); table->entries_num = 0; table->bands_num = 0; ofl_msg_free_meter_mod(mod, false); return 0; } else {
/* Handles group mod messages with DELETE command. */ static ofl_err group_table_delete(struct group_table *table, struct ofl_msg_group_mod *mod) { if (mod->group_id == OFPG_ALL) { struct group_entry *entry, *next; HMAP_FOR_EACH_SAFE(entry, next, struct group_entry, node, &table->entries) { group_entry_destroy(entry); } hmap_destroy(&table->entries); hmap_init(&table->entries); table->entries_num = 0; table->buckets_num = 0; ofl_msg_free_group_mod(mod, true, table->dp->exp); return 0; } else {
/* Decreases PAGE's reference count and destroys PAGE if this causes the reference count to fall to zero. */ void render_page_unref (struct render_page *page) { if (page != NULL && --page->ref_cnt == 0) { int i; struct render_overflow *overflow, *next; HMAP_FOR_EACH_SAFE (overflow, next, struct render_overflow, node, &page->overflows) free (overflow); hmap_destroy (&page->overflows); table_unref (page->table); for (i = 0; i < TABLE_N_AXES; ++i) { free (page->join_crossing[i]); free (page->cp[i]); } free (page); }
int main(int argc, char *argv[]) { struct unixctl_server *unixctl; bool exiting; int retval; ovs_cmdl_proctitle_init(argc, argv); set_program_name(argv[0]); parse_options(argc, argv); fatal_ignore_sigpipe(); daemonize_start(); retval = unixctl_server_create(NULL, &unixctl); if (retval) { exit(EXIT_FAILURE); } unixctl_command_register("exit", "", 0, 0, ovn_controller_exit, &exiting); daemonize_complete(); ovsrec_init(); sbrec_init(); ofctrl_init(); lflow_init(); /* Connect to OVS OVSDB instance. We do not monitor all tables by * default, so modules must register their interest explicitly. */ struct idl_loop ovs_idl_loop = IDL_LOOP_INITIALIZER( ovsdb_idl_create(ovs_remote, &ovsrec_idl_class, false, true)); ovsdb_idl_add_table(ovs_idl_loop.idl, &ovsrec_table_open_vswitch); ovsdb_idl_add_column(ovs_idl_loop.idl, &ovsrec_open_vswitch_col_external_ids); chassis_register_ovs_idl(ovs_idl_loop.idl); encaps_register_ovs_idl(ovs_idl_loop.idl); binding_register_ovs_idl(ovs_idl_loop.idl); physical_register_ovs_idl(ovs_idl_loop.idl); get_initial_snapshot(ovs_idl_loop.idl); /* Connect to OVN SB database. */ char *ovnsb_remote = get_ovnsb_remote(ovs_idl_loop.idl); struct idl_loop ovnsb_idl_loop = IDL_LOOP_INITIALIZER( ovsdb_idl_create(ovnsb_remote, &sbrec_idl_class, true, true)); get_initial_snapshot(ovnsb_idl_loop.idl); /* Main loop. */ exiting = false; while (!exiting) { struct controller_ctx ctx = { .ovs_idl = ovs_idl_loop.idl, .ovs_idl_txn = idl_loop_run(&ovs_idl_loop), .ovnsb_idl = ovnsb_idl_loop.idl, .ovnsb_idl_txn = idl_loop_run(&ovnsb_idl_loop), }; const struct ovsrec_bridge *br_int = get_br_int(ctx.ovs_idl); const char *chassis_id = get_chassis_id(ctx.ovs_idl); if (chassis_id) { chassis_run(&ctx, chassis_id); encaps_run(&ctx, br_int, chassis_id); binding_run(&ctx, br_int, chassis_id); } if (br_int) { struct hmap flow_table = HMAP_INITIALIZER(&flow_table); lflow_run(&ctx, &flow_table); if (chassis_id) { physical_run(&ctx, br_int, chassis_id, &flow_table); } ofctrl_run(br_int, &flow_table); hmap_destroy(&flow_table); } unixctl_server_run(unixctl); unixctl_server_wait(unixctl); if (exiting) { poll_immediate_wake(); } idl_loop_commit_and_wait(&ovnsb_idl_loop); idl_loop_commit_and_wait(&ovs_idl_loop); if (br_int) { ofctrl_wait(); } poll_block(); } /* It's time to exit. Clean up the databases. */ bool done = false; while (!done) { struct controller_ctx ctx = { .ovs_idl = ovs_idl_loop.idl, .ovs_idl_txn = idl_loop_run(&ovs_idl_loop), .ovnsb_idl = ovnsb_idl_loop.idl, .ovnsb_idl_txn = idl_loop_run(&ovnsb_idl_loop), }; const struct ovsrec_bridge *br_int = get_br_int(ctx.ovs_idl); const char *chassis_id = get_chassis_id(ctx.ovs_idl); /* Run all of the cleanup functions, even if one of them returns false. * We're done if all of them return true. */ done = binding_cleanup(&ctx, chassis_id); done = chassis_cleanup(&ctx, chassis_id) && done; done = encaps_cleanup(&ctx, br_int) && done; if (done) { poll_immediate_wake(); } idl_loop_commit_and_wait(&ovnsb_idl_loop); idl_loop_commit_and_wait(&ovs_idl_loop); poll_block(); } unixctl_server_destroy(unixctl); lflow_destroy(); ofctrl_destroy(); idl_loop_destroy(&ovs_idl_loop); idl_loop_destroy(&ovnsb_idl_loop); free(ovnsb_remote); free(ovs_remote); exit(retval); } static void parse_options(int argc, char *argv[]) { enum { OPT_PEER_CA_CERT = UCHAR_MAX + 1, VLOG_OPTION_ENUMS, DAEMON_OPTION_ENUMS }; static struct option long_options[] = { {"help", no_argument, NULL, 'h'}, {"version", no_argument, NULL, 'V'}, VLOG_LONG_OPTIONS, DAEMON_LONG_OPTIONS, STREAM_SSL_LONG_OPTIONS, {"peer-ca-cert", required_argument, NULL, OPT_PEER_CA_CERT}, {NULL, 0, NULL, 0} }; char *short_options = ovs_cmdl_long_options_to_short_options(long_options); for (;;) { int c; c = getopt_long(argc, argv, short_options, long_options, NULL); if (c == -1) { break; } switch (c) { case 'h': usage(); case 'V': ovs_print_version(OFP13_VERSION, OFP13_VERSION); exit(EXIT_SUCCESS); VLOG_OPTION_HANDLERS DAEMON_OPTION_HANDLERS STREAM_SSL_OPTION_HANDLERS case OPT_PEER_CA_CERT: stream_ssl_set_peer_ca_cert_file(optarg); break; case '?': exit(EXIT_FAILURE); default: abort(); } } free(short_options); argc -= optind; argv += optind; if (argc == 0) { ovs_remote = xasprintf("unix:%s/db.sock", ovs_rundir()); } else if (argc == 1) { ovs_remote = xstrdup(argv[0]); } else { VLOG_FATAL("exactly zero or one non-option argument required; " "use --help for usage"); } } static void usage(void) { printf("%s: OVN controller\n" "usage %s [OPTIONS] [OVS-DATABASE]\n" "where OVS-DATABASE is a socket on which the OVS OVSDB server is listening.\n", program_name, program_name); stream_usage("OVS-DATABASE", true, false, false); daemon_usage(); vlog_usage(); printf("\nOther options:\n" " -h, --help display this help message\n" " -V, --version display version information\n"); exit(EXIT_SUCCESS); } static void ovn_controller_exit(struct unixctl_conn *conn, int argc OVS_UNUSED, const char *argv[] OVS_UNUSED, void *exiting_) { bool *exiting = exiting_; *exiting = true; unixctl_command_reply(conn, NULL); }
void xml_read_callback(xmlreader* r, xml_read_callback_fn* fn) { ssize_t n; buffer* b = r->b; stralloc tag, attr, val; stralloc_init(&tag); stralloc_init(&attr); stralloc_init(&val); hmap_init(XML_HMAP_BUCKETS, &r->attrmap); while((n = buffer_skip_until(b, "<", 1)) > 0) { const char* s; stralloc_zero(&tag); r->self_closing = r->closing = 0; s = buffer_peek(b); if(*s == '/') { r->closing = 1; buffer_skipc(b); } else if(*s == '?') { r->self_closing = 1; } else if(*s == '!') { if(buffer_skip_until(b, ">", 1) <= 0) return; continue; } if((n = buffer_gettok_sa(b, &tag, " \n\t\r\v/>", 7)) < 0) return; stralloc_nul(&tag); buffer_skipspace(b); while((s = buffer_peek(b)) && isalpha(*s)) { char ch; int quoted = 0; const char* charset; stralloc_zero(&attr); stralloc_zero(&val); if((n = buffer_gettok_sa(b, &attr, "=", 1)) < 0) break; if(buffer_skipc(b) < 0) return; if(*buffer_peek(b) == '"') { if(buffer_skipc(b) < 0) return; quoted = 1; } charset = quoted ? "\"" : "/> \t\r\n\v"; if((n = buffer_gettok_sa(b, &val, charset, str_len(charset))) < 0) break; if(quoted && buffer_skipc(b) < 0) return; stralloc_nul(&attr); stralloc_nul(&val); hmap_set(&r->attrmap, attr.s, attr.len, val.s, val.len + 1); if(!fn(r, XML_ATTRIBUTE, &attr, &val, NULL)) return; buffer_skipspace(b); } buffer_skipspace(b); if((s = buffer_peek(b)) && str_chr("/?", *s) < 2) { r->self_closing = 1; r->closing = 0; buffer_skipc(b); } buffer_skipspace(b); if((s = buffer_peek(b)) && *s == '>') buffer_skipc(b); if(!fn(r, XML_ELEMENT, &tag, NULL, &r->attrmap)) return; if(r->attrmap) { hmap_destroy(&r->attrmap); r->attrmap = NULL; } hmap_init(XML_HMAP_BUCKETS, &r->attrmap); stralloc_zero(&tag); if((n = buffer_gettok_sa(b, &tag, "<", 1)) < 0) return; s = buffer_peek(b); if(!is_whitespace(tag.s, tag.len)) { if(!fn(r, XML_TEXT, NULL, &tag, NULL)) return; } } }