summaryrefslogtreecommitdiffstats
path: root/test/util.py
blob: a648490681d31825cec3e19558a26b7c1f4676da (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
""" test framework utilities """

import socket
import sys
from abc import abstractmethod, ABCMeta
from cStringIO import StringIO


def ppp(headline, packet):
    """ Return string containing the output of scapy packet.show() call. """
    o = StringIO()
    old_stdout = sys.stdout
    sys.stdout = o
    print(headline)
    packet.show()
    sys.stdout = old_stdout
    return o.getvalue()


def ppc(headline, capture, limit=10):
    """ Return string containing ppp() printout for a capture.

    :param headline: printed as first line of output
    :param capture: packets to print
    :param limit: limit the print to # of packets
    """
    if not capture:
        return headline
    tail = ""
    if limit < len(capture):
        tail = "\nPrint limit reached, %s out of %s packets printed" % (
            len(capture), limit)
        limit = len(capture)
    body = "".join([ppp("Packet #%s:" % count, p)
                    for count, p in zip(range(0, limit), capture)])
    return "%s\n%s%s" % (headline, body, tail)


def ip4_range(ip4, s, e):
    tmp = ip4.rsplit('.', 1)[0]
    return ("%s.%d" % (tmp, i) for i in range(s, e))


def ip4n_range(ip4n, s, e):
    ip4 = socket.inet_ntop(socket.AF_INET, ip4n)
    return (socket.inet_pton(socket.AF_INET, ip)
            for ip in ip4_range(ip4, s, e))


class NumericConstant(object):
    __metaclass__ = ABCMeta

    desc_dict = {}

    @abstractmethod
    def __init__(self, value):
        self._value = value

    def __int__(self):
        return self._value

    def __long__(self):
        return self._value

    def __str__(self):
        if self._value in self.desc_dict:
            return self.desc_dict[self._value]
        return ""


class Host(object):
    """ Generic test host "connected" to VPPs interface. """

    @property
    def mac(self):
        """ MAC address """
        return self._mac

    @property
    def ip4(self):
        """ IPv4 address - string """
        return self._ip4

    @property
    def ip4n(self):
        """ IPv4 address of remote host - raw, suitable as API parameter."""
        return socket.inet_pton(socket.AF_INET, self._ip4)

    @property
    def ip6(self):
        """ IPv6 address - string """
        return self._ip6

    @property
    def ip6n(self):
        """ IPv6 address of remote host - raw, suitable as API parameter."""
        return socket.inet_pton(socket.AF_INET6, self._ip6)

    def __init__(self, mac=None, ip4=None, ip6=None):
        self._mac = mac
        self._ip4 = ip4
        self._ip6 = ip6
n class="p">, vlib_buffer_t * b) { l2t_main_t *lm = &l2t_main; vlib_node_t *n = vlib_get_node (vm, l2t_encap_node.index); u32 node_counter_base_index = n->error_heap_index; vlib_error_main_t *em = &vm->error_main; l2tpv3_header_t *l2tp; u32 session_index; u32 counter_index; l2t_session_t *s; ip6_header_t *ip6; u16 payload_length; u32 next_index = L2T_ENCAP_NEXT_IP6_LOOKUP; /* Other-than-output pkt? We're done... */ if (vnet_buffer (b)->l2t.next_index != L2T_ENCAP_NEXT_IP6_LOOKUP) return vnet_buffer (b)->l2t.next_index; em->counters[node_counter_base_index + L2T_ENCAP_ERROR_NETWORK_TO_USER] += 1; session_index = vnet_buffer (b)->l2t.session_index; counter_index = session_index_to_counter_index (session_index, SESSION_COUNTER_NETWORK_TO_USER); /* per-mapping byte stats include the ethernet header */ vlib_increment_combined_counter (&lm->counter_main, vlib_get_thread_index (), counter_index, 1 /* packet_increment */ , vlib_buffer_length_in_chain (vm, b)); s = pool_elt_at_index (lm->sessions, session_index); vnet_buffer (b)->sw_if_index[VLIB_TX] = s->encap_fib_index; /* Paint on an l2tpv3 hdr */ vlib_buffer_advance (b, -(s->l2tp_hdr_size)); l2tp = vlib_buffer_get_current (b); l2tp->session_id = s->remote_session_id; l2tp->cookie = s->remote_cookie; if (PREDICT_FALSE (s->l2_sublayer_present)) { l2tp->l2_specific_sublayer = 0; } /* Paint on an ip6 header */ vlib_buffer_advance (b, -(sizeof (*ip6))); ip6 = vlib_buffer_get_current (b); if (PREDICT_FALSE (!(s->admin_up))) { b->error = node->errors[L2T_ENCAP_ERROR_ADMIN_DOWN]; next_index = L2T_ENCAP_NEXT_DROP; goto done; } ip6->ip_version_traffic_class_and_flow_label = clib_host_to_net_u32 (0x6 << 28); /* calculate ip6 payload length */ payload_length = vlib_buffer_length_in_chain (vm, b); payload_length -= sizeof (*ip6); ip6->payload_length = clib_host_to_net_u16 (payload_length); ip6->protocol = IP_PROTOCOL_L2TP; ip6->hop_limit = 0xff; ip6->src_address.as_u64[0] = s->our_address.as_u64[0]; ip6->src_address.as_u64[1] = s->our_address.as_u64[1]; ip6->dst_address.as_u64[0] = s->client_address.as_u64[0]; ip6->dst_address.as_u64[1] = s->client_address.as_u64[1]; done: if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED)) { l2t_trace_t *t = vlib_add_trace (vm, node, b, sizeof (*t)); t->is_user_to_network = 0; t->our_address.as_u64[0] = ip6->src_address.as_u64[0]; t->our_address.as_u64[1] = ip6->src_address.as_u64[1]; t->client_address.as_u64[0] = ip6->dst_address.as_u64[0]; t->client_address.as_u64[1] = ip6->dst_address.as_u64[1]; t->session_index = session_index; } return next_index; } #include <vnet/pipeline.h> VLIB_NODE_FN (l2t_encap_node) (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame) { return dispatch_pipeline (vm, node, frame); } /* *INDENT-OFF* */ VLIB_REGISTER_NODE (l2t_encap_node) = { .name = "l2tp-encap", .vector_size = sizeof (u32), .format_trace = format_l2t_trace, .type = VLIB_NODE_TYPE_INTERNAL, .runtime_data_bytes = sizeof (l2tp_encap_runtime_t), .n_errors = ARRAY_LEN(l2t_encap_error_strings), .error_strings = l2t_encap_error_strings, .n_next_nodes = L2T_ENCAP_N_NEXT, /* add dispositions here */ .next_nodes = { [L2T_ENCAP_NEXT_IP6_LOOKUP] = "ip6-lookup", [L2T_ENCAP_NEXT_DROP] = "error-drop", }, }; /* *INDENT-ON* */ #ifndef CLIB_MARCH_VARIANT void l2tp_encap_init (vlib_main_t * vm) { l2tp_encap_runtime_t *rt; rt = vlib_node_get_runtime_data (vm, l2t_encap_node.index); rt->vnet_main = vnet_get_main (); rt->cached_sw_if_index = (u32) ~ 0; rt->cached_session_index = (u32) ~ 0; } #endif /* CLIB_MARCH_VARIANT */ /* * fd.io coding-style-patch-verification: ON * * Local Variables: * eval: (c-set-style "gnu") * End: */