summaryrefslogtreecommitdiff
path: root/ipacm/src/IPACM_Header.cpp
blob: c77c69c45e2ac76ad4681d6a0cd6433e51dc0602 (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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
Copyright (c) 2013, The Linux Foundation. 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 The Linux Foundation 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 "AS IS" AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
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 <sys/ioctl.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>

#include "IPACM_Header.h"
#include "IPACM_Log.h"

/////////////////////////////////////////////////////////////////////////////////////////////////////////

//All interaction through the driver are made through this inode.
static const char *DEVICE_NAME = "/dev/ipa";

/////////////////////////////////////////////////////////////////////////////////////////////////////////

IPACM_Header::IPACM_Header()
{
	m_fd = open(DEVICE_NAME, O_RDWR);
	if (-1 == m_fd)
	{
		IPACMERR("Failed to open %s in IPACM_Header test application constructor.\n", DEVICE_NAME);
	}
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////

IPACM_Header::~IPACM_Header()
{
	if (-1 != m_fd)
	{
		close(m_fd);
	}
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////

bool IPACM_Header::DeviceNodeIsOpened()
{
	return (-1 != m_fd);
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////

bool IPACM_Header::AddHeader(struct ipa_ioc_add_hdr *pHeaderTableToAdd)
{
	int nRetVal = 0;
	//call the Driver ioctl in order to add header
	nRetVal = ioctl(m_fd, IPA_IOC_ADD_HDR, pHeaderTableToAdd);
	IPACMDBG("return value: %d\n", nRetVal);
	return (-1 != nRetVal);
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////

bool IPACM_Header::DeleteHeader(struct ipa_ioc_del_hdr *pHeaderTableToDelete)
{
	int nRetVal = 0;
	//call the Driver ioctl in order to remove header
	nRetVal = ioctl(m_fd, IPA_IOC_DEL_HDR, pHeaderTableToDelete);
	IPACMDBG("return value: %d\n", nRetVal);
	return (-1 != nRetVal);
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////

bool IPACM_Header::Commit()
{
	int nRetVal = 0;
	nRetVal = ioctl(m_fd, IPA_IOC_COMMIT_HDR);
	IPACMDBG("return value: %d\n", nRetVal);
	return true;
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////

bool IPACM_Header::Reset()
{
	int nRetVal = 0;

	nRetVal = ioctl(m_fd, IPA_IOC_RESET_HDR);
	nRetVal |= ioctl(m_fd, IPA_IOC_COMMIT_HDR);
	IPACMDBG("return value: %d\n", nRetVal);
	return true;
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////

bool IPACM_Header::GetHeaderHandle(struct ipa_ioc_get_hdr *pHeaderStruct)
{
	int retval = 0;

	if (!DeviceNodeIsOpened()) return false;

	retval = ioctl(m_fd, IPA_IOC_GET_HDR, pHeaderStruct);
	if (retval)
	{
		IPACMERR("IPA_IOC_GET_HDR ioctl failed, routingTable =0x%p, retval=0x%x.\n", pHeaderStruct, retval);
		return false;
	}

	IPACMDBG("IPA_IOC_GET_HDR ioctl issued to IPA header insertion block.\n");
	return true;
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////

bool IPACM_Header::CopyHeader(struct ipa_ioc_copy_hdr *pCopyHeaderStruct)
{
	int retval = 0;

	if (!DeviceNodeIsOpened()) return false;

	retval = ioctl(m_fd, IPA_IOC_COPY_HDR, pCopyHeaderStruct);
	if (retval)
	{
		IPACMERR("IPA_IOC_COPY_HDR ioctl failed, retval=0x%x.\n", retval);
		return false;
	}

	IPACMDBG("IPA_IOC_COPY_HDR ioctl issued to IPA header insertion block.\n");
	return true;
}

bool IPACM_Header::DeleteHeaderHdl(uint32_t hdr_hdl)
{
	const uint8_t NUM_HDLS = 1;
	struct ipa_ioc_del_hdr *pHeaderDescriptor = NULL;
	struct ipa_hdr_del *hd_rule_entry;
	int len = 0;
	bool res = true;

	if (hdr_hdl == 0)
	{
		IPACMERR("Invalid header handle passed. Ignoring it\n");
		return false;
	}

	len = (sizeof(struct ipa_ioc_del_hdr)) + (NUM_HDLS * sizeof(struct ipa_hdr_del));
	pHeaderDescriptor = (struct ipa_ioc_del_hdr *)malloc(len);
	if (pHeaderDescriptor == NULL)
	{
		IPACMERR("Unable to allocate memory for del header\n");
		return false;
	}

	memset(pHeaderDescriptor, 0, len);
	pHeaderDescriptor->commit = true;
	pHeaderDescriptor->num_hdls = NUM_HDLS;
	hd_rule_entry = &pHeaderDescriptor->hdl[0];

	hd_rule_entry->hdl = hdr_hdl;
	hd_rule_entry->status = -1;

	IPACMDBG("Deleting Header hdl:(%x)\n", hd_rule_entry->hdl);
	if ((false == DeleteHeader(pHeaderDescriptor)) ||
			(hd_rule_entry->status))
	{
	    IPACMERR("Header hdl:(%x) deletion failed!  status: %d\n", hd_rule_entry->hdl,hd_rule_entry->status);
		res = false;
		goto fail;
	}

	IPACMDBG_H("Deleted Header hdl:(%x) successfully\n", hd_rule_entry->hdl);

fail:
	free(pHeaderDescriptor);

	return res;

}

bool IPACM_Header::AddHeaderProcCtx(struct ipa_ioc_add_hdr_proc_ctx* pHeader)
{
	int ret = 0;
	//call the Driver ioctl to add header processing context
	ret = ioctl(m_fd, IPA_IOC_ADD_HDR_PROC_CTX, pHeader);
	return (ret == 0);
}

bool IPACM_Header::DeleteHeaderProcCtx(uint32_t hdl)
{
	int len, ret;
	struct ipa_ioc_del_hdr_proc_ctx* pHeaderTable = NULL;

	len = sizeof(struct ipa_ioc_del_hdr_proc_ctx) + sizeof(struct ipa_hdr_proc_ctx_del);
	pHeaderTable = (struct ipa_ioc_del_hdr_proc_ctx*)malloc(len);
	if(pHeaderTable == NULL)
	{
		IPACMERR("Failed to allocate buffer.\n");
		return false;
	}
	memset(pHeaderTable, 0, len);

	pHeaderTable->commit = 1;
	pHeaderTable->num_hdls = 1;
	pHeaderTable->hdl[0].hdl = hdl;

	ret = ioctl(m_fd, IPA_IOC_DEL_HDR_PROC_CTX, pHeaderTable);
	if(ret != 0)
	{
		IPACMERR("Failed to delete hdr proc ctx: return value %d, status %d\n",
			ret, pHeaderTable->hdl[0].status);
	}
	free(pHeaderTable);
	return (ret == 0);
}