{"id":1817,"date":"2023-05-31T15:47:50","date_gmt":"2023-05-31T19:47:50","guid":{"rendered":"https:\/\/enterpriseadmins.org\/blog\/?p=1817"},"modified":"2023-05-31T15:47:50","modified_gmt":"2023-05-31T19:47:50","slug":"emulating-a-wide-area-network-with-latency-and-packet-loss","status":"publish","type":"post","link":"https:\/\/enterpriseadmins.org\/blog\/virtualization\/emulating-a-wide-area-network-with-latency-and-packet-loss\/","title":{"rendered":"Emulating a Wide Area Network with Latency and Packet Loss"},"content":{"rendered":"\n<p>Have you ever found yourself needing to simulate network latency or packet loss?  I recently wanted to test a replication scenario but needed to have at least 15ms or so of latency between the source and destination.  <\/p>\n\n\n\n<p>Years ago I used a WANem virtual appliance as a router to do something similar.  This project can be found online (<a href=\"https:\/\/wanem.sourceforge.net\/\">https:\/\/wanem.sourceforge.net<\/a>), however it appears that development has stopped, with the latest releases being nearly 10 years old.  WANem provides a PHP web interface allowing the user to configure latency and packet loss, among other things.  When you apply settings, the web interface issues commands to Linux Traffic Control (<code>tc<\/code>) Network Emulator (<code>netem<\/code>) to enforce those settings.  <\/p>\n\n\n\n<p>Instead of using the WANem appliance, which ships as a very old and unpatched Knoppix Linux distribution, I decided to take the command it ran and try it on an Ubuntu template available in my lab.<\/p>\n\n\n\n<p>I created an Ubuntu VM with 2 network interfaces.  One interface connected to my routable lab network using a static IP (<code>ens192: 192.168.40.11<\/code>) and the other connected to non-routable VLAN 19 that was available and was assigned another static IP (<code>ens224: 192.168.19.1<\/code>) &#8212; which would become the default gateway for this new network.<\/p>\n\n\n\n<p>I then added the following two lines to the <code>\/etc\/rc.local<\/code> file on this system:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>echo \"1\" > \/proc\/sys\/net\/ipv4\/ip_forward\ntc qdisc add dev ens192 root handle 1: netem delay 19ms loss 2%<\/code><\/pre>\n\n\n\n<p>The first line makes the system act as a router, the second adds 19ms of latency and 2% packet loss to traffic which passes the network interface.  More configuration options for this second command can be found here: <a href=\"https:\/\/man7.org\/linux\/man-pages\/man8\/tc-netem.8.html\">https:\/\/man7.org\/linux\/man-pages\/man8\/tc-netem.8.html<\/a>, including how to send corrupt\/duplicate\/reordered packets &#8212; really make your WAN experience terrible.  Since it is in <code>\/etc\/rc.local<\/code> these commands will run automatically when the system starts.<\/p>\n\n\n\n<p>I also wanted this system to act as a DHCP server for the clients at the remote site.  I did this by installing a DHCP server (<code>apt install isc-dhcp-server<\/code>), making environment specific changes to <code>\/etc\/dhcp\/dhcpd.conf<\/code> such as DNS servers and lease-time, and also adding my new subnet with:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>subnet 192.168.19.0 netmask 255.255.255.0 {\n   range 192.168.19.50 192.168.19.99;\n   option subnet-mask 255.255.255.0;\n   option routers 192.168.19.1;\n}<\/code><\/pre>\n\n\n\n<p>To activate these DHCP changes, I ran <code>\/etc\/init.d\/isc-dhcp-server restart<\/code>.  <\/p>\n\n\n\n<p>Finally I added a static route to the primary router in my lab, such that any requests for <code>192.168.19.0\/24<\/code> go to the gateway at <code>192.168.40.11<\/code>.  Now any request in or out of my WAN site has latency injected at some occasional lost packets:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/enterpriseadmins.org\/blog\/wp-content\/uploads\/2023\/05\/image.png\"><img loading=\"lazy\" decoding=\"async\" width=\"481\" height=\"171\" src=\"https:\/\/enterpriseadmins.org\/blog\/wp-content\/uploads\/2023\/05\/image.png\" alt=\"\" class=\"wp-image-1822\" srcset=\"https:\/\/enterpriseadmins.org\/blog\/wp-content\/uploads\/2023\/05\/image.png 481w, https:\/\/enterpriseadmins.org\/blog\/wp-content\/uploads\/2023\/05\/image-300x107.png 300w\" sizes=\"auto, (max-width: 481px) 100vw, 481px\" \/><\/a><\/figure>\n\n\n\n<p>I&#8217;m now able to deploy virtual machines at a &#8220;remote location&#8221; about 19-20ms away, but actually run on the same vSphere cluster.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Have you ever found yourself needing to simulate network latency or packet loss? I recently wanted to test a replication scenario but needed to have at least 15ms or so of latency between the source and destination. Years ago I &hellip; <a href=\"https:\/\/enterpriseadmins.org\/blog\/virtualization\/emulating-a-wide-area-network-with-latency-and-packet-loss\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":6,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[9,4],"tags":[],"class_list":["post-1817","post","type-post","status-publish","format-standard","hentry","category-lab-infrastructure","category-virtualization"],"_links":{"self":[{"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/posts\/1817","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/comments?post=1817"}],"version-history":[{"count":5,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/posts\/1817\/revisions"}],"predecessor-version":[{"id":1823,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/posts\/1817\/revisions\/1823"}],"wp:attachment":[{"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/media?parent=1817"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/categories?post=1817"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/tags?post=1817"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}