[kea-dev] Is there still any interest for a hook embedding python?

Dave Cole davejohncole at gmail.com
Sat Jan 4 01:28:54 UTC 2020

For people who have been around for a while, I am the Dave Cole who posted
this message:


I tried multiple times while working at the nbn to convince them to
opensource the work I did embedding Python in Kea.  While everyone was
enthusiastic about releasing the code, none of my managers ever seemed to
be able to make in progress along those lines.

I left the nbn close to a year ago and it has been annoying me ever since.
These Christmas holidays I decided to see if I could start from scratch and
embed Python 3.  Turns out these things are a lot easier the second time

In less than 2 weeks I have replicated the work I did at nbn and taken
things a little further.  To ensure I was doing something useful I decided
to re-implement the lease-cmds example hook from the Kea source.

I am wondering if anyone is interested in playing with this work?  Some
more information follows...

I have only been working on IPv4 for now, but have implemented all of the
IPv4 commands except lease4-add and lease4-update in 242 lines of Python.

The embedded interpreter can run Pythons threads.  For a stress test I
wrote a simple hook that uses prometheus-client to export metrics like this:
from kea import *
from ipaddress import IPv4Address, IPv4Network
from prometheus_client import start_http_server, Counter

PKT_RECEIVE = Counter('dhcp4_pkt_receive_total', 'Packets received', ['type'
PKT_SEND = Counter('dhcp4_pkt_send_total', 'Packets sent', ['type'])

class Config:
def __init__(self, conf):
dhcp4 = conf['Dhcp4']
self.options = [Option(DHO_DHCP_LEASE_TIME).setUint32(dhcp4.get(
'valid-lifetime', 7200)),
Option(DHO_DHCP_RENEWAL_TIME).setUint32(dhcp4.get('renew-timer', 1800)),
Option(DHO_DHCP_REBINDING_TIME).setUint32(dhcp4.get('rebind-timer', 3600))]
# snip extra code

def load(handle):
global config, type_to_label
config = Config(CfgMgr().getStagingCfg().toElement())
type_to_label = dict([(v, k[4:].lower())
for k, v in globals().items()
if k.startswith('DHCP')])
return 0
def pkt4_receive(handle):
query = handle.getArgument('query4')
PKT_RECEIVE.labels(type=type_to_label.get(query.getType(), 'unknown')).inc()
# client must request address in Option 82, suboption 1.
o = query.getOption(DHO_DHCP_AGENT_OPTIONS)
if not o:
raise RuntimeError('client must send option %s' % DHO_DHCP_AGENT_OPTIONS)
o = o.getOption(1)
if not o:
raise RuntimeError('missing suboption 1 in option %s' %
handle.setContext('requested-addr', o.getString())
return 0

Then I run another program that runs dhtest (
https://github.com/saravana815/dhtest) many times in parallel sending the
address I want from Kea in option 82.  This lets me confirm that the server
allocated the correct address.

Running the server and stress test on my laptop I get these results:
root at d95ea00a891d:/workdir# python3 examples/stress-test/stress_test.py
--parallel 50 --total 10000
00:01: 713 at 714/sec
00:02: 1564 at 782/sec
00:03: 2364 at 803/sec
00:04: 3042 at 773/sec
00:05: 3756 at 730/sec
00:06: 4470 at 702/sec
00:07: 5203 at 720/sec
00:08: 5929 at 726/sec
00:09: 6703 at 744/sec
00:10: 7368 at 722/sec
00:11: 8089 at 719/sec
00:12: 8809 at 702/sec
00:13: 9499 at 711/sec
total 10000 at 730/sec with 0 errors

In another shell I continuously query prometheus metrics like this:
root at f30313c2a09d:/workdir# while true; do curl -s http://localhost:9100/ |
grep ^dhcp && echo; sleep 0.2; done

This is to give me some confidence that my threading is working correctly.
As soon as the first request hits the server it start producing output a
bit like this:
dhcp4_pkt_receive_total{type="request"} 50.0
dhcp4_pkt_receive_total{type="discover"} 50.0
dhcp4_pkt_send_total{type="ack"} 50.0
dhcp4_pkt_send_total{type="offer"} 50.0

dhcp4_pkt_receive_total{type="request"} 211.0
dhcp4_pkt_receive_total{type="discover"} 211.0
dhcp4_pkt_send_total{type="ack"} 211.0
dhcp4_pkt_send_total{type="offer"} 211.0

dhcp4_pkt_receive_total{type="request"} 389.0
dhcp4_pkt_receive_total{type="discover"} 395.0
dhcp4_pkt_send_total{type="ack"} 389.0
dhcp4_pkt_send_total{type="offer"} 395.0
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.isc.org/pipermail/kea-dev/attachments/20200104/f41b0729/attachment.htm>

More information about the kea-dev mailing list