1809f0800SChristian Ehrhardt/*-
2809f0800SChristian Ehrhardt *   BSD LICENSE
3809f0800SChristian Ehrhardt *
4809f0800SChristian Ehrhardt *   Copyright(c) 2016 RehiveTech. All rights reserved.
5809f0800SChristian Ehrhardt *   All rights reserved.
6809f0800SChristian Ehrhardt *
7809f0800SChristian Ehrhardt *   Redistribution and use in source and binary forms, with or without
8809f0800SChristian Ehrhardt *   modification, are permitted provided that the following conditions
9809f0800SChristian Ehrhardt *   are met:
10809f0800SChristian Ehrhardt *
11809f0800SChristian Ehrhardt *     * Redistributions of source code must retain the above copyright
12809f0800SChristian Ehrhardt *       notice, this list of conditions and the following disclaimer.
13809f0800SChristian Ehrhardt *     * Redistributions in binary form must reproduce the above copyright
14809f0800SChristian Ehrhardt *       notice, this list of conditions and the following disclaimer in
15809f0800SChristian Ehrhardt *       the documentation and/or other materials provided with the
16809f0800SChristian Ehrhardt *       distribution.
17809f0800SChristian Ehrhardt *     * Neither the name of RehiveTech nor the names of its
18809f0800SChristian Ehrhardt *       contributors may be used to endorse or promote products derived
19809f0800SChristian Ehrhardt *       from this software without specific prior written permission.
20809f0800SChristian Ehrhardt *
21809f0800SChristian Ehrhardt *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22809f0800SChristian Ehrhardt *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23809f0800SChristian Ehrhardt *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24809f0800SChristian Ehrhardt *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25809f0800SChristian Ehrhardt *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26809f0800SChristian Ehrhardt *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27809f0800SChristian Ehrhardt *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28809f0800SChristian Ehrhardt *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29809f0800SChristian Ehrhardt *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30809f0800SChristian Ehrhardt *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31809f0800SChristian Ehrhardt *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32809f0800SChristian Ehrhardt */
33809f0800SChristian Ehrhardt
34809f0800SChristian Ehrhardt#ifndef _RESOURCE_H_
35809f0800SChristian Ehrhardt#define _RESOURCE_H_
36809f0800SChristian Ehrhardt
37809f0800SChristian Ehrhardt/**
38809f0800SChristian Ehrhardt * @file
39809f0800SChristian Ehrhardt *
40809f0800SChristian Ehrhardt * Test Resource API
41809f0800SChristian Ehrhardt *
42809f0800SChristian Ehrhardt * Each test can require and use some external resources. Usually, an external
43809f0800SChristian Ehrhardt * resource is a file or a filesystem sub-hierarchy. A resource is included
44809f0800SChristian Ehrhardt * inside the test executable.
45809f0800SChristian Ehrhardt */
46809f0800SChristian Ehrhardt
47809f0800SChristian Ehrhardt#include <sys/queue.h>
48809f0800SChristian Ehrhardt#include <stdio.h>
49809f0800SChristian Ehrhardt#include <stddef.h>
50809f0800SChristian Ehrhardt
51809f0800SChristian Ehrhardt#include <rte_eal.h>
52809f0800SChristian Ehrhardt#include <rte_common.h>
53809f0800SChristian Ehrhardt
54809f0800SChristian EhrhardtTAILQ_HEAD(resource_list, resource);
55809f0800SChristian Ehrhardtextern struct resource_list resource_list;
56809f0800SChristian Ehrhardt
57809f0800SChristian Ehrhardt/**
58809f0800SChristian Ehrhardt * Representation of a resource. It points to the resource's binary data.
59809f0800SChristian Ehrhardt * The semantics of the binary data are defined by the target test.
60809f0800SChristian Ehrhardt */
61809f0800SChristian Ehrhardtstruct resource {
62809f0800SChristian Ehrhardt	const char *name;  /**< Unique name of the resource */
63809f0800SChristian Ehrhardt	const char *begin; /**< Start of resource data */
64809f0800SChristian Ehrhardt	const char *end;   /**< End of resource data */
65809f0800SChristian Ehrhardt	TAILQ_ENTRY(resource) next;
66809f0800SChristian Ehrhardt};
67809f0800SChristian Ehrhardt
68809f0800SChristian Ehrhardt/**
69809f0800SChristian Ehrhardt * @return size of the given resource
70809f0800SChristian Ehrhardt */
71809f0800SChristian Ehrhardtsize_t resource_size(const struct resource *r);
72809f0800SChristian Ehrhardt
73809f0800SChristian Ehrhardt/**
74809f0800SChristian Ehrhardt * Find a resource by name in the global list of resources.
75809f0800SChristian Ehrhardt */
76809f0800SChristian Ehrhardtconst struct resource *resource_find(const char *name);
77809f0800SChristian Ehrhardt
78809f0800SChristian Ehrhardt/**
79809f0800SChristian Ehrhardt * Write the raw data of the resource to the given file.
80809f0800SChristian Ehrhardt * @return 0 on success
81809f0800SChristian Ehrhardt */
82809f0800SChristian Ehrhardtint resource_fwrite(const struct resource *r, FILE *f);
83809f0800SChristian Ehrhardt
84809f0800SChristian Ehrhardt/**
85809f0800SChristian Ehrhardt * Write the raw data of the resource to the given file given by name.
86809f0800SChristian Ehrhardt * The name is relative to the current working directory.
87809f0800SChristian Ehrhardt * @return 0 on success
88809f0800SChristian Ehrhardt */
89809f0800SChristian Ehrhardtint resource_fwrite_file(const struct resource *r, const char *fname);
90809f0800SChristian Ehrhardt
91809f0800SChristian Ehrhardt/**
92809f0800SChristian Ehrhardt * Treat the given resource as a tar archive. Extract
93809f0800SChristian Ehrhardt * the archive to the current directory.
94809f0800SChristian Ehrhardt */
95809f0800SChristian Ehrhardtint resource_untar(const struct resource *res);
96809f0800SChristian Ehrhardt
97809f0800SChristian Ehrhardt/**
98809f0800SChristian Ehrhardt * Treat the given resource as a tar archive. Remove
99809f0800SChristian Ehrhardt * all files (related to the current directory) listed
100809f0800SChristian Ehrhardt * in the tar archive.
101809f0800SChristian Ehrhardt */
102809f0800SChristian Ehrhardtint resource_rm_by_tar(const struct resource *res);
103809f0800SChristian Ehrhardt
104809f0800SChristian Ehrhardt/**
105809f0800SChristian Ehrhardt * Register a resource in the global list of resources.
106809f0800SChristian Ehrhardt * Not intended for direct use, please check the REGISTER_RESOURCE
107809f0800SChristian Ehrhardt * macro.
108809f0800SChristian Ehrhardt */
109809f0800SChristian Ehrhardtvoid resource_register(struct resource *r);
110809f0800SChristian Ehrhardt
111809f0800SChristian Ehrhardt/**
112809f0800SChristian Ehrhardt * Definition of a resource linked externally (by means of the used toolchain).
113809f0800SChristian Ehrhardt * Only the base name of the resource is expected. The name refers to the
114809f0800SChristian Ehrhardt * linked pointers beg_<name> and end_<name> provided externally.
115809f0800SChristian Ehrhardt */
116809f0800SChristian Ehrhardt#define REGISTER_LINKED_RESOURCE(n) \
117809f0800SChristian Ehrhardtextern const char beg_ ##n;         \
118809f0800SChristian Ehrhardtextern const char end_ ##n;         \
119809f0800SChristian EhrhardtREGISTER_RESOURCE(n, &beg_ ##n, &end_ ##n) \
120809f0800SChristian Ehrhardt
121809f0800SChristian Ehrhardt/**
122809f0800SChristian Ehrhardt * Definition of a resource described by its name, and pointers begin, end.
123809f0800SChristian Ehrhardt */
124809f0800SChristian Ehrhardt#define REGISTER_RESOURCE(n, b, e) \
125809f0800SChristian Ehrhardtstatic struct resource linkres_ ##n = {       \
126809f0800SChristian Ehrhardt	.name = RTE_STR(n),     \
127809f0800SChristian Ehrhardt	.begin = b,             \
128809f0800SChristian Ehrhardt	.end = e,               \
129809f0800SChristian Ehrhardt};                              \
130809f0800SChristian Ehrhardtstatic void __attribute__((constructor, used)) resinitfn_ ##n(void) \
131809f0800SChristian Ehrhardt{                               \
132809f0800SChristian Ehrhardt	resource_register(&linkres_ ##n);  \
133809f0800SChristian Ehrhardt}
134809f0800SChristian Ehrhardt
135809f0800SChristian Ehrhardt#endif
136