int main(void) { int n; for (n = 0; n < 32; n++) { /* Check minimum x such that f(x) == n. */ check_log_2_floor(1 << n, n); check_ctz(1 << n, n); /* Check maximum x such that f(x) == n. */ check_log_2_floor((1 << n) | ((1 << n) - 1), n); check_ctz(UINT32_MAX << n, n); /* Check a random value in the middle. */ check_log_2_floor((random_uint32() & ((1 << n) - 1)) | (1 << n), n); check_ctz((random_uint32() | 1) << n, n); } /* Check ctz(0). * (log_2_floor(0) is undefined.) */ check_ctz(0, 32); check_bitwise_copy(); check_bitwise_zero(); check_bitwise_one(); check_bitwise_is_all_zeros(); return 0; }
void ethos_setup(ethos_t *dev, const ethos_params_t *params) { dev->netdev.driver = &netdev_driver_ethos; dev->uart = params->uart; dev->state = WAIT_FRAMESTART; dev->framesize = 0; dev->frametype = 0; dev->last_framesize = 0; tsrb_init(&dev->inbuf, (char*)params->buf, params->bufsize); mutex_init(&dev->out_mutex); uint32_t a = random_uint32(); memcpy(dev->mac_addr, (char*)&a, 4); a = random_uint32(); memcpy(dev->mac_addr+4, (char*)&a, 2); dev->mac_addr[0] &= (0x2); /* unset globally unique bit */ dev->mac_addr[0] &= ~(0x1); /* set unicast bit*/ uart_init(params->uart, params->baudrate, ethos_isr, (void*)dev); uint8_t frame_delim = ETHOS_FRAME_DELIMITER; uart_write(dev->uart, &frame_delim, 1); ethos_send_frame(dev, dev->mac_addr, 6, ETHOS_FRAME_TYPE_HELLO); }
static void benchmark(struct classifier *cls) { struct timespec before, after; struct rusage rbefore, rafter; struct flow_wildcards wc; struct match match_wc_str; struct flow flow; size_t i; memset(&flow, 0, sizeof flow); flow.in_port.ofp_port = OFPP_LOCAL; flow.dl_type = htons(ETH_TYPE_IP); flow.nw_proto = IPPROTO_TCP; fat_rwlock_rdlock(&cls->rwlock); clock_gettime(CLOCK_MONOTONIC_RAW, &before); getrusage(RUSAGE_SELF, &rbefore); for (i = 0; i < 10000000; i++) { eth_addr_random(flow.dl_src); eth_addr_random(flow.dl_dst); flow.nw_src = htonl(random_uint32()); flow.nw_dst = htonl(random_uint32()); flow.tp_src = htons(random_uint16()); flow.tp_dst = htons(random_uint16()); flow.tp_dst = htons(i); flow_wildcards_init_catchall(&wc); //VLOG_DBG("Finding relevant wc's for flow: tp_dst=%d (0x%04x)", // ntohs(flow.tp_dst), ntohs(flow.tp_dst)); classifier_lookup(cls, &flow, &wc); match_init(&match_wc_str, &flow, &wc); //VLOG_DBG("Relevant fields: %s", match_to_string(&match_wc_str, 0)); } getrusage(RUSAGE_SELF, &rafter); clock_gettime(CLOCK_MONOTONIC_RAW, &after); fat_rwlock_unlock(&cls->rwlock); printf("real %lldms\n" "user %lldms\n" "sys %lldms\n" "soft faults %ld\n" "hard faults %ld\n", timespec_to_msec(&after) - timespec_to_msec(&before), timeval_to_msec(&rafter.ru_utime) - timeval_to_msec(&rbefore.ru_utime), timeval_to_msec(&rafter.ru_stime) - timeval_to_msec(&rbefore.ru_stime), rafter.ru_minflt - rbefore.ru_minflt, rafter.ru_majflt - rbefore.ru_majflt); }
/* Chooses and returns a randomly selected node from 'hmap', which must not be * empty. * * I wouldn't depend on this algorithm to be fair, since I haven't analyzed it. * But it does at least ensure that any node in 'hmap' can be chosen. */ struct hmap_node * hmap_random_node(const struct hmap *hmap) { struct hmap_node *bucket, *node; size_t n, i; /* Choose a random non-empty bucket. */ for (;;) { bucket = hmap->buckets[random_uint32() & hmap->mask]; if (bucket) { break; } } /* Count nodes in bucket. */ n = 0; for (node = bucket; node; node = node->next) { n++; } /* Choose random node from bucket. */ i = random_range(n); for (node = bucket; i-- > 0; node = node->next) { continue; } return node; }
static void odp_execute_sample(void *dp, struct dpif_packet *packet, bool steal, struct pkt_metadata *md, const struct nlattr *action, odp_execute_cb dp_execute_action, bool more_actions) { const struct nlattr *subactions = NULL; const struct nlattr *a; size_t left; NL_NESTED_FOR_EACH_UNSAFE (a, left, action) { int type = nl_attr_type(a); switch ((enum ovs_sample_attr) type) { case OVS_SAMPLE_ATTR_PROBABILITY: if (random_uint32() >= nl_attr_get_u32(a)) { return; } break; case OVS_SAMPLE_ATTR_ACTIONS: subactions = a; break; case OVS_SAMPLE_ATTR_UNSPEC: case __OVS_SAMPLE_ATTR_MAX: default: OVS_NOT_REACHED(); } }
int main(void) { gnrc_netreg_entry_t ne; uint8_t cpuid[CPUID_LEN]; cpuid_get(cpuid); conn_test_id = djb2_hash(cpuid, CPUID_LEN); random_init(conn_test_id); ne.pid = thread_create(_stack, sizeof(_stack), THREAD_PRIORITY_MAIN - 1, THREAD_CREATE_STACKTEST, _listener, NULL, "listener"); ne.demux_ctx = GNRC_NETREG_DEMUX_CTX_ALL; gnrc_netreg_register(GNRC_NETTYPE_UNDEF, &ne); puts("Connectivity Test program!"); printf("MY ID: %08lX\n", (unsigned long) conn_test_id); unsigned res = CONN_TEST_CHAN; if (gnrc_netapi_set(CONN_TEST_NETIF, NETOPT_CHANNEL, 0, (uint16_t *)&res, sizeof(uint16_t)) < 0) { puts("main: error setting channel"); } unsigned int addr_len = 8; if (gnrc_netapi_set(CONN_TEST_NETIF, NETOPT_SRC_LEN, 0, (uint16_t *)&addr_len, sizeof(uint16_t)) < 0) { printf("main: error setting addressing mode\n"); } xtimer_set_msg(&ct_timer, (SEC_IN_USEC * 3) + (random_uint32() & 0x001FFFFF), &ct_m, ne.pid); char line_buf[SHELL_DEFAULT_BUFSIZE]; shell_run(shell_commands, line_buf, SHELL_DEFAULT_BUFSIZE); return 0; }
static void * search_ccmap(void *aux_) { struct ccmap_aux *aux = aux_; size_t i; if (mutation_frac) { for (i = 0; i < n_elems; i++) { uint32_t hash = hash_int(i, 0); if (random_uint32() < mutation_frac) { ovs_mutex_lock(&aux->mutex); uint32_t count = ccmap_find(aux->ccmap, hash); if (count) { ccmap_dec(aux->ccmap, hash); } ovs_mutex_unlock(&aux->mutex); } else { ignore(ccmap_find(aux->ccmap, hash)); } } } else { for (i = 0; i < n_elems; i++) { ignore(ccmap_find(aux->ccmap, hash_int(i, 0))); } } return NULL; }
/* * Determine the level for a skiplist node by choosing a level N with * probability P(N) = 1/(2**(N+1)) in the range 0..32, with the returned * level clamped at the current skiplist height plus 1. */ static int skiplist_determine_level(struct skiplist *sl) { int lvl; lvl = clz32(random_uint32()); return MIN(lvl, sl->level + 1); }
static void trylocks(int id) { int i, fd; srand48(getpid()); fd = open (lockfile, O_RDWR, 0); if (fd < 0) err(1, "%s", lockfile); printf("%d: start\n", id); for (i = 0; i < nlocks; i++) { struct flock fl; fl.l_start = random_uint32() % filesize; fl.l_len = random_uint32() % filesize; switch (random_uint32() % 3) { case 0: fl.l_type = F_RDLCK; break; case 1: fl.l_type = F_WRLCK; break; case 2: fl.l_type = F_UNLCK; break; } fl.l_whence = SEEK_SET; (void)fcntl(fd, F_SETLKW, &fl); if (usleep(sleeptime) < 0) #if defined(__FreeBSD__) if (errno != EINTR) #endif err(1, "usleep"); } printf("%d: done\n", id); close (fd); }
/** * @brief Generate random unused local port above the well-known ports (> 1024). * * @returns Generated port number. */ static uint16_t _get_random_local_port(void) { uint16_t ret = 0; do { ret = random_uint32(); if (ret < 1024) { continue; } } while(_is_local_port_in_use(ret)); return ret; }
uint8_t hal_getrand(void) { #if defined(MODULE_PERIPH_HWRNG) uint8_t res; hwrng_read((char *)&res, sizeof(res)); return res; #elif defined(MODULE_RANDOM) return (uint8_t)(random_uint32() % UINT8_MAX); #else return 4; /* keeping the meme alive ;-) */ #endif }
/* Initialize a flow with random fields that matter for nx_hash_fields. */ void flow_random_hash_fields(struct flow *flow) { uint16_t rnd = random_uint16(); /* Initialize to all zeros. */ memset(flow, 0, sizeof *flow); eth_addr_random(flow->dl_src); eth_addr_random(flow->dl_dst); flow->vlan_tci = (OVS_FORCE ovs_be16) (random_uint16() & VLAN_VID_MASK); /* Make most of the random flows IPv4, some IPv6, and rest random. */ flow->dl_type = rnd < 0x8000 ? htons(ETH_TYPE_IP) : rnd < 0xc000 ? htons(ETH_TYPE_IPV6) : (OVS_FORCE ovs_be16)rnd; if (dl_type_is_ip_any(flow->dl_type)) { if (flow->dl_type == htons(ETH_TYPE_IP)) { flow->nw_src = (OVS_FORCE ovs_be32)random_uint32(); flow->nw_dst = (OVS_FORCE ovs_be32)random_uint32(); } else { random_bytes(&flow->ipv6_src, sizeof flow->ipv6_src); random_bytes(&flow->ipv6_dst, sizeof flow->ipv6_dst); } /* Make most of IP flows TCP, some UDP or SCTP, and rest random. */ rnd = random_uint16(); flow->nw_proto = rnd < 0x8000 ? IPPROTO_TCP : rnd < 0xc000 ? IPPROTO_UDP : rnd < 0xd000 ? IPPROTO_SCTP : (uint8_t)rnd; if (flow->nw_proto == IPPROTO_TCP || flow->nw_proto == IPPROTO_UDP || flow->nw_proto == IPPROTO_SCTP) { flow->tp_src = (OVS_FORCE ovs_be16)random_uint16(); flow->tp_dst = (OVS_FORCE ovs_be16)random_uint16(); } } }
END_TEST void test_wordcount (uint32_t bits) { const uint32_t iterations = 10000000; // Do not set higher than ~134,000,000 const uint32_t mask = (1 << bits) - 1; uint32_t buckets[32] = {0}; uint32_t tmp; /* Discard first result */ random_uint32 (); for (uint32_t i = 0; i < iterations; ++i) { tmp = random_uint32 (); for (uint32_t j = 0; j < 32 / bits; ++j) buckets[(tmp >> (j * bits)) & mask] += 1; } uint32_t expected = ((32 / bits) * iterations) >> bits; printf ("Random Test: Wordcount (k=%u)\n", bits); uint32_t maxerror = 0; for (uint32_t i = 0; i < (uint32_t)(1 << bits); ++i) { uint32_t error = (buckets[i] > expected) ? (buckets[i] - expected) : (expected - buckets[i]); printf ("[%2u] = %u (expected %u)\n", i, buckets[i], expected); if (error > maxerror) maxerror = error; } printf ("Max Error: %f%%\n\n", (maxerror * 100.0f) / expected); }
static void check_bitwise_is_all_zeros(void) { int n_loops; n_loops = 0; for (n_loops = 0; n_loops < 100; n_loops++) { ovs_be64 x = htonll(0); int i; for (i = 0; i < 64; i++) { ovs_be64 bit; int ofs, n; /* Change a random 0-bit into a 1-bit. */ do { bit = htonll(UINT64_C(1) << (random_uint32() % 64)); } while (x & bit); x |= bit; for (ofs = 0; ofs < 64; ofs++) { for (n = 0; n <= 64 - ofs; n++) { bool expect; bool answer; expect = (n == 64 ? x == 0 : !(x & htonll(((UINT64_C(1) << n) - 1) << ofs))); answer = bitwise_is_all_zeros(&x, sizeof x, ofs, n); if (expect != answer) { fprintf(stderr, "bitwise_is_all_zeros(0x%016"PRIx64",8,%d,%d " "returned %s instead of %s\n", ntohll(x), ofs, n, answer ? "true" : "false", expect ? "true" : "false"); abort(); } } } } } }
/* This code is useful for generating new test cases for RFC 1624 section 4. */ static void generate_rfc1624_test_case(void) { int i; for (i = 0; i < 10000000; i++) { uint32_t data[8]; int j; for (j = 0; j < 8; j++) { data[j] = random_uint32(); } data[7] &= 0x0000ffff; data[7] |= 0x55550000; if (ntohs(~csum(data, sizeof data - 2)) == 0xcd7a) { ovs_hex_dump(stdout, data, sizeof data, 0, false); exit(0); } } }
/* Creates and returns a new MAC learning table. */ struct mac_learning * mac_learning_create(void) { struct mac_learning *ml; int i; ml = xmalloc(sizeof *ml); list_init(&ml->lrus); list_init(&ml->free); for (i = 0; i < MAC_HASH_SIZE; i++) { list_init(&ml->table[i]); } for (i = 0; i < MAC_MAX; i++) { struct mac_entry *s = &ml->entries[i]; list_push_front(&ml->free, &s->lru_node); } ml->secret = random_uint32(); ml->flood_vlans = NULL; return ml; }
bool kadhlp_gen_udp_key( uint32_t* udp_key_out ) { bool result = false; uint32_t udp_key = 0; do { if (!udp_key_out) break; udp_key = random_uint32(); *udp_key_out = udp_key; result = true; } while (false); return result; }
static void *_listener(void *unused) { msg_init_queue(_listener_mq, 8); msg_t msg; while (1) { msg_receive(&msg); gnrc_pktsnip_t *snip = NULL; switch (msg.type) { case GNRC_NETAPI_MSG_TYPE_RCV: { gnrc_pktsnip_t *pkt = (gnrc_pktsnip_t *)msg.content.ptr; snip = pkt; while (snip != NULL) { if (snip->type == GNRC_NETTYPE_NETIF) { //gnrc_netif_hdr_print(snip->data); } else if (snip->type == GNRC_NETTYPE_UNDEF) { printf("ID: %08lX\n", (unsigned long) ((conn_test_payload_t*)snip->data)->id); } snip = snip->next; } gnrc_pktbuf_release(pkt); break; } case CONN_TEST_SEND: _send(0, NULL); xtimer_set_msg(&ct_timer, (SEC_IN_USEC * 3) + (random_uint32() & 0x001FFFFF), &ct_m, sched_active_pid); break; default: puts("UNEXPECTED MESSAGE TYPE!"); } } return NULL; }
int _tftp_init_ctxt(ipv6_addr_t *addr, const char *file_name, tftp_opcodes_t op, tftp_mode_t mode, tftp_context_type type, tftp_start_cb_t start, tftp_stop_cb_t stop, tftp_data_cb_t data, bool enable_options, tftp_context_t *ctxt) { if (!addr) { return TS_FAILED; } memset(ctxt, 0, sizeof(*ctxt)); /* set the default context parameters */ ctxt->op = op; ctxt->ct = type; ctxt->data_cb = data; ctxt->start_cb = start; ctxt->stop_cb = stop; memcpy(&(ctxt->peer), addr, sizeof(ctxt->peer)); ctxt->mode = mode; if (file_name) { strncpy(ctxt->file_name, file_name, GNRC_TFTP_MAX_FILENAME_LEN); } ctxt->file_name[GNRC_TFTP_MAX_FILENAME_LEN - 1] = 0; ctxt->dst_port = GNRC_TFTP_DEFAULT_DST_PORT; ctxt->enable_options = enable_options; /* transport layer parameters */ ctxt->block_size = GNRC_TFTP_MAX_TRANSFER_UNIT; ctxt->block_timeout = GNRC_TFTP_DEFAULT_TIMEOUT; ctxt->write_finished = false; /* generate a random source UDP source port */ do { ctxt->src_port = (random_uint32() & 0xff) + GNRC_TFTP_DEFAULT_SRC_PORT; } while (gnrc_netreg_num(GNRC_NETTYPE_UDP, ctxt->src_port)); return TS_FINISHED; }
int main() { int i; int limit; char seed[128]; random_init(seed); /* Call random_uint32 a bunch of times and make sure it doesn't return a * value greater than the limit specified, and that it does return the limit * specified. */ limit = 0; for (i = 0; i < 2048; i++) { int val = random_uint32() % 12; if (val >= 12) return 1; else if (val == 11) limit = 1; } if (limit == 0) return 1; return 0; }
/** * @brief FSM handling function for opening a TCP connection. * * @param[in,out] tcb TCB holding the connection information. * * @returns Zero on success. * -ENOMEM if receive buffer could not be allocated. * -EADDRINUSE if given local port number is already in use. */ static int _fsm_call_open(gnrc_tcp_tcb_t *tcb) { int ret = 0; DEBUG("gnrc_tcp_fsm.c : _fsm_call_open()\n"); tcb->rcv_wnd = GNRC_TCP_DEFAULT_WINDOW; if (tcb->status & STATUS_PASSIVE) { /* Passive open, T: CLOSED -> LISTEN */ if (_transition_to(tcb, FSM_STATE_LISTEN) == -ENOMEM) { _transition_to(tcb, FSM_STATE_CLOSED); return -ENOMEM; } } else { /* Active Open, set TCB values, send SYN, T: CLOSED -> SYN_SENT */ tcb->iss = random_uint32(); tcb->snd_nxt = tcb->iss; tcb->snd_una = tcb->iss; /* Transition FSM to SYN_SENT */ ret = _transition_to(tcb, FSM_STATE_SYN_SENT); if (ret < 0) { _transition_to(tcb, FSM_STATE_CLOSED); return ret; } /* Send SYN */ gnrc_pktsnip_t *out_pkt = NULL; uint16_t seq_con = 0; _pkt_build(tcb, &out_pkt, &seq_con, MSK_SYN, tcb->iss, 0, NULL, 0); _pkt_setup_retransmit(tcb, out_pkt, false); _pkt_send(tcb, out_pkt, seq_con, false); } return ret; }
/* Takes care of necessary 'sw' activity, except for receiving packets (which * the caller must do). */ void lswitch_run(struct lswitch *sw, struct rconn *rconn) { long long int now = time_msec(); if (sw->ml) { mac_learning_run(sw->ml, NULL); } /* If we're waiting for more replies, keeping waiting for up to 10 s. */ if (sw->last_reply != LLONG_MIN) { if (now - sw->last_reply > 10000) { VLOG_ERR_RL(&rl, "%012llx: No more flow stat replies last 10 s", sw->datapath_id); sw->last_reply = LLONG_MIN; sw->last_query = LLONG_MIN; schedule_query(sw, 0); } else { return; } } /* If we're waiting for any reply at all, keep waiting for up to 10 s. */ if (sw->last_query != LLONG_MIN) { if (now - sw->last_query > 10000) { VLOG_ERR_RL(&rl, "%012llx: No flow stat replies in last 10 s", sw->datapath_id); sw->last_query = LLONG_MIN; schedule_query(sw, 0); } else { return; } } /* If it's time to send another query, do so. */ if (sw->next_query != LLONG_MIN && now >= sw->next_query) { sw->next_query = LLONG_MIN; if (!rconn_is_connected(rconn)) { schedule_query(sw, 1000); } else { struct ofp_stats_request *osr; struct ofp_flow_stats_request *ofsr; struct ofpbuf *b; int error; VLOG_DBG("%012llx: Sending flow stats request to implement STP", sw->datapath_id); sw->last_query = now; sw->query_xid = random_uint32(); sw->n_flows = 0; sw->n_no_recv = 0; sw->n_no_send = 0; osr = make_openflow_xid(sizeof *osr + sizeof *ofsr, OFPT_STATS_REQUEST, sw->query_xid, &b); osr->type = htons(OFPST_FLOW); osr->flags = htons(0); ofsr = (struct ofp_flow_stats_request *) osr->body; ofsr->match.wildcards = htonl(OFPFW_ALL); ofsr->table_id = 0xff; ofsr->out_port = htons(OFPP_NONE); error = rconn_send(rconn, b, NULL); if (error) { VLOG_WARN_RL(&rl, "%012llx: sending flow stats request " "failed: %s", sw->datapath_id, strerror(error)); ofpbuf_delete(b); schedule_query(sw, 1000); } } } }
static void do_sleep(int factor) { uint32_t timeout_us = (random_uint32() % 100000) * factor; /* PRINTF("sleep for % 8i µs.", timeout_us); */ xtimer_usleep(timeout_us); }
int random_range(int max) { return random_uint32() % max; }
int main(void) { const struct test_case *tc; int i; for (tc = test_cases; tc < &test_cases[ARRAY_SIZE(test_cases)]; tc++) { const uint16_t *data16 = (const uint16_t *) tc->data; const uint32_t *data32 = (const uint32_t *) tc->data; uint32_t partial; size_t i; /* Test csum(). */ assert(ntohs(csum(tc->data, tc->size)) == tc->csum); mark('.'); /* Test csum_add16(). */ partial = 0; for (i = 0; i < tc->size / 2; i++) { partial = csum_add16(partial, data16[i]); } assert(ntohs(csum_finish(partial)) == tc->csum); mark('.'); /* Test csum_add32(). */ partial = 0; for (i = 0; i < tc->size / 4; i++) { partial = csum_add32(partial, data32[i]); } assert(ntohs(csum_finish(partial)) == tc->csum); mark('.'); /* Test alternating csum_add16() and csum_add32(). */ partial = 0; for (i = 0; i < tc->size / 4; i++) { if (i % 2) { partial = csum_add32(partial, data32[i]); } else { partial = csum_add16(partial, data16[i * 2]); partial = csum_add16(partial, data16[i * 2 + 1]); } } assert(ntohs(csum_finish(partial)) == tc->csum); mark('.'); /* Test csum_continue(). */ partial = 0; for (i = 0; i < tc->size / 4; i++) { if (i) { partial = csum_continue(partial, &data32[i], 4); } else { partial = csum_continue(partial, &data16[i * 2], 2); partial = csum_continue(partial, &data16[i * 2 + 1], 2); } } assert(ntohs(csum_finish(partial)) == tc->csum); mark('#'); } test_rfc1624(); /* Test recalc_csum16(). */ for (i = 0; i < 32; i++) { uint16_t old_u16, new_u16; uint16_t old_csum; uint16_t data[16]; int j, index; for (j = 0; j < ARRAY_SIZE(data); j++) { data[j] = random_uint32(); } old_csum = csum(data, sizeof data); index = random_range(ARRAY_SIZE(data)); old_u16 = data[index]; new_u16 = data[index] = random_uint32(); assert(csum(data, sizeof data) == recalc_csum16(old_csum, old_u16, new_u16)); mark('.'); } mark('#'); /* Test recalc_csum32(). */ for (i = 0; i < 32; i++) { uint32_t old_u32, new_u32; uint16_t old_csum; uint32_t data[16]; int j, index; for (j = 0; j < ARRAY_SIZE(data); j++) { data[j] = random_uint32(); } old_csum = csum(data, sizeof data); index = random_range(ARRAY_SIZE(data)); old_u32 = data[index]; new_u32 = data[index] = random_uint32(); assert(csum(data, sizeof data) == recalc_csum32(old_csum, old_u32, new_u32)); mark('.'); } mark('#'); putchar('\n'); return 0; }
uint8_t random_uint8(void) { return random_uint32(); }
uint16_t random_uint16(void) { return random_uint32(); }
int main(void) { const struct test_case *tc; int i; for (tc = test_cases; tc < &test_cases[ARRAY_SIZE(test_cases)]; tc++) { const void *data = tc->data; const ovs_be16 *data16 = (OVS_FORCE const ovs_be16 *) data; const ovs_be32 *data32 = (OVS_FORCE const ovs_be32 *) data; uint32_t partial; /* Test csum(). */ assert(ntohs(csum(tc->data, tc->size)) == tc->csum); mark('.'); /* Test csum_add16(). */ partial = 0; for (i = 0; i < tc->size / 2; i++) { partial = csum_add16(partial, get_unaligned_be16(&data16[i])); } assert(ntohs(csum_finish(partial)) == tc->csum); mark('.'); /* Test csum_add32(). */ partial = 0; for (i = 0; i < tc->size / 4; i++) { partial = csum_add32(partial, get_unaligned_be32(&data32[i])); } assert(ntohs(csum_finish(partial)) == tc->csum); mark('.'); /* Test alternating csum_add16() and csum_add32(). */ partial = 0; for (i = 0; i < tc->size / 4; i++) { if (i % 2) { partial = csum_add32(partial, get_unaligned_be32(&data32[i])); } else { ovs_be16 u0 = get_unaligned_be16(&data16[i * 2]); ovs_be16 u1 = get_unaligned_be16(&data16[i * 2 + 1]); partial = csum_add16(partial, u0); partial = csum_add16(partial, u1); } } assert(ntohs(csum_finish(partial)) == tc->csum); mark('.'); /* Test csum_continue(). */ partial = 0; for (i = 0; i < tc->size / 4; i++) { if (i) { partial = csum_continue(partial, &data32[i], 4); } else { partial = csum_continue(partial, &data16[i * 2], 2); partial = csum_continue(partial, &data16[i * 2 + 1], 2); } } assert(ntohs(csum_finish(partial)) == tc->csum); mark('#'); } test_rfc1624(); /* Test recalc_csum16(). */ for (i = 0; i < 32; i++) { ovs_be16 old_u16, new_u16; ovs_be16 old_csum; ovs_be16 data[16]; int j, index; for (j = 0; j < ARRAY_SIZE(data); j++) { data[j] = (OVS_FORCE ovs_be16) random_uint32(); } old_csum = csum(data, sizeof data); index = random_range(ARRAY_SIZE(data)); old_u16 = data[index]; new_u16 = data[index] = (OVS_FORCE ovs_be16) random_uint32(); assert(csum(data, sizeof data) == recalc_csum16(old_csum, old_u16, new_u16)); mark('.'); } mark('#'); /* Test recalc_csum32(). */ for (i = 0; i < 32; i++) { ovs_be32 old_u32, new_u32; ovs_be16 old_csum; ovs_be32 data[16]; int j, index; for (j = 0; j < ARRAY_SIZE(data); j++) { data[j] = (OVS_FORCE ovs_be32) random_uint32(); } old_csum = csum(data, sizeof data); index = random_range(ARRAY_SIZE(data)); old_u32 = data[index]; new_u32 = data[index] = (OVS_FORCE ovs_be32) random_uint32(); assert(csum(data, sizeof data) == recalc_csum32(old_csum, old_u32, new_u32)); mark('.'); } mark('#'); putchar('\n'); return 0; }
/* XXX we should really use consecutive xids to avoid probabilistic * failures. */ static inline uint32_t alloc_xid(void) { return random_uint32(); }