aboutsummaryrefslogtreecommitdiffstats
path: root/vpp-api/java/jvpp/gen/jvpp_c_gen.py
blob: c02c826fc49a4bc67f9652abc85598c586b09e8a (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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#!/usr/bin/env python
#
# Copyright (c) 2016 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
# l
# 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.
#

import os, util
from string import Template

def is_manually_generated(f_name):
    return f_name in {'control_ping_reply'}


class_reference_template = Template("""jclass ${ref_name}Class;
""")

find_class_invocation_template = Template("""
    ${ref_name}Class = (jclass)(*env)->NewGlobalRef(env, (*env)->FindClass(env, "org/openvpp/jvpp/dto/${class_name}"));
    if ((*env)->ExceptionCheck(env)) {
        (*env)->ExceptionDescribe(env);
        return JNI_ERR;
    }""")

class_cache_template = Template("""
$class_references
static int cache_class_references(JNIEnv* env) {
    $find_class_invocations
    return 0;
}""")

def generate_class_cache(func_list):
    class_references = []
    find_class_invocations = []
    for f in func_list:
        c_name = f['name']
        class_name = util.underscore_to_camelcase_upper(c_name)
        ref_name = util.underscore_to_camelcase(c_name)

        if not util.is_reply(class_name) or util.is_ignored(c_name) or util.is_notification(c_name):
            # TODO handle notifications
            continue

        class_references.append(class_reference_template.substitute(
                ref_name=ref_name))

        find_class_invocations.append(find_class_invocation_template.substitute(
                ref_name=ref_name,
                class_name=class_name))

    return class_cache_template.substitute(
            class_references="".join(class_references), find_class_invocations="".join(find_class_invocations))


# TODO: cache method and field identifiers to achieve better performance
# https://jira.fd.io/browse/HONEYCOMB-42
request_class_template = Template("""
    jclass requestClass = (*env)->FindClass(env, "org/openvpp/jvpp/dto/${java_name_upper}");""")

request_field_identifier_template = Template("""
    jfieldID ${java_name}FieldId = (*env)->GetFieldID(env, requestClass, "${java_name}", "${jni_signature}");
    ${jni_type} ${java_name} = (*env)->Get${jni_getter}(env, request, ${java_name}FieldId);
    """)

u8_struct_setter_template = Template("""
    mp->${c_name} = ${java_name};""")

u16_struct_setter_template = Template("""
    mp->${c_name} = clib_host_to_net_u16(${java_name});""")

u32_struct_setter_template = Template("""
    mp->${c_name} = clib_host_to_net_u32(${java_name});""")

i32_struct_setter_template = Template("""
    mp->${c_name} = clib_host_to_net_i32(${java_name});!""")

u64_struct_setter_template = Template("""
    mp->${c_name} = clib_host_to_net_u64(${java_name});""")

u8_array_struct_setter_template = Template("""
    {
        jsize cnt = (*env)->GetArrayLength (env, ${java_name});
        size_t max_size = ${field_length};
        if (max_size != 0 && cnt > max_size) cnt = max_size;
        (*env)->GetByteArrayRegion(env, ${java_name}, 0, cnt, (jbyte *)mp->${c_name});
    }
""")

u32_array_struct_setter_template = Template("""
    jint * ${java_name}ArrayElements = (*env)->GetIntArrayElements(env, ${java_name}, NULL);
    {
        size_t _i;
        jsize cnt = (*env)->GetArrayLength (env, ${java_name});
        size_t max_size = ${field_length};
        if (max_size != 0 && cnt > max_size) cnt = max_size;
        for (_i = 0; _i < cnt; _i++) {
            mp->${c_name}[_i] = clib_host_to_net_u32(${java_name}ArrayElements[_i]);
        }
    }
    (*env)->ReleaseIntArrayElements (env, ${java_name}, ${java_name}ArrayElements, 0);
    """)

vl_api_ip4_fib_counter_t_array_struct_setter_template = Template("""
    // vl_api_ip4_fib_counter_t_array_field_setter_template FIXME""")

vl_api_ip6_fib_counter_t_array_struct_setter_template = Template("""
    // vl_api_ip6_fib_counter_t_array_field_setter_template FIXME""")

struct_setter_templates = {'u8': u8_struct_setter_template,
                          'u16': u32_struct_setter_template,
                          'u32': u32_struct_setter_template,
                          'i32': u32_struct_setter_template,
                          'u64': u64_struct_setter_template,
                          'u8[]': u8_array_struct_setter_template,
                          'u32[]': u32_array_struct_setter_template,
                          'vl_api_ip4_fib_counter_t[]': vl_api_ip4_fib_counter_t_array_struct_setter_template,
                          'vl_api_ip6_fib_counter_t[]': vl_api_ip6_fib_counter_t_array_struct_setter_template
                  }

jni_impl_template = Template("""
/**
 * JNI binding for sending ${c_name} vpe.api message.
 * Generated based on $inputfile preparsed data:
$api_data
 */
JNIEXPORT jint JNICALL Java_org_openvpp_jvpp_JVppImpl_${java_name}0
(JNIEnv * env, jclass clazz$args) {
    vppjni_main_t *jm = &vppjni_main;
    vl_api_${c_name}_t * mp;
    u32 my_context_id;
    int rv;
    rv = vppjni_sanity_check (jm);
    if (rv) return rv;
    my_context_id = vppjni_get_context_id (jm);
    $request_class
    $field_identifiers
    M(${c_name_uppercase}, ${c_name});
    mp->context = clib_host_to_net_u32 (my_context_id);
    $struct_setters
    S;
    return my_context_id;
}""")

def generate_jni_impl(func_list, inputfile):
    jni_impl = []
    for f in func_list:
        f_name = f['name']
        camel_case_function_name = util.underscore_to_camelcase(f_name)
        if is_manually_generated(f_name) or util.is_reply(camel_case_function_name) \
                or util.is_ignored(f_name) or util.is_notification(f_name):
            continue

        arguments = ''
        request_class = ''
        field_identifiers = ''
        struct_setters = ''
        f_name_uppercase = f_name.upper()

        if f['args']:
            arguments = ', jobject request'
            camel_case_function_name_upper = util.underscore_to_camelcase_upper(f_name)

            request_class = request_class_template.substitute(java_name_upper=camel_case_function_name_upper)

            # field identifiers
            for t in zip(f['types'], f['args']):
                jni_type = t[0]
                java_field_name = util.underscore_to_camelcase(t[1])
                jni_signature = util.jni_2_signature_mapping[jni_type]
                jni_getter = util.jni_field_accessors[jni_type]
                field_identifiers += request_field_identifier_template.substitute(
                        jni_type=jni_type,
                        java_name=java_field_name,
                        jni_signature=jni_signature,
                        jni_getter=jni_getter)

            # field setters
            for t in zip(f['c_types'], f['args'], f['lengths']):
                c_type = t[0]
                c_name = t[1]
                field_length = t[2]
                java_field_name = util.underscore_to_camelcase(c_name)

                struct_setter_template = struct_setter_templates[c_type]

                struct_setters += struct_setter_template.substitute(
                        c_name=c_name,
                        java_name=java_field_name,
                        field_length=field_length)

        jni_impl.append(jni_impl_template.substitute(
                inputfile=inputfile,
                api_data=util.api_message_to_javadoc(f),
                java_name=camel_case_function_name,
                c_name_uppercase=f_name_uppercase,
                c_name=f_name,
                request_class=request_class,
                field_identifiers=field_identifiers,
                struct_setters=struct_setters,
                args=arguments))

    return "\n".join(jni_impl)


dto_field_id_template = Template("""
    jfieldID ${java_name}FieldId = (*env)->GetFieldID(env, ${class_ref_name}Class, "${java_name}", "${jni_signature}");""")

default_dto_field_setter_template = Template("""
    (*env)->Set${jni_setter}(env, dto, ${java_name}FieldId, mp->${c_name});
""")

u32_dto_field_setter_template = Template("""
    (*env)->Set${jni_setter}(env, dto, ${java_name}FieldId, clib_net_to_host_u32(mp->${c_name}));
""")

u64_dto_field_setter_template = Template("""
    (*env)->Set${jni_setter}(env, dto, ${java_name}FieldId, clib_net_to_host_u64(mp->${c_name}));
""")

u8_array_dto_field_setter_template = Template("""
    jbyteArray ${java_name} = (*env)->NewByteArray(env, ${field_length});
    (*env)->SetByteArrayRegion(env, ${java_name}, 0, ${field_length}, (const jbyte*)mp->${c_name});
    (*env)->SetObjectField(env, dto, ${java_name}FieldId, ${java_name});
""")

# For each u64 array we get its elements. Then we convert values to host byte order.
# All changes to  jint* buffer are written to jlongArray (isCopy is set to NULL)
u64_array_dto_field_setter_template = Template("""
    {
        jlongArray ${java_name} = (*env)->NewLongArray(env, ${field_length});
        jlong * ${java_name}ArrayElements = (*env)->GetLongArrayElements(env, ${java_name}, NULL);
        unsigned int _i;
        for (_i = 0; _i < ${field_length}; _i++) {
            ${java_name}ArrayElements[_i] = clib_net_to_host_u64(mp->${c_name}[_i]);
        }
        (*env)->SetObjectField(env, dto, ${java_name}FieldId, ${java_name});
    }
""")

dto_field_setter_templates = {'u8': default_dto_field_setter_template,
                      'u16': u32_dto_field_setter_template,
                      'u32': u32_dto_field_setter_template,
                      'i32': u32_dto_field_setter_template,
                      'u64': u64_dto_field_setter_template,
                      'f64': default_dto_field_setter_template,
                      'u64[]': u64_array_dto_field_setter_template,
                      'u8[]': u8_array_dto_field_setter_template
                      }

msg_handler_template = Template("""
/**
 * Handler for ${handler_name} vpe.api message.
 * Generated based on $inputfile preparsed data:
$api_data
 */
static void vl_api_${handler_name}_t_handler (vl_api_${handler_name}_t * mp)
{
    vppjni_main_t * jm = &vppjni_main;
    JNIEnv *env = jm->jenv;

    jmethodID constructor = (*env)->GetMethodID(env, ${class_ref_name}Class, "<init>", "()V");
    jmethodID callbackMethod = (*env)->GetMethodID(env, jm->callbackClass, "on${dto_name}", "(Lorg/openvpp/jvpp/dto/${dto_name};)V");

    jobject dto = (*env)->NewObject(env, ${class_ref_name}Class, constructor);
    $dto_setters
    (*env)->CallVoidMethod(env, jm->callback, callbackMethod, dto);
}""")

def generate_msg_handlers(func_list, inputfile):
    handlers = []
    for f in func_list:
        handler_name = f['name']
        dto_name = util.underscore_to_camelcase_upper(handler_name)
        ref_name = util.underscore_to_camelcase(handler_name)

        if is_manually_generated(handler_name) or not util.is_reply(dto_name) or util.is_ignored(handler_name) or util.is_notification(handler_name):
            # TODO handle notifications
            continue

        dto_setters = ''
        # dto setters
        for t in zip(f['c_types'], f['types'], f['args'], f['lengths']):
            c_type = t[0]
            jni_type = t[1]
            c_name = t[2]
            field_length = t[3]

            java_field_name = util.underscore_to_camelcase(c_name)
            jni_signature = util.jni_2_signature_mapping[jni_type]
            jni_setter = util.jni_field_accessors[jni_type]

            dto_setters += dto_field_id_template.substitute(
                    java_name=java_field_name,
                    class_ref_name=ref_name,
                    jni_signature=jni_signature)

            dto_setter_template = dto_field_setter_templates[c_type]

            dto_setters += dto_setter_template.substitute(
                    java_name=java_field_name,
                    jni_signature=jni_signature,
                    c_name=c_name,
                    jni_setter=jni_setter,
                    field_length=field_length)

        handlers.append(msg_handler_template.substitute(
                inputfile=inputfile,
                api_data=util.api_message_to_javadoc(f),
                handler_name=handler_name,
                dto_name=dto_name,
                class_ref_name=ref_name,
                dto_setters=dto_setters))

    return "\n".join(handlers)


handler_registration_template = Template("""_(${upercase_name}, ${name}) \\
""")


def generate_handler_registration(func_list):
    handler_registration = ["#define foreach_vpe_api_msg \\\n"]
    for f in func_list:
        name = f['name']
        camelcase_name = util.underscore_to_camelcase(f['name'])

        if not util.is_reply(camelcase_name) or util.is_ignored(name) or util.is_notification(name):
            # TODO handle notifications
            continue

        handler_registration.append(handler_registration_template.substitute(
                name=name,
                upercase_name=name.upper()))

    return "".join(handler_registration)


jvpp_c_template = Template("""/**
 * This file contains JNI bindings for jvpp Java API.
 * It was generated by jvpp_c_gen.py based on $inputfile
 * (python representation of vpe.api generated by vppapigen).
 */

// JAVA class reference cache
$class_cache

// JNI bindings
$jni_implementations

// Message handlers
$msg_handlers

// Registration of message handlers in vlib
$handler_registration
""")

def generate_jvpp(func_list, inputfile):
    """ Generates jvpp C file """
    print "Generating jvpp C"

    class_cache = generate_class_cache(func_list)
    jni_impl = generate_jni_impl(func_list, inputfile)
    msg_handlers = generate_msg_handlers(func_list, inputfile)
    handler_registration = generate_handler_registration(func_list)

    jvpp_c_file = open("jvpp_gen.h", 'w')
    jvpp_c_file.write(jvpp_c_template.substitute(
            inputfile=inputfile,
            class_cache=class_cache,
            jni_implementations=jni_impl,
            msg_handlers=msg_handlers,
            handler_registration=handler_registration))
    jvpp_c_file.flush()
    jvpp_c_file.close()
ass="mi">6]), 5) self.assertEqual(ord(data[7]), 4) self.assertEqual(data[8], claddr[0]) self.assertEqual(data[9], claddr[1]) self.assertEqual(data[10], claddr[2]) self.assertEqual(data[11], claddr[3]) if oui != 0: # sub-option 151 encodes the 3 byte oui # and the 4 byte fib_id self.assertEqual(ord(data[12]), 151) self.assertEqual(ord(data[13]), 8) self.assertEqual(ord(data[14]), 1) self.assertEqual(ord(data[15]), 0) self.assertEqual(ord(data[16]), 0) self.assertEqual(ord(data[17]), oui) self.assertEqual(ord(data[18]), 0) self.assertEqual(ord(data[19]), 0) self.assertEqual(ord(data[20]), 0) self.assertEqual(ord(data[21]), fib_id) # VSS control sub-option self.assertEqual(ord(data[22]), 152) self.assertEqual(ord(data[23]), 0) found = 1 self.assertTrue(found) return data def verify_dhcp_msg_type(self, pkt, name): dhcp = pkt[DHCP] found = False for o in dhcp.options: if type(o) is tuple: if o[0] == "message-type" \ and DHCPTypes[o[1]] == name: found = True self.assertTrue(found) def verify_dhcp_offer(self, pkt, intf, fib_id=0, oui=0): ether = pkt[Ether] self.assertEqual(ether.dst, "ff:ff:ff:ff:ff:ff") self.assertEqual(ether.src, intf.local_mac) ip = pkt[IP] self.assertEqual(ip.dst, "255.255.255.255") self.assertEqual(ip.src, intf.local_ip4) udp = pkt[UDP] self.assertEqual(udp.dport, DHCP4_CLIENT_PORT) self.assertEqual(udp.sport, DHCP4_SERVER_PORT) self.verify_dhcp_msg_type(pkt, "offer") data = self.validate_relay_options(pkt, intf, intf.local_ip4, fib_id, oui) def verify_orig_dhcp_pkt(self, pkt, intf): ether = pkt[Ether] self.assertEqual(ether.dst, "ff:ff:ff:ff:ff:ff") self.assertEqual(ether.src, intf.local_mac) ip = pkt[IP] self.assertEqual(ip.dst, "255.255.255.255") self.assertEqual(ip.src, "0.0.0.0") udp = pkt[UDP] self.assertEqual(udp.dport, DHCP4_SERVER_PORT) self.assertEqual(udp.sport, DHCP4_CLIENT_PORT) def verify_orig_dhcp_discover(self, pkt, intf, hostname, client_id=None): self.verify_orig_dhcp_pkt(pkt, intf) self.verify_dhcp_msg_type(pkt, "discover") self.verify_dhcp_has_option(pkt, "hostname", hostname) if client_id: self.verify_dhcp_has_option(pkt, "client_id", client_id) bootp = pkt[BOOTP] self.assertEqual(bootp.ciaddr, "0.0.0.0") self.assertEqual(bootp.giaddr, "0.0.0.0") self.assertEqual(bootp.flags, 0x8000) def verify_orig_dhcp_request(self, pkt, intf, hostname, ip): self.verify_orig_dhcp_pkt(pkt, intf) self.verify_dhcp_msg_type(pkt, "request") self.verify_dhcp_has_option(pkt, "hostname", hostname) self.verify_dhcp_has_option(pkt, "requested_addr", ip) bootp = pkt[BOOTP] self.assertEqual(bootp.ciaddr, "0.0.0.0") self.assertEqual(bootp.giaddr, "0.0.0.0") self.assertEqual(bootp.flags, 0x8000) def verify_relayed_dhcp_discover(self, pkt, intf, src_intf=None, fib_id=0, oui=0, dst_mac=None, dst_ip=None): if not dst_mac: dst_mac = intf.remote_mac if not dst_ip: dst_ip = intf.remote_ip4 ether = pkt[Ether] self.assertEqual(ether.dst, dst_mac) self.assertEqual(ether.src, intf.local_mac) ip = pkt[IP] self.assertEqual(ip.dst, dst_ip) self.assertEqual(ip.src, intf.local_ip4) udp = pkt[UDP] self.assertEqual(udp.dport, DHCP4_SERVER_PORT) self.assertEqual(udp.sport, DHCP4_CLIENT_PORT) dhcp = pkt[DHCP] is_discover = False for o in dhcp.options: if type(o) is tuple: if o[0] == "message-type" \ and DHCPTypes[o[1]] == "discover": is_discover = True self.assertTrue(is_discover) data = self.validate_relay_options(pkt, src_intf, src_intf.local_ip4, fib_id, oui) return data def verify_dhcp6_solicit(self, pkt, intf, peer_ip, peer_mac, fib_id=0, oui=0, dst_mac=None, dst_ip=None): if not dst_mac: dst_mac = intf.remote_mac if not dst_ip: dst_ip = in6_ptop(intf.remote_ip6) ether = pkt[Ether] self.assertEqual(ether.dst, dst_mac) self.assertEqual(ether.src, intf.local_mac) ip = pkt[IPv6] self.assertEqual(in6_ptop(ip.dst), dst_ip) self.assertEqual(in6_ptop(ip.src), in6_ptop(intf.local_ip6)) udp = pkt[UDP] self.assertEqual(udp.dport, DHCP6_CLIENT_PORT) self.assertEqual(udp.sport, DHCP6_SERVER_PORT) relay = pkt[DHCP6_RelayForward] self.assertEqual(in6_ptop(relay.peeraddr), in6_ptop(peer_ip)) oid = pkt[DHCP6OptIfaceId] cll = pkt[DHCP6OptClientLinkLayerAddr] self.assertEqual(cll.optlen, 8) self.assertEqual(cll.lltype, 1) self.assertEqual(cll.clladdr, peer_mac) if fib_id != 0: vss = pkt[DHCP6OptVSS] self.assertEqual(vss.optlen, 8) self.assertEqual(vss.type, 1) # the OUI and FIB-id are really 3 and 4 bytes resp. # but the tested range is small self.assertEqual(ord(vss.data[0]), 0) self.assertEqual(ord(vss.data[1]), 0) self.assertEqual(ord(vss.data[2]), oui) self.assertEqual(ord(vss.data[3]), 0) self.assertEqual(ord(vss.data[4]), 0) self.assertEqual(ord(vss.data[5]), 0) self.assertEqual(ord(vss.data[6]), fib_id) # the relay message should be an encoded Solicit msg = pkt[DHCP6OptRelayMsg] sol = DHCP6_Solicit() self.assertEqual(msg.optlen, len(str(sol))) self.assertEqual(str(sol), (str(msg[1]))[:msg.optlen]) def verify_dhcp6_advert(self, pkt, intf, peer): ether = pkt[Ether] self.assertEqual(ether.dst, "ff:ff:ff:ff:ff:ff") self.assertEqual(ether.src, intf.local_mac) ip = pkt[IPv6] self.assertEqual(in6_ptop(ip.dst), in6_ptop(peer)) self.assertEqual(in6_ptop(ip.src), in6_ptop(intf.local_ip6)) udp = pkt[UDP] self.assertEqual(udp.dport, DHCP6_SERVER_PORT) self.assertEqual(udp.sport, DHCP6_CLIENT_PORT) # not sure why this is not decoding # adv = pkt[DHCP6_Advertise] def test_dhcp_proxy(self): """ DHCPv4 Proxy """ # # Verify no response to DHCP request without DHCP config # p_disc_vrf0 = (Ether(dst="ff:ff:ff:ff:ff:ff", src=self.pg2.remote_mac) / IP(src="0.0.0.0", dst="255.255.255.255") / UDP(sport=DHCP4_CLIENT_PORT, dport=DHCP4_SERVER_PORT) / BOOTP(op=1) / DHCP(options=[('message-type', 'discover'), ('end')])) pkts_disc_vrf0 = [p_disc_vrf0] p_disc_vrf1 = (Ether(dst="ff:ff:ff:ff:ff:ff", src=self.pg3.remote_mac) / IP(src="0.0.0.0", dst="255.255.255.255") / UDP(sport=DHCP4_CLIENT_PORT, dport=DHCP4_SERVER_PORT) / BOOTP(op=1) / DHCP(options=[('message-type', 'discover'), ('end')])) pkts_disc_vrf1 = [p_disc_vrf0] self.send_and_assert_no_replies(self.pg2, pkts_disc_vrf0, "DHCP with no configuration") self.send_and_assert_no_replies(self.pg3, pkts_disc_vrf1, "DHCP with no configuration") # # Enable DHCP proxy in VRF 0 # server_addr = self.pg0.remote_ip4n src_addr = self.pg0.local_ip4n self.vapi.dhcp_proxy_config(server_addr, src_addr, rx_table_id=0) # # Discover packets from the client are dropped because there is no # IP address configured on the client facing interface # self.send_and_assert_no_replies(self.pg2, pkts_disc_vrf0, "Discover DHCP no relay address") # # Inject a response from the server # dropped, because there is no IP addrees on the # client interfce to fill in the option. # p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) / UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_SERVER_PORT) / BOOTP(op=1) / DHCP(options=[('message-type', 'offer'), ('end')])) pkts = [p] self.send_and_assert_no_replies(self.pg2, pkts, "Offer DHCP no relay address") # # configure an IP address on the client facing interface # self.pg2.config_ip4() # # Try again with a discover packet # Rx'd packet should be to the server address and from the configured # source address # UDP source ports are unchanged # we've no option 82 config so that should be absent # self.pg2.add_stream(pkts_disc_vrf0) self.pg_enable_capture(self.pg_interfaces) self.pg_start() rx = self.pg0.get_capture(1) rx = rx[0] option_82 = self.verify_relayed_dhcp_discover(rx, self.pg0, src_intf=self.pg2) # # Create an DHCP offer reply from the server with a correctly formatted # option 82. i.e. send back what we just captured # The offer, sent mcast to the client, still has option 82. # p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) / UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_SERVER_PORT) / BOOTP(op=1) / DHCP(options=[('message-type', 'offer'), ('relay_agent_Information', option_82), ('end')])) pkts = [p] self.pg0.add_stream(pkts) self.pg_enable_capture(self.pg_interfaces) self.pg_start() rx = self.pg2.get_capture(1) rx = rx[0] self.verify_dhcp_offer(rx, self.pg2) # # Bogus Option 82: # # 1. not our IP address = not checked by VPP? so offer is replayed # to client bad_ip = option_82[0:8] + chr(33) + option_82[9:] p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) / UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_SERVER_PORT) / BOOTP(op=1) / DHCP(options=[('message-type', 'offer'), ('relay_agent_Information', bad_ip), ('end')])) pkts = [p] self.send_and_assert_no_replies(self.pg0, pkts, "DHCP offer option 82 bad address") # 2. Not a sw_if_index VPP knows bad_if_index = option_82[0:2] + chr(33) + option_82[3:] p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) / UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_SERVER_PORT) / BOOTP(op=1) / DHCP(options=[('message-type', 'offer'), ('relay_agent_Information', bad_if_index), ('end')])) pkts = [p] self.send_and_assert_no_replies(self.pg0, pkts, "DHCP offer option 82 bad if index") # # Send a DHCP request in VRF 1. should be dropped. # self.send_and_assert_no_replies(self.pg3, pkts_disc_vrf1, "DHCP with no configuration VRF 1") # # Delete the DHCP config in VRF 0 # Should now drop requests. # self.vapi.dhcp_proxy_config(server_addr, src_addr, rx_table_id=0, is_add=0) self.send_and_assert_no_replies(self.pg2, pkts_disc_vrf0, "DHCP config removed VRF 0") self.send_and_assert_no_replies(self.pg3, pkts_disc_vrf1, "DHCP config removed VRF 1") # # Add DHCP config for VRF 1 # server_addr = self.pg1.remote_ip4n src_addr = self.pg1.local_ip4n self.vapi.dhcp_proxy_config(server_addr, src_addr, rx_table_id=1, server_table_id=1) # # Confim DHCP requests ok in VRF 1. # - dropped on IP config on client interface # self.send_and_assert_no_replies(self.pg3, pkts_disc_vrf1, "DHCP config removed VRF 1") # # configure an IP address on the client facing interface # self.pg3.config_ip4() self.pg3.add_stream(pkts_disc_vrf1) self.pg_enable_capture(self.pg_interfaces) self.pg_start() rx = self.pg1.get_capture(1) rx = rx[0] self.verify_relayed_dhcp_discover(rx, self.pg1, src_intf=self.pg3) # # Add VSS config # table=1, fib=id=1, oui=4 self.vapi.dhcp_proxy_set_vss(1, 1, 4) self.pg3.add_stream(pkts_disc_vrf1) self.pg_enable_capture(self.pg_interfaces) self.pg_start() rx = self.pg1.get_capture(1) rx = rx[0] self.verify_relayed_dhcp_discover(rx, self.pg1, src_intf=self.pg3, fib_id=1, oui=4) # # Add a second DHCP server in VRF 1 # expect clients messages to be relay to both configured servers # self.pg1.generate_remote_hosts(2) server_addr2 = socket.inet_pton(AF_INET, self.pg1.remote_hosts[1].ip4) self.vapi.dhcp_proxy_config(server_addr2, src_addr, rx_table_id=1, server_table_id=1, is_add=1) # # We'll need an ARP entry for the server to send it packets # arp_entry = VppNeighbor(self, self.pg1.sw_if_index, self.pg1.remote_hosts[1].mac, self.pg1.remote_hosts[1].ip4) arp_entry.add_vpp_config() # # Send a discover from the client. expect two relayed messages # The frist packet is sent to the second server # We're not enforcing that here, it's just the way it is. # self.pg3.add_stream(pkts_disc_vrf1) self.pg_enable_capture(self.pg_interfaces) self.pg_start() rx = self.pg1.get_capture(2) option_82 = self.verify_relayed_dhcp_discover( rx[0], self.pg1, src_intf=self.pg3, dst_mac=self.pg1.remote_hosts[1].mac, dst_ip=self.pg1.remote_hosts[1].ip4, fib_id=1, oui=4) self.verify_relayed_dhcp_discover(rx[1], self.pg1, src_intf=self.pg3, fib_id=1, oui=4) # # Send both packets back. Client gets both. # p1 = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) / IP(src=self.pg1.remote_ip4, dst=self.pg1.local_ip4) / UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_SERVER_PORT) / BOOTP(op=1) / DHCP(options=[('message-type', 'offer'), ('relay_agent_Information', option_82), ('end')])) p2 = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) / IP(src=self.pg1.remote_hosts[1].ip4, dst=self.pg1.local_ip4) / UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_SERVER_PORT) / BOOTP(op=1) / DHCP(options=[('message-type', 'offer'), ('relay_agent_Information', option_82), ('end')])) pkts = [p1, p2] self.pg1.add_stream(pkts) self.pg_enable_capture(self.pg_interfaces) self.pg_start() rx = self.pg3.get_capture(2) self.verify_dhcp_offer(rx[0], self.pg3, fib_id=1, oui=4) self.verify_dhcp_offer(rx[1], self.pg3, fib_id=1, oui=4) # # Ensure offers from non-servers are dropeed # p2 = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) / IP(src="8.8.8.8", dst=self.pg1.local_ip4) / UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_SERVER_PORT) / BOOTP(op=1) / DHCP(options=[('message-type', 'offer'), ('relay_agent_Information', option_82), ('end')])) self.send_and_assert_no_replies(self.pg1, p2, "DHCP offer from non-server") # # Ensure only the discover is sent to multiple servers # p_req_vrf1 = (Ether(dst="ff:ff:ff:ff:ff:ff", src=self.pg3.remote_mac) / IP(src="0.0.0.0", dst="255.255.255.255") / UDP(sport=DHCP4_CLIENT_PORT, dport=DHCP4_SERVER_PORT) / BOOTP(op=1) / DHCP(options=[('message-type', 'request'), ('end')])) self.pg3.add_stream(p_req_vrf1) self.pg_enable_capture(self.pg_interfaces) self.pg_start() rx = self.pg1.get_capture(1) # # Remove the second DHCP server # self.vapi.dhcp_proxy_config(server_addr2, src_addr, rx_table_id=1, server_table_id=1, is_add=0) # # Test we can still relay with the first # self.pg3.add_stream(pkts_disc_vrf1) self.pg_enable_capture(self.pg_interfaces) self.pg_start() rx = self.pg1.get_capture(1) rx = rx[0] self.verify_relayed_dhcp_discover(rx, self.pg1, src_intf=self.pg3, fib_id=1, oui=4) # # Remove the VSS config # relayed DHCP has default vlaues in the option. # self.vapi.dhcp_proxy_set_vss(1, 1, 4, is_add=0) self.pg3.add_stream(pkts_disc_vrf1) self.pg_enable_capture(self.pg_interfaces) self.pg_start() rx = self.pg1.get_capture(1) rx = rx[0] self.verify_relayed_dhcp_discover(rx, self.pg1, src_intf=self.pg3) # # remove DHCP config to cleanup # self.vapi.dhcp_proxy_config(server_addr, src_addr, rx_table_id=1, server_table_id=1, is_add=0) self.send_and_assert_no_replies(self.pg2, pkts_disc_vrf0, "DHCP cleanup VRF 0") self.send_and_assert_no_replies(self.pg3, pkts_disc_vrf1, "DHCP cleanup VRF 1") def test_dhcp6_proxy(self): """ DHCPv6 Proxy""" # # Verify no response to DHCP request without DHCP config # dhcp_solicit_dst = "ff02::1:2" dhcp_solicit_src_vrf0 = mk_ll_addr(self.pg2.remote_mac) dhcp_solicit_src_vrf1 = mk_ll_addr(self.pg3.remote_mac) server_addr_vrf0 = self.pg0.remote_ip6n src_addr_vrf0 = self.pg0.local_ip6n server_addr_vrf1 = self.pg1.remote_ip6n src_addr_vrf1 = self.pg1.local_ip6n dmac = in6_getnsmac(inet_pton(socket.AF_INET6, dhcp_solicit_dst)) p_solicit_vrf0 = (Ether(dst=dmac, src=self.pg2.remote_mac) / IPv6(src=dhcp_solicit_src_vrf0, dst=dhcp_solicit_dst) / UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_CLIENT_PORT) / DHCP6_Solicit()) p_solicit_vrf1 = (Ether(dst=dmac, src=self.pg3.remote_mac) / IPv6(src=dhcp_solicit_src_vrf1, dst=dhcp_solicit_dst) / UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_CLIENT_PORT) / DHCP6_Solicit()) self.send_and_assert_no_replies(self.pg2, p_solicit_vrf0, "DHCP with no configuration") self.send_and_assert_no_replies(self.pg3, p_solicit_vrf1, "DHCP with no configuration") # # DHCPv6 config in VRF 0. # Packets still dropped because the client facing interface has no # IPv6 config # self.vapi.dhcp_proxy_config(server_addr_vrf0, src_addr_vrf0, rx_table_id=0, server_table_id=0, is_ipv6=1) self.send_and_assert_no_replies(self.pg2, p_solicit_vrf0, "DHCP with no configuration") self.send_and_assert_no_replies(self.pg3, p_solicit_vrf1, "DHCP with no configuration") # # configure an IP address on the client facing interface # self.pg2.config_ip6() # # Now the DHCP requests are relayed to the server # self.pg2.add_stream(p_solicit_vrf0) self.pg_enable_capture(self.pg_interfaces) self.pg_start() rx = self.pg0.get_capture(1) self.verify_dhcp6_solicit(rx[0], self.pg0, dhcp_solicit_src_vrf0, self.pg2.remote_mac) # # Exception cases for rejected relay responses # # 1 - not a relay reply p_adv_vrf0 = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / IPv6(dst=self.pg0.local_ip6, src=self.pg0.remote_ip6) / UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) / DHCP6_Advertise()) self.send_and_assert_no_replies(self.pg2, p_adv_vrf0, "DHCP6 not a relay reply") # 2 - no relay message option p_adv_vrf0 = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / IPv6(dst=self.pg0.local_ip6, src=self.pg0.remote_ip6) / UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) / DHCP6_RelayReply() / DHCP6_Advertise()) self.send_and_assert_no_replies(self.pg2, p_adv_vrf0, "DHCP not a relay message") # 3 - no circuit ID p_adv_vrf0 = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / IPv6(dst=self.pg0.local_ip6, src=self.pg0.remote_ip6) / UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) / DHCP6_RelayReply() / DHCP6OptRelayMsg(optlen=0) / DHCP6_Advertise()) self.send_and_assert_no_replies(self.pg2, p_adv_vrf0, "DHCP6 no circuit ID") # 4 - wrong circuit ID p_adv_vrf0 = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / IPv6(dst=self.pg0.local_ip6, src=self.pg0.remote_ip6) / UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) / DHCP6_RelayReply() / DHCP6OptIfaceId(optlen=4, ifaceid='\x00\x00\x00\x05') / DHCP6OptRelayMsg(optlen=0) / DHCP6_Advertise()) self.send_and_assert_no_replies(self.pg2, p_adv_vrf0, "DHCP6 wrong circuit ID") # # Send the relay response (the advertisement) # - no peer address p_adv_vrf0 = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / IPv6(dst=self.pg0.local_ip6, src=self.pg0.remote_ip6) / UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) / DHCP6_RelayReply() / DHCP6OptIfaceId(optlen=4, ifaceid='\x00\x00\x00\x03') / DHCP6OptRelayMsg(optlen=0) / DHCP6_Advertise(trid=1) / DHCP6OptStatusCode(statuscode=0)) pkts_adv_vrf0 = [p_adv_vrf0] self.pg0.add_stream(pkts_adv_vrf0) self.pg_enable_capture(self.pg_interfaces) self.pg_start() rx = self.pg2.get_capture(1) self.verify_dhcp6_advert(rx[0], self.pg2, "::") # # Send the relay response (the advertisement) # - with peer address p_adv_vrf0 = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) / IPv6(dst=self.pg0.local_ip6, src=self.pg0.remote_ip6) / UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) / DHCP6_RelayReply(peeraddr=dhcp_solicit_src_vrf0) / DHCP6OptIfaceId(optlen=4, ifaceid='\x00\x00\x00\x03') / DHCP6OptRelayMsg(optlen=0) / DHCP6_Advertise(trid=1) / DHCP6OptStatusCode(statuscode=0)) pkts_adv_vrf0 = [p_adv_vrf0] self.pg0.add_stream(pkts_adv_vrf0) self.pg_enable_capture(self.pg_interfaces) self.pg_start() rx = self.pg2.get_capture(1) self.verify_dhcp6_advert(rx[0], self.pg2, dhcp_solicit_src_vrf0) # # Add all the config for VRF 1 # self.vapi.dhcp_proxy_config(server_addr_vrf1, src_addr_vrf1, rx_table_id=1, server_table_id=1, is_ipv6=1) self.pg3.config_ip6() # # VRF 1 solicit # self.pg3.add_stream(p_solicit_vrf1) self.pg_enable_capture(self.pg_interfaces) self.pg_start() rx = self.pg1.get_capture(1) self.verify_dhcp6_solicit(rx[0], self.pg1, dhcp_solicit_src_vrf1, self.pg3.remote_mac) # # VRF 1 Advert # p_adv_vrf1 = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) / IPv6(dst=self.pg1.local_ip6, src=self.pg1.remote_ip6) / UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) / DHCP6_RelayReply(peeraddr=dhcp_solicit_src_vrf1) / DHCP6OptIfaceId(optlen=4, ifaceid='\x00\x00\x00\x04') / DHCP6OptRelayMsg(optlen=0) / DHCP6_Advertise(trid=1) / DHCP6OptStatusCode(statuscode=0)) pkts_adv_vrf1 = [p_adv_vrf1] self.pg1.add_stream(pkts_adv_vrf1) self.pg_enable_capture(self.pg_interfaces) self.pg_start() rx = self.pg3.get_capture(1) self.verify_dhcp6_advert(rx[0], self.pg3, dhcp_solicit_src_vrf1) # # Add VSS config # table=1, fib=id=1, oui=4 self.vapi.dhcp_proxy_set_vss(1, 1, 4, is_ip6=1) self.pg3.add_stream(p_solicit_vrf1) self.pg_enable_capture(self.pg_interfaces) self.pg_start() rx = self.pg1.get_capture(1) self.verify_dhcp6_solicit(rx[0], self.pg1, dhcp_solicit_src_vrf1, self.pg3.remote_mac, fib_id=1, oui=4) # # Remove the VSS config # relayed DHCP has default vlaues in the option. # self.vapi.dhcp_proxy_set_vss(1, 1, 4, is_ip6=1, is_add=0) self.pg3.add_stream(p_solicit_vrf1) self.pg_enable_capture(self.pg_interfaces) self.pg_start() rx = self.pg1.get_capture(1) self.verify_dhcp6_solicit(rx[0], self.pg1, dhcp_solicit_src_vrf1, self.pg3.remote_mac) # # Add a second DHCP server in VRF 1 # expect clients messages to be relay to both configured servers # self.pg1.generate_remote_hosts(2) server_addr2 = socket.inet_pton(AF_INET6, self.pg1.remote_hosts[1].ip6) self.vapi.dhcp_proxy_config(server_addr2, src_addr_vrf1, rx_table_id=1, server_table_id=1, is_ipv6=1) # # We'll need an ND entry for the server to send it packets # nd_entry = VppNeighbor(self, self.pg1.sw_if_index, self.pg1.remote_hosts[1].mac, self.pg1.remote_hosts[1].ip6, af=AF_INET6) nd_entry.add_vpp_config() # # Send a discover from the client. expect two relayed messages # The frist packet is sent to the second server # We're not enforcing that here, it's just the way it is. # self.pg3.add_stream(p_solicit_vrf1) self.pg_enable_capture(self.pg_interfaces) self.pg_start() rx = self.pg1.get_capture(2) self.verify_dhcp6_solicit(rx[0], self.pg1, dhcp_solicit_src_vrf1, self.pg3.remote_mac) self.verify_dhcp6_solicit(rx[1], self.pg1, dhcp_solicit_src_vrf1, self.pg3.remote_mac, dst_mac=self.pg1.remote_hosts[1].mac, dst_ip=self.pg1.remote_hosts[1].ip6) # # Send both packets back. Client gets both. # p1 = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) / IPv6(dst=self.pg1.local_ip6, src=self.pg1.remote_ip6) / UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) / DHCP6_RelayReply(peeraddr=dhcp_solicit_src_vrf1) / DHCP6OptIfaceId(optlen=4, ifaceid='\x00\x00\x00\x04') / DHCP6OptRelayMsg(optlen=0) / DHCP6_Advertise(trid=1) / DHCP6OptStatusCode(statuscode=0)) p2 = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_hosts[1].mac) / IPv6(dst=self.pg1.local_ip6, src=self.pg1._remote_hosts[1].ip6) / UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) / DHCP6_RelayReply(peeraddr=dhcp_solicit_src_vrf1) / DHCP6OptIfaceId(optlen=4, ifaceid='\x00\x00\x00\x04') / DHCP6OptRelayMsg(optlen=0) / DHCP6_Advertise(trid=1) / DHCP6OptStatusCode(statuscode=0)) pkts = [p1, p2] self.pg1.add_stream(pkts) self.pg_enable_capture(self.pg_interfaces) self.pg_start() rx = self.pg3.get_capture(2) self.verify_dhcp6_advert(rx[0], self.pg3, dhcp_solicit_src_vrf1) self.verify_dhcp6_advert(rx[1], self.pg3, dhcp_solicit_src_vrf1) # # Ensure only solicit messages are duplicated # p_request_vrf1 = (Ether(dst=dmac, src=self.pg3.remote_mac) / IPv6(src=dhcp_solicit_src_vrf1, dst=dhcp_solicit_dst) / UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_CLIENT_PORT) / DHCP6_Request()) self.pg3.add_stream(p_request_vrf1) self.pg_enable_capture(self.pg_interfaces) self.pg_start() rx = self.pg1.get_capture(1) # # Test we drop DHCP packets from addresses that are not configured as # DHCP servers # p2 = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_hosts[1].mac) / IPv6(dst=self.pg1.local_ip6, src="3001::1") / UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) / DHCP6_RelayReply(peeraddr=dhcp_solicit_src_vrf1) / DHCP6OptIfaceId(optlen=4, ifaceid='\x00\x00\x00\x04') / DHCP6OptRelayMsg(optlen=0) / DHCP6_Advertise(trid=1) / DHCP6OptStatusCode(statuscode=0)) self.send_and_assert_no_replies(self.pg1, p2, "DHCP6 not from server") # # Remove the second DHCP server # self.vapi.dhcp_proxy_config(server_addr2, src_addr_vrf1, rx_table_id=1, server_table_id=1, is_ipv6=1, is_add=0) # # Test we can still relay with the first # self.pg3.add_stream(p_solicit_vrf1) self.pg_enable_capture(self.pg_interfaces) self.pg_start() rx = self.pg1.get_capture(1) self.verify_dhcp6_solicit(rx[0], self.pg1, dhcp_solicit_src_vrf1, self.pg3.remote_mac) # # Cleanup # self.vapi.dhcp_proxy_config(server_addr_vrf1, src_addr_vrf1, rx_table_id=1, server_table_id=1, is_ipv6=1, is_add=0) self.vapi.dhcp_proxy_config(server_addr_vrf0, src_addr_vrf0, rx_table_id=0, server_table_id=0, is_ipv6=1, is_add=0) # duplicate delete self.vapi.dhcp_proxy_config(server_addr_vrf0, src_addr_vrf0, rx_table_id=0, server_table_id=0, is_ipv6=1, is_add=0) def test_dhcp_client(self): """ DHCP Client""" hostname = 'universal-dp' self.pg_enable_capture(self.pg_interfaces) # # Configure DHCP client on PG2 and capture the discover sent # self.vapi.dhcp_client(self.pg2.sw_if_index, hostname) rx = self.pg2.get_capture(1) self.verify_orig_dhcp_discover(rx[0], self.pg2, hostname) # # Sned back on offer, expect the request # p_offer = (Ether(dst=self.pg2.local_mac, src=self.pg2.remote_mac) / IP(src=self.pg2.remote_ip4, dst="255.255.255.255") / UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_CLIENT_PORT) / BOOTP(op=1, yiaddr=self.pg2.local_ip4) / DHCP(options=[('message-type', 'offer'), ('server_id', self.pg2.remote_ip4), ('end')])) self.pg2.add_stream(p_offer) self.pg_enable_capture(self.pg_interfaces) self.pg_start() rx = self.pg2.get_capture(1) self.verify_orig_dhcp_request(rx[0], self.pg2, hostname, self.pg2.local_ip4) # # Send an acknowloedgement # p_ack = (Ether(dst=self.pg2.local_mac, src=self.pg2.remote_mac) / IP(src=self.pg2.remote_ip4, dst="255.255.255.255") / UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_CLIENT_PORT) / BOOTP(op=1, yiaddr=self.pg2.local_ip4) / DHCP(options=[('message-type', 'ack'), ('subnet_mask', "255.255.255.0"), ('router', self.pg2.remote_ip4), ('server_id', self.pg2.remote_ip4), ('lease_time', 43200), ('end')])) self.pg2.add_stream(p_ack) self.pg_enable_capture(self.pg_interfaces) self.pg_start() # # We'll get an ARP request for the router address # rx = self.pg2.get_capture(1) self.assertEqual(rx[0][ARP].pdst, self.pg2.remote_ip4) self.pg_enable_capture(self.pg_interfaces) # # At the end of this procedure there should be a connected route # in the FIB # self.assertTrue(find_route(self, self.pg2.local_ip4, 24)) self.assertTrue(find_route(self, self.pg2.local_ip4, 32)) # remove the left over ARP entry self.vapi.ip_neighbor_add_del(self.pg2.sw_if_index, self.pg2.remote_mac, self.pg2.remote_ip4, is_add=0) # # remove the DHCP config # self.vapi.dhcp_client(self.pg2.sw_if_index, hostname, is_add=0) # # and now the route should be gone # self.assertFalse(find_route(self, self.pg2.local_ip4, 32)) self.assertFalse(find_route(self, self.pg2.local_ip4, 24)) # # Start the procedure again. this time have VPP send the client-ID # self.pg2.admin_down() self.sleep(1) self.pg2.admin_up() self.vapi.dhcp_client(self.pg2.sw_if_index, hostname, client_id=self.pg2.local_mac) rx = self.pg2.get_capture(1) self.verify_orig_dhcp_discover(rx[0], self.pg2, hostname, self.pg2.local_mac) self.pg2.add_stream(p_offer) self.pg_enable_capture(self.pg_interfaces) self.pg_start() rx = self.pg2.get_capture(1) self.verify_orig_dhcp_request(rx[0], self.pg2, hostname, self.pg2.local_ip4) # # unicast the ack to the offered address # p_ack = (Ether(dst=self.pg2.local_mac, src=self.pg2.remote_mac) / IP(src=self.pg2.remote_ip4, dst=self.pg2.local_ip4) / UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_CLIENT_PORT) / BOOTP(op=1, yiaddr=self.pg2.local_ip4) / DHCP(options=[('message-type', 'ack'), ('subnet_mask', "255.255.255.0"), ('router', self.pg2.remote_ip4), ('server_id', self.pg2.remote_ip4), ('lease_time', 43200), ('end')])) self.pg2.add_stream(p_ack) self.pg_enable_capture(self.pg_interfaces) self.pg_start() # # At the end of this procedure there should be a connected route # in the FIB # self.assertTrue(find_route(self, self.pg2.local_ip4, 32)) self.assertTrue(find_route(self, self.pg2.local_ip4, 24)) # # remove the DHCP config # self.vapi.dhcp_client(self.pg2.sw_if_index, hostname, is_add=0) self.assertFalse(find_route(self, self.pg2.local_ip4, 32)) self.assertFalse(find_route(self, self.pg2.local_ip4, 24)) if __name__ == '__main__': unittest.main(testRunner=VppTestRunner)