aboutsummaryrefslogtreecommitdiff
path: root/test/linux-generic/pktio_ipc/ipc_common.c
blob: 85cbc8b418400ceb10e521aece4686d3d50b8985 (plain)
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/* Copyright (c) 2015, Linaro Limited
 * All rights reserved.
 *
 * SPDX-License-Identifier:     BSD-3-Clause
 */

#include "ipc_common.h"

/** Run time in seconds */
int run_time_sec;
/** Pid of the master process */
int master_pid;

int ipc_odp_packet_send_or_free(odp_pktio_t pktio,
				odp_packet_t pkt_tbl[], int num)
{
	int ret;
	int sent = 0;
	odp_time_t start_time;
	odp_time_t end_time;
	odp_time_t wait;
	odp_pktout_queue_t pktout;
	int i;

	start_time = odp_time_local();
	wait = odp_time_local_from_ns(ODP_TIME_SEC_IN_NS);
	end_time = odp_time_sum(start_time, wait);

	if (odp_pktout_queue(pktio, &pktout, 1) != 1) {
		EXAMPLE_ERR("no output queue\n");
		return -1;
	}

	while (sent != num) {
		ret = odp_pktout_send(pktout, &pkt_tbl[sent], num - sent);
		if (ret < 0) {
			EXAMPLE_ERR("odp_pktout_send return %d\n", ret);
			for (i = sent; i < num; i++)
				odp_packet_free(pkt_tbl[i]);
			return -1;
		}

		sent += ret;

		if (odp_time_cmp(end_time, odp_time_local()) < 0) {
			for (i = sent; i < num; i++)
				odp_packet_free(pkt_tbl[i]);
			EXAMPLE_ERR("Send Timeout!\n");
			return -1;
		}
	}

	return 0;
}

odp_pktio_t create_pktio(odp_pool_t pool, int master_pid)
{
	odp_pktio_param_t pktio_param;
	odp_pktio_t ipc_pktio;
	char name[30];

	odp_pktio_param_init(&pktio_param);

	if (master_pid)
		sprintf(name, TEST_IPC_PKTIO_PID_NAME, master_pid);
	else
		sprintf(name, TEST_IPC_PKTIO_NAME);

	printf("pid: %d, create IPC pktio %s\n", getpid(), name);
	ipc_pktio = odp_pktio_open(name, pool, &pktio_param);
	if (ipc_pktio == ODP_PKTIO_INVALID) {
		EXAMPLE_ERR("Error: ipc pktio %s create failed.\n", name);
		return ODP_PKTIO_INVALID;
	}

	if (odp_pktin_queue_config(ipc_pktio, NULL)) {
		EXAMPLE_ERR("Input queue config failed\n");
		return ODP_PKTIO_INVALID;
	}

	if (odp_pktout_queue_config(ipc_pktio, NULL)) {
		EXAMPLE_ERR("Output queue config failed\n");
		return ODP_PKTIO_INVALID;
	}

	return ipc_pktio;
}

/**
 * Parse and store the command line arguments
 *
 * @param argc       argument count
 * @param argv[]     argument vector
 * @param appl_args  Store application arguments here
 */
void parse_args(int argc, char *argv[])
{
	int opt;
	int long_index;
	static struct option longopts[] = {
		{"time", required_argument, NULL, 't'},
		{"pid", required_argument, NULL, 'p'}, /* master process pid */
		{"help", no_argument, NULL, 'h'},     /* return 'h' */
		{NULL, 0, NULL, 0}
	};

	run_time_sec = 0; /* loop forever if time to run is 0 */
	master_pid = 0;

	while (1) {
		opt = getopt_long(argc, argv, "+t:p:h",
				  longopts, &long_index);

		if (opt == -1)
			break;	/* No more options */

		switch (opt) {
		case 't':
			run_time_sec = atoi(optarg);
			break;
		case 'p':
			master_pid = atoi(optarg);
			break;
		case 'h':
		default:
			usage(argv[0]);
			exit(EXIT_SUCCESS);
			break;
		}
	}

	optind = 1;		/* reset 'extern optind' from the getopt lib */
}

/**
 * Print system and application info
 */
void print_info(char *progname)
{
	printf("\n"
	       "ODP system info\n"
	       "---------------\n"
	       "ODP API version: %s\n"
	       "CPU model:       %s\n"
	       "\n",
	       odp_version_api_str(), odp_cpu_model_str());

	printf("Running ODP appl: \"%s\"\n"
	       "-----------------\n"
	       "Using IF:        %s\n",
	       progname, pktio_name);
	printf("\n\n");
	fflush(NULL);
}

/**
 * Prinf usage information
 */
void usage(char *progname)
{
	printf("\n"
	       "Usage: %s OPTIONS\n"
	       "  E.g. -n ipc_name_space %s -t seconds\n"
	       "\n"
	       "OpenDataPlane odp-linux ipc test application.\n"
	       "\n"
		"Mandatory OPTIONS:\n"
	       "  -n, --ns           IPC name space ID /dev/shm/odp-<ns>-objname.\n"
	       "Optional OPTIONS\n"
	       "  -h, --help           Display help and exit.\n"
	       "  -t, --time           Time to run in seconds.\n"
	       "\n", NO_PATH(progname), NO_PATH(progname)
	    );
}