summaryrefslogtreecommitdiffstats
path: root/tests/test_lblmgr.c
blob: 652d7618a8665809411983ad4bbe438200550833 (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
/*
 * Label Manager Test
 *
 * Copyright (C) 2017 by Bingen Eguzkitza,
 *                       Volta Networks Inc.
 *
 * This file is part of FreeRangeRouting (FRR)
 *
 * FRR is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2, or (at your option) any
 * later version.
 *
 * FRR is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; see the file COPYING; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "lib/stream.h"
#include "lib/zclient.h"

#define ZSERV_PATH "/tmp/zserv.api" // TODO!!
#define KEEP 0 /* change to 1 to avoid garbage collection */
#define CHUNK_SIZE 32

struct zclient *zclient;
unsigned short instance = 1;

const char *sequence = "GGRGGGRRG";

static int zebra_send_get_label_chunk(void);
static int zebra_send_release_label_chunk(uint32_t start, uint32_t end);

static void process_next_call(uint32_t start, uint32_t end)
{
	sleep(3);
	if (!*sequence)
		exit(0);
	if (*sequence == 'G')
		zebra_send_get_label_chunk();
	else if (*sequence == 'R')
		zebra_send_release_label_chunk(start, end);
}

/* Connect to Label Manager */

static int zebra_send_label_manager_connect()
{
	int ret;

	printf("Connect to Label Manager\n");

	ret = lm_label_manager_connect(zclient);
	printf("Label Manager connection result: %u \n", ret);
	if (ret != 0) {
		fprintf(stderr, "Error %d connecting to Label Manager %s\n",
			ret, strerror(errno));
		exit(1);
	}

	process_next_call(0, 0);
}

/* Get Label Chunk */

static int zebra_send_get_label_chunk()
{
	uint32_t start;
	uint32_t end;
	int ret;

	printf("Ask for label chunk \n");

	ret = lm_get_label_chunk(zclient, KEEP, CHUNK_SIZE, &start, &end);
	if (ret != 0) {
		fprintf(stderr, "Error %d requesting label chunk %s\n", ret,
			strerror(errno));
		exit(1);
	}

	sequence++;

	printf("Label Chunk assign: %u - %u \n", start, end);

	process_next_call(start, end);
}

/* Release Label Chunk */

static int zebra_send_release_label_chunk(uint32_t start, uint32_t end)
{
	struct stream *s;
	int ret;

	printf("Release label chunk: %u - %u\n", start, end);

	ret = lm_release_label_chunk(zclient, start, end);
	if (ret != 0) {
		fprintf(stderr, "Error releasing label chunk\n");
		exit(1);
	}

	sequence++;

	process_next_call(start - CHUNK_SIZE, end - CHUNK_SIZE);
}


void init_zclient(struct thread_master *master, char *lm_zserv_path)
{
	frr_zclient_addr(&zclient_addr, &zclient_addr_len, lm_zserv_path);

	zclient = zclient_new_notify(master, &zclient_options_default);
	/* zclient_init(zclient, ZEBRA_LABEL_MANAGER, 0); */
	zclient->sock = -1;
	zclient->redist_default = ZEBRA_ROUTE_LDP;
	zclient->instance = instance;
	if (zclient_socket_connect(zclient) < 0) {
		printf("Error connecting synchronous zclient!\n");
		exit(1);
	}
}

int main(int argc, char *argv[])
{
	struct thread_master *master;
	struct thread thread;
	int ret;

	printf("Sequence to be tested: %s\n", sequence);

	master = thread_master_create(NULL);
	init_zclient(master, ZSERV_PATH);

	zebra_send_label_manager_connect();

	return 0;
}