public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
blob 8d812965f1227e1a9915251903b1d9374e0bf958 3121 bytes (raw)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
 
// SPDX-License-Identifier: GPL-2.0-or-later

/* rampstream - Generate a check and stream of bytes in a ramp pattern
 *
 * Copyright Red Hat
 * Author: David Gibson <david@gibson.dropbear.id.au>
 */

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>

/* Length of the repeating ramp.  This is a deliberately not a "round" number so
 * that we're very likely to misalign with likely block or chunk sizes of the
 * transport.  That means we'll detect gaps in the stream, even if they occur
 * neatly on block boundaries.  Specifically this is the largest 8-bit prime. */
#define RAMPLEN		251

#define INTERVAL	10000

#define	ARRAY_SIZE(a)	((int)(sizeof(a) / sizeof((a)[0])))

#define die(...)						\
	do {							\
		fprintf(stderr, "rampstream: " __VA_ARGS__);	\
		exit(1);					\
	} while (0)

static void usage(void)
{
	die("Usage:\n"
	    "  rampstream send <number>\n"
	    "    Generate a ramp pattern of bytes on stdout, repeated <number>\n"
	    "    times\n"
	    "  rampstream check <number>\n"
	    "    Check a ramp pattern of bytes on stdin, repeater <number>\n"
	    "    times\n");
}

static void ramp_send(unsigned long long num, const uint8_t *ramp)
{
	unsigned long long i;

	for (i = 0; i < num; i++) {
		int off = 0;
		ssize_t rc;

		if (i % INTERVAL == 0)
			fprintf(stderr, "%llu...\r", i);

		while (off < RAMPLEN) {
			rc = write(1, ramp + off, RAMPLEN - off);
			if (rc < 0) {
				if (errno == EINTR ||
				    errno == EAGAIN ||
				    errno == EWOULDBLOCK)
					continue;
				die("Error writing ramp: %s\n",
				    strerror(errno));
			}
			if (rc == 0)
				die("Zero length write\n");
			off += rc;
		}
	}
}

static void ramp_check(unsigned long long num, const uint8_t *ramp)
{
	unsigned long long i;

	for (i = 0; i < num; i++) {
		uint8_t buf[RAMPLEN];
		int off = 0;
		ssize_t rc;

		if (i % INTERVAL == 0)
			fprintf(stderr, "%llu...\r", i);
		
		while (off < RAMPLEN) {
			rc = read(0, buf + off, RAMPLEN - off);
			if (rc < 0) {
				if (errno == EINTR ||
				    errno == EAGAIN ||
				    errno == EWOULDBLOCK)
					continue;
				die("Error reading ramp: %s\n",
				    strerror(errno));
			}
			if (rc == 0)
				die("Unexpected EOF, ramp %llu, byte %d\n",
				    i, off);
			off += rc;
		}

		if (memcmp(buf, ramp, sizeof(buf)) != 0) {
			int j, k;

			for (j = 0; j < RAMPLEN; j++)
				if (buf[j] != ramp[j])
					break;
			for (k = j; k < RAMPLEN && k < j + 16; k++)
				fprintf(stderr,
					"Byte %d: expected 0x%02x, got 0x%02x\n",
					k, ramp[k], buf[k]);
			die("Data mismatch, ramp %llu, byte %d\n", i, j);
		}
	}
}

int main(int argc, char *argv[])
{
	const char *subcmd = argv[1];
	unsigned long long num;
	uint8_t ramp[RAMPLEN];
	char *e;
	int i;

	if (argc < 2)
		usage();

	errno = 0;
	num = strtoull(argv[2], &e, 0);
	if (*e || errno)
		usage();

	/* Initialize the ramp block */
	for (i = 0; i < RAMPLEN; i++)
		ramp[i] = i;

	if (strcmp(subcmd, "send") == 0)
		ramp_send(num, ramp);
	else if (strcmp(subcmd, "check") == 0)
		ramp_check(num, ramp);
	else
		usage();

	exit(0);
}

debug log:

solving 8d812965 ...
found 8d812965 in https://passt.top/passt

Code repositories for project(s) associated with this public inbox

	https://passt.top/passt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for IMAP folder(s).