@anonymous/

ExcitingPresentFile

C

No description

fork
loading
Files
  • main.c
main.c
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
#include <stdio.h>
#include <errno.h>

#define PEERS 5

struct ntb_dev {
        int logical_port_num;
        int peer_port_nums[PEERS-1];
};

static int port_numbers[PEERS] = {0, 1, 4, 5, 72};
//static int port_numbers[PEERS] = {0, 0};

static int ntb_port_number(struct ntb_dev *ntb)
{
	return port_numbers[ntb->logical_port_num];
}

static int ntb_peer_port_count(struct ntb_dev *ntb)
{
	return PEERS - 1;
}

static int ntb_peer_port_number(struct ntb_dev *ntb, int pidx)
{
	return ntb->peer_port_nums[pidx];
}

static inline int ntb_logical_port_number(struct ntb_dev *ntb)
{
	int lport = ntb_port_number(ntb);
	int pidx;

	if (lport < 0)
		return lport;

	for (pidx = 0; pidx < ntb_peer_port_count(ntb); pidx++)
		if (lport <= ntb_peer_port_number(ntb, pidx))
			return pidx;

	return pidx;
}

static inline int ntb_peer_logical_port_number(struct ntb_dev *ntb, int pidx)
{
	if (ntb_peer_port_number(ntb, pidx) < ntb_port_number(ntb))
		return pidx;
	else
		return pidx + 1;
}

static inline int ntb_peer_resource_idx(struct ntb_dev *ntb, int pidx)
{
	int local_port, peer_port;

	if (pidx >= ntb_peer_port_count(ntb))
		return -EINVAL;

	local_port = ntb_logical_port_number(ntb);
	peer_port = ntb_peer_logical_port_number(ntb, pidx);

	if (peer_port < local_port)
		return local_port - 1;
	else
		return local_port;
}

static void setup_ntb(struct ntb_dev *ntb, int logical_port_num)
{
	int i, j;

	ntb->logical_port_num = logical_port_num;

	for (i = j = 0; i < PEERS; i++)
		if (i != logical_port_num)
			ntb->peer_port_nums[j++] = port_numbers[i];
}

int main(void)
{
	struct ntb_dev ntbs[PEERS];
	int i, j;
	
	for (i = 0; i < PEERS; i++)
		setup_ntb(&ntbs[i], i);

	for (i = 0; i < PEERS; i++) {
		printf("Peer %d - %d\n", i, port_numbers[i]);
		if (ntb_port_number(&ntbs[i]) != port_numbers[i])
		printf(" --- Incorrect Port Number %d %d\n",
			ntb_port_number(&ntbs[i]), port_numbers[i]);

		printf("  Peer Index:\t\t\t");
		for (j = 0; j < ntb_peer_port_count(&ntbs[i]); j++)
			printf("%2d  ", j);
		printf("\n");

		printf("  Port Number:\t\t\t");
		for (j = 0; j < ntb_peer_port_count(&ntbs[i]); j++)
			printf("%2d  ", ntb_peer_port_number(&ntbs[i], j));
		printf("\n");

		printf("  Logical Port Number:\t");
		for (j = 0; j < ntb_peer_port_count(&ntbs[i]); j++) {
			int log = ntb_peer_logical_port_number(&ntbs[i], j);
			int good = (port_numbers[log] == 
				          ntb_peer_port_number(&ntbs[i], j));

		printf("%2d%c ", ntb_peer_logical_port_number(&ntbs[i], j),
		       good ? ' ' : '*');
	}
	printf("\n");
	
	printf("  Peer Resource Idx:\t");
	for (j = 0; j < ntb_peer_port_count(&ntbs[i]); j++)
		printf("%2d  ", ntb_peer_resource_idx(&ntbs[i], j));
	printf("\n");

	}
}