summaryrefslogtreecommitdiff
path: root/src/vf/command_conn.c
blob: d964ade453ecff9bb8ed6bd3f33372a28d87f960 (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
175
176
177
/*
 *   BSD LICENSE
 *
 *   Copyright(c) 2017 Nippon Telegraph and Telephone Corporation
 *   All rights reserved.
 *
 *   Redistribution and use in source and binary forms, with or without
 *   modification, are permitted provided that the following conditions
 *   are met:
 *
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in
 *       the documentation and/or other materials provided with the
 *       distribution.
 *     * Neither the name of Nippon Telegraph and Telephone Corporation
 *       nor the names of its contributors may be used to endorse or
 *       promote products derived from this software without specific
 *       prior written permission.
 *
 *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <arpa/inet.h>

#include <rte_common.h>
#include <rte_log.h>
#include <rte_branch_prediction.h>

#include "string_buffer.h"
#include "command_conn.h"

#define RTE_LOGTYPE_SPP_COMMAND_PROC RTE_LOGTYPE_USER1

/* one receive message size */
#define MESSAGE_BUFFER_BLOCK_SIZE 2048

/* controller's IP address */
static char g_controller_ip[128] = "";

/* controller's port number */
static int g_controller_port;

/* initialize command connection */
int
spp_command_conn_init(const char *controller_ip, int controller_port)
{
	strcpy(g_controller_ip, controller_ip);
	g_controller_port = controller_port;

	return 0;
}

/* connect to controller */
int
spp_connect_to_controller(int *sock)
{
	static struct sockaddr_in controller_addr;
	int ret = -1;
	int sock_flg = 0;

	if (likely(*sock >= 0))
		return 0;

	/* create socket */
	RTE_LOG(INFO, SPP_COMMAND_PROC, "Creating socket...\n");
	*sock = socket(AF_INET, SOCK_STREAM, 0);
	if (unlikely(*sock < 0)) {
		RTE_LOG(ERR, SPP_COMMAND_PROC,
				"Cannot create TCP socket. errno=%d\n", errno);
		return SPP_CONNERR_TEMPORARY;
	}

	memset(&controller_addr, 0, sizeof(controller_addr));
	controller_addr.sin_family = AF_INET;
	controller_addr.sin_addr.s_addr = inet_addr(g_controller_ip);
	controller_addr.sin_port = htons(g_controller_port);

	/* connect to */
	RTE_LOG(INFO, SPP_COMMAND_PROC, "Trying to connect ... socket=%d\n",
			*sock);
	ret = connect(*sock, (struct sockaddr *)&controller_addr,
			sizeof(controller_addr));
	if (unlikely(ret < 0)) {
		RTE_LOG(ERR, SPP_COMMAND_PROC,
				"Cannot connect to controller. errno=%d\n",
				errno);
		close(*sock);
		*sock = -1;
		return SPP_CONNERR_TEMPORARY;
	}

	RTE_LOG(INFO, SPP_COMMAND_PROC, "Connected\n");

	/* set non-blocking */
	sock_flg = fcntl(*sock, F_GETFL, 0);
	fcntl(*sock, F_SETFL, sock_flg | O_NONBLOCK);

	return 0;
}

/* receive message */
int
spp_receive_message(int *sock, char **strbuf)
{
	int ret = -1;
	int n_rx = 0;
	char *new_strbuf = NULL;

	char rx_buf[MESSAGE_BUFFER_BLOCK_SIZE];
	size_t rx_buf_sz = MESSAGE_BUFFER_BLOCK_SIZE;

	ret = recv(*sock, rx_buf, rx_buf_sz, 0);
	if (unlikely(ret <= 0)) {
		if (likely(ret == 0)) {
			RTE_LOG(INFO, SPP_COMMAND_PROC,
					"Controller has performed an shutdown.");
		} else if (unlikely(errno != EAGAIN && errno != EWOULDBLOCK)) {
			RTE_LOG(ERR, SPP_COMMAND_PROC,
					"Receive failure. errno=%d\n", errno);
		} else {
			/* no receive message */
			return 0;
		}

		RTE_LOG(INFO, SPP_COMMAND_PROC, "Assume Server closed connection.\n");
		close(*sock);
		*sock = -1;
		return SPP_CONNERR_TEMPORARY;
	}

	RTE_LOG(DEBUG, SPP_COMMAND_PROC, "Receive message. count=%d\n", ret);
	n_rx = ret;

	new_strbuf = spp_strbuf_append(*strbuf, rx_buf, n_rx);
	if (unlikely(new_strbuf == NULL)) {
		RTE_LOG(ERR, SPP_COMMAND_PROC,
				"Cannot allocate memory for receive data.\n");
		return SPP_CONNERR_FATAL;
	}

	*strbuf = new_strbuf;

	return n_rx;
}

/* send message */
int
spp_send_message(int *sock, const char *message, size_t message_len)
{
	int ret = -1;

	ret = send(*sock, message, message_len, 0);
	if (unlikely(ret == -1)) {
		RTE_LOG(ERR, SPP_COMMAND_PROC, "Send failure. ret=%d\n", ret);
		close(*sock);
		*sock = -1;
		return SPP_CONNERR_TEMPORARY;
	}

	return 0;
}