/* * Copyright (c) 2015 Cisco and/or its affiliates. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at: * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /* Copyright (c) 2001, 2002, 2003 Eliot Dresselhaus Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #ifndef included_vec_h #define included_vec_h #include /* word, etc */ #include /* clib_mem_free */ #include /* memcpy, memmove */ #include /** \file CLIB vectors are ubiquitous dynamically resized arrays with by user defined "headers". Many CLIB data structures (e.g. hash, heap, pool) are vectors with various different headers. The memory layout looks like this: ~~~~~~~~ user header (aligned to uword boundary) vector length: number of elements user's pointer-> vector element #0 vector element #1 ... ~~~~~~~~ The user pointer contains the address of vector element # 0. Null pointer vectors are valid and mean a zero length vector. You can reset the length of an allocated vector to zero via the vec_reset_length(v) macro, or by setting the vector length field to zero (e.g. _vec_len (v) = 0). Vec_reset_length(v) preferred: it understands Null pointers. Typically, the header is not present. Headers allow for other data structures to be built atop CLIB vectors. Users may specify the alignment for first data element of a vector via the vec_*_aligned macros. Vector elements can be any C type e.g. (int, double, struct bar). This is also true for data types built atop vectors (e.g. heap, pool, etc.). Many macros have \_a variants supporting alignment of vector elements and \_h variants supporting non-zero-length vector headers. The \_ha variants support both. Additionally cacheline alignment within a vector element structure can be specified using the CLIB_CACHE_LINE_ALIGN_MARK() macro. Standard programming error: memorize a pointer to the ith element of a vector then expand it. Vectors expand by 3/2, so such code may appear to work for a period of time. Memorize vector indices which are invariant. */ /** \brief Low-level resize allocation function, usually not called directly @param v pointer to a vector @param length_increment length increment in elements @param data_bytes requested size in bytes @param header_bytes header size in bytes (may be zero) @param data_align alignment (may be zero) @return v_prime pointer to resized vector, may or may not equal v */ void *vec_resize_allocate_memory (void *v, word length_increment, uword data_bytes, uword header_bytes, uword data_align); /** \brief Low-level vector resize function, usually not called directly @param v pointer to a vector @param length_increment length increment in elements @param data_bytes requested size in bytes @param header_bytes header size in bytes (may be zero) @param data_align alignment (may be zero) @return v_prime pointer to resized vector, may or may not equal v */ #define _vec_resize(V,L,DB,HB,A) \ _vec_resize_inline(V,L,DB,HB,clib_max((__alignof__((V)[0])),(A))) always_inline void * _vec_resize_inline (void *v, word length_increment, uword data_bytes, uword header_bytes, uword data_align) { vec_header_t *vh = _vec_find (v); uword new_data_bytes, aligned_header_bytes; aligned_header_bytes = vec_header_bytes (header_bytes); new_data_bytes = data_bytes + aligned_header_bytes; if (PREDICT_TRUE (v != 0)) { void *p = v - aligned_header_bytes; /* Vector header must start heap object. */ ASSERT (clib_mem_is_heap_object (p)); /* Typically we'll not need to resize. */ if (new_data_bytes <= clib_mem_size (p)) { vh->len += length_increment; return v; } } /* Slow path: call helper function. */ return vec_resize_allocate_memory (v, length_increment, data_bytes, header_bytes, clib_max (sizeof (vec_header_t), data_align)); } /** \brief Determine if vector will resize with next allocation @param v pointer to a vector @param length_increment length increment in elements @param data_bytes requested size in bytes @param header_bytes header size in bytes (may be zero) @param data_align alignment (may be zero) @return 1 if vector will resize 0 otherwise */ always_inline int _vec_resize_will_expand (void *v, word length_increment, uword data_bytes, uword header_bytes, uword data_align) { uword new_data_bytes, aligned_header_bytes; aligned_header_bytes = vec_header_bytes (header_bytes); new_data_bytes = data_bytes + aligned_header_bytes; if (PREDICT_TRUE (v != 0)) { void *p = v - aligned_header_bytes; /* Vector header must start heap object. */ ASSERT (clib_mem_is_heap_object (p)); /* Typically we'll not need to resize. */ if (new_data_bytes <= clib_mem_size (p)) return 0; } return 1; } /** \brief Predicate function, says whether the supplied vector is a clib heap object (general version). @param v pointer to a vector @param header_bytes vector header size in bytes (may be zero) @return 0 or 1 */ uword clib_mem_is_vec_h (void *v, uword header_bytes); /** \brief Predicate function, says whether the supplied vector is a clib heap object @param v pointer to a vector @return 0 or 1 */ always_inline uword clib_mem_is
#!/usr/bin/env python
""" VAPI test """

import unittest
import os
import signal
from framework import VppTestCase, running_extended_tests, \
    running_on_centos, VppTestRunner, Worker


@unittest.skipUnless(running_extended_tests, "part of extended tests")
class VAPITestCase(VppTestCase):
    """ VAPI test """

    @classmethod
    def setUpClass(cls):
        super(VAPITestCase, cls).setUpClass()

    @classmethod
    def tearDownClass(cls):
        super(VAPITestCase, cls).tearDownClass()

    def test_vapi_c(self):
        """ run C VAPI tests """
        var = "TEST_DIR"
        built_root = os.getenv(var, None)
        self.assertIsNotNone(built_root,
                             "Environment variable `%s' not set" % var)
        executable = "%s/build/vapi_test/vapi_c_test" % built_root
        worker = Worker(
            [executable, "vapi client", self.shm_prefix], self.logger)
        worker.start()
        timeout = 60
        worker.join(timeout)
        self.logger.info("Worker result is `%s'" % worker.result)
        error = False
        if worker.result is None:
            try:
                error = True
                self.logger.error(
                    "Timeout! Worker did not finish in %ss" % timeout)
                os.killpg(os.getpgid(worker.process.pid), signal.SIGTERM)
                worker.join()
            except:
                self.logger.debug("Couldn't kill worker-spawned process")
                raise
        if error:
            raise Exception(
                "Timeout! Worker did not finish in %ss" % timeout)
        self.assert_equal(worker.result, 0, "Binary test return code")

    @unittest.skipIf(running_on_centos, "Centos's gcc can't compile our C++")
    def test_vapi_cpp(self):
        """ run C++ VAPI tests """
        var = "TEST_DIR"
        built_root = os.getenv(var, None)
        self.assertIsNotNone(built_root,
                             "Environment variable `%s' not set" % var)
        executable = "%s/build/vapi_test/vapi_cpp_test" % built_root
        worker = Worker(
            [executable, "vapi client", self.shm_prefix], self.logger)
        worker.start()
        timeout = 120
        worker.join(timeout)
        self.logger.info("Worker result is `%s'" % worker.result)
        error = False
        if worker.result is None:
            try:
                error = True
                self.logger.error(
                    "Timeout! Worker did not finish in %ss" % timeout)
                os.killpg(os.getpgid(worker.process.pid), signal.SIGTERM)
                worker.join()
            except:
                raise Exception("Couldn't kill worker-spawned process")
        if error:
            raise Exception(
                "Timeout! Worker did not finish in %ss" % timeout)
        self.assert_equal(worker.result, 0, "Binary test return code")


if __name__ == '__main__':
    unittest.main(testRunner=VppTestRunner)
unequal */ #define vec_is_equal(v1,v2) \ (vec_len (v1) == vec_len (v2) && ! memcmp ((v1), (v2), vec_len (v1) * sizeof ((v1)[0]))) /** \brief Compare two vectors (only applicable to vectors of signed numbers). Used in qsort compare functions. @param v1 Pointer to a vector @param v2 Pointer to a vector @return -1, 0, +1 */ #define vec_cmp(v1,v2) \ ({ \ word _v(i), _v(cmp), _v(l); \ _v(l) = clib_min (vec_len (v1), vec_len (v2)); \ _v(cmp) = 0; \ for (_v(i) = 0; _v(i) < _v(l); _v(i)++) { \ _v(cmp) = (v1)[_v(i)] - (v2)[_v(i)]; \ if (_v(cmp)) \ break; \ } \ if (_v(cmp) == 0 && _v(l) > 0) \ _v(cmp) = vec_len(v1) - vec_len(v2); \ (_v(cmp) < 0 ? -1 : (_v(cmp) > 0 ? +1 : 0)); \ }) /** \brief Search a vector for the index of the entry that matches. @param v Pointer to a vector @param E Entry to match @return index of match or ~0 */ #define vec_search(v,E) \ ({ \ word _v(i) = 0; \ while (_v(i) < vec_len(v)) \ { \ if ((v)[_v(i)] == E) \ break; \ _v(i)++; \ } \ if (_v(i) == vec_len(v)) \ _v(i) = ~0; \ _v(i); \ }) /** \brief Search a vector for the index of the entry that matches. @param v Pointer to a vector @param E Pointer to entry to match @param fn Comparison function !0 => match @return index of match or ~0 */ #define vec_search_with_function(v,E,fn) \ ({ \ word _v(i) = 0; \ while (_v(i) < vec_len(v)) \ { \ if (0 != fn(&(v)[_v(i)], (E))) \ break; \ _v(i)++; \ } \ if (_v(i) == vec_len(v)) \ _v(i) = ~0; \ _v(i); \ }) /** \brief Sort a vector using the supplied element comparison function @param vec vector to sort @param f comparison function */ #define vec_sort_with_function(vec,f) \ do { \ qsort (vec, vec_len (vec), sizeof (vec[0]), (void *) (f)); \ } while (0) /** \brief Make a vector containing a NULL terminated c-string. @param V (possibly NULL) pointer to a vector. @param S pointer to string buffer. @param L string length (NOT including the terminating NULL; a la strlen()) */ #define vec_validate_init_c_string(V, S, L) \ do { \ vec_reset_length (V); \ vec_validate ((V), (L)); \ if ((S) && (L)) \ clib_memcpy_fast ((V), (S), (L)); \ (V)[(L)] = 0; \ } while (0) /** \brief Test whether a vector is a NULL terminated c-string. @param V (possibly NULL) pointer to a vector. @return BOOLEAN indicating if the vector c-string is null terminated. */ #define vec_c_string_is_terminated(V) \ (((V) != 0) && (vec_len (V) != 0) && ((V)[vec_len ((V)) - 1] == 0)) /** \brief (If necessary) NULL terminate a vector containing a c-string. @param V (possibly NULL) pointer to a vector. @return V (value-result macro parameter) */ #define vec_terminate_c_string(V) \ do { \ u32 vl = vec_len ((V)); \ if (!vec_c_string_is_terminated(V)) \ { \ vec_validate ((V), vl); \ (V)[vl] = 0; \ } \ } while (0) #endif /* included_vec_h */ /* * fd.io coding-style-patch-verification: ON * * Local Variables: * eval: (c-set-style "gnu") * End: */