Skip to content

Commit

Permalink
Release v0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
majek committed Feb 4, 2015
1 parent b50ecff commit 7fecec9
Show file tree
Hide file tree
Showing 15 changed files with 600 additions and 153 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -1,2 +1,4 @@
libpcap.a
pmtud
core

27 changes: 27 additions & 0 deletions LICENSE-BSD-CloudFlare
@@ -0,0 +1,27 @@
Copyright (c) 2015 CloudFlare, Inc. All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
* Neither the name of the CloudFlare, Inc. nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
60 changes: 59 additions & 1 deletion Makefile
Expand Up @@ -32,7 +32,8 @@ all: pmtud

pmtud: libpcap.a src/*.c src/*.h Makefile
$(CC) $(COPTS) \
src/main.c src/utils.c src/pcap.c src/uevent.c \
src/main.c src/utils.c src/net.c src/uevent.c \
src/hashlimit.c src/csiphash.c \
libpcap.a \
$(LDOPTS) \
-o pmtud
Expand All @@ -51,3 +52,60 @@ distclean: clean

format:
clang-format-3.5 -i src/*.c src/*.h


# Release process
# ---------------
GITVER := $(shell git describe --tags --always --dirty=-dev)
VERSION := $(shell python -c 'print "$(GITVER)"[1:].partition("-")[0]')
ITERATION := $(shell python -c 'print ("$(GITVER)"[1:].partition("-")[2] or "0")')
NEXT_VERSION := v0.$(shell python -c 'print int("$(GITVER)"[1:].partition("-")[0][2:]) + 1')

.PHONY: release

release:
@echo "[*] Curr version: $(VERSION)-$(ITERATION)"
@echo "[*] Next version: $(NEXT_VERSION)"
echo "$(NEXT_VERSION) (`date '+%Y%m%d-%H%M'`)" > RELEASE_NOTES.tmp
git log --reverse --date=short --format="- %ad %s" tags/v$(VERSION)..HEAD >> RELEASE_NOTES.tmp
echo "" >> RELEASE_NOTES.tmp
cat RELEASE_NOTES >> RELEASE_NOTES.tmp
mv RELEASE_NOTES.tmp RELEASE_NOTES
git add RELEASE_NOTES
git commit -m "Release $(NEXT_VERSION)"
git tag $(NEXT_VERSION)
@echo "[*] To push the release run:"
@echo "git push origin master; git push origin $(NEXT_VERSION)"

# Build process
# -------------

BIN_PREFIX ?= /usr/local/bin

.PHONY: print-builddeps cf-package

CFDEPENDENCIES = python flex bison valgrind gcc make

print-builddeps:
@echo $(CFDEPENDENCIES) $(DEPENDENCIES)


cf-package:
@echo "[*] resetting submodules"
git submodule sync --quiet
git submodule update --init --recursive --quiet
@echo "[*] rebuilding"
-$(MAKE) clean
-$(MAKE) distclean
$(MAKE) pmtud BUILD=release CC=gcc
cp pmtud $(BIN_PREFIX)

fakeroot fpm -C / \
-s dir \
-t deb \
--deb-compression bzip2 \
-v $(VERSION) \
--iteration $(ITERATION) \
-n pmtud \
$(BIN_PREFIX)/pmtud
rm $(BIN_PREFIX)/pmtud
40 changes: 40 additions & 0 deletions README.md
@@ -0,0 +1,40 @@
Path MTU daemon
===============

With ECMP enabled the ICMP messages are routed mostly to wrong
server. To fix that let's broadcast the ICMP messages that we think
are worth it to every machine in colo. Some reading:

* https://tools.ietf.org/html/draft-jaeggli-v6ops-pmtud-ecmp-problem-00


Path MTU daemon is a program that captures and broadcasts ICMP
messages related to MTU detection. It listens on an interface
waiting for ICMP messages (ip type 3 code 4 or ipv6 type 2 code 0)
and it forwards them verbatim to broadcast ethernet address.

Once again, it listens waiting for packets matching:

((icmp and icmp[0] == 3 and icmp[1] == 4) or
(icmp6 and ip6[40+0] == 2 and ip6[40+1] == 0)) and
(ether dst not ff:ff:ff:ff:ff:ff)

And having appropriate length, and forwards them to ethernet broadcast
ff:ff:ff:ff:ff:ff.

To debug use tcpdump:

sudo tcpdump -s0 -e -ni eth0 '((icmp and icmp[0] == 3 and icmp[1] == 4) or
(icmp6 and ip6[40+0] == 2 and ip6[40+1] == 0))'


To build type:

git submodule update --init --recursive
make


To test run it in dry-run and verbose mode:

sudo ./pmtud --iface=eth0 --dry-run -v -v -v

2 changes: 2 additions & 0 deletions RELEASE_NOTES
@@ -0,0 +1,2 @@
v0.1 (20150131-0037)

5 changes: 0 additions & 5 deletions TODO

This file was deleted.

127 changes: 127 additions & 0 deletions src/csiphash.c
@@ -0,0 +1,127 @@
/* <MIT License>
Copyright (c) 2013 Marek Majkowski <marek@popcount.org>
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.
</MIT License>
Original location:
https://github.com/majek/csiphash/
Solution inspired by code from:
Samuel Neves (supercop/crypto_auth/siphash24/little)
djb (supercop/crypto_auth/siphash24/little2)
Jean-Philippe Aumasson (https://131002.net/siphash/siphash24.c)
*/

#include <stdint.h>

#if defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && \
__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
#define _le64toh(x) ((uint64_t)(x))
#elif defined(_WIN32)
/* Windows is always little endian, unless you're on xbox360
http://msdn.microsoft.com/en-us/library/b0084kay(v=vs.80).aspx */
#define _le64toh(x) ((uint64_t)(x))
#elif defined(__APPLE__)
#include <libkern/OSByteOrder.h>
#define _le64toh(x) OSSwapLittleToHostInt64(x)
#else

/* See: http://sourceforge.net/p/predef/wiki/Endianness/ */
#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
#include <sys/endian.h>
#else
#include <endian.h>
#endif
#if defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && \
__BYTE_ORDER == __LITTLE_ENDIAN
#define _le64toh(x) ((uint64_t)(x))
#else
#define _le64toh(x) le64toh(x)
#endif

#endif

#define ROTATE(x, b) (uint64_t)(((x) << (b)) | ((x) >> (64 - (b))))

#define HALF_ROUND(a, b, c, d, s, t) \
a += b; \
c += d; \
b = ROTATE(b, s) ^ a; \
d = ROTATE(d, t) ^ c; \
a = ROTATE(a, 32);

#define DOUBLE_ROUND(v0, v1, v2, v3) \
HALF_ROUND(v0, v1, v2, v3, 13, 16); \
HALF_ROUND(v2, v1, v0, v3, 17, 21); \
HALF_ROUND(v0, v1, v2, v3, 13, 16); \
HALF_ROUND(v2, v1, v0, v3, 17, 21);

uint64_t siphash24(const void *src, unsigned long src_sz, const char key[16])
{
const uint64_t *_key = (uint64_t *)key;
uint64_t k0 = _le64toh(_key[0]);
uint64_t k1 = _le64toh(_key[1]);
uint64_t b = (uint64_t)src_sz << 56;
const uint64_t *in = (uint64_t *)src;

uint64_t v0 = k0 ^ 0x736f6d6570736575ULL;
uint64_t v1 = k1 ^ 0x646f72616e646f6dULL;
uint64_t v2 = k0 ^ 0x6c7967656e657261ULL;
uint64_t v3 = k1 ^ 0x7465646279746573ULL;

while (src_sz >= 8) {
uint64_t mi = _le64toh(*in);
in += 1;
src_sz -= 8;
v3 ^= mi;
DOUBLE_ROUND(v0, v1, v2, v3);
v0 ^= mi;
}

uint64_t t = 0;
uint8_t *pt = (uint8_t *)&t;
uint8_t *m = (uint8_t *)in;
switch (src_sz) {
case 7:
pt[6] = m[6];
case 6:
pt[5] = m[5];
case 5:
pt[4] = m[4];
case 4:
*((uint32_t *)&pt[0]) = *((uint32_t *)&m[0]);
break;
case 3:
pt[2] = m[2];
case 2:
pt[1] = m[1];
case 1:
pt[0] = m[0];
}
b |= _le64toh(t);

v3 ^= b;
DOUBLE_ROUND(v0, v1, v2, v3);
v0 ^= b;
v2 ^= 0xff;
DOUBLE_ROUND(v0, v1, v2, v3);
DOUBLE_ROUND(v0, v1, v2, v3);
return (v0 ^ v1) ^ (v2 ^ v3);
}
19 changes: 0 additions & 19 deletions src/debug.h

This file was deleted.

92 changes: 92 additions & 0 deletions src/hashlimit.c
@@ -0,0 +1,92 @@
// PMTUD
//
// Copyright (c) 2015 CloudFlare, Inc.
//
// Rate limiting algorithm inspired by linux iptables hashlimit module.
// http://lxr.free-electrons.com/source/net/netfilter/xt_hashlimit.c?v=3.17#L383
// http://lxr.free-electrons.com/source/net/sched/sch_tbf.c?v=3.17#L26

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>

uint64_t siphash24(const void *src, unsigned long src_sz,
const unsigned char key[16]);

#define TIMESPEC_NSEC(ts) ((ts)->tv_sec * 1000000000ULL + (ts)->tv_nsec)
#define MSEC_NSEC(ms) ((ms)*1000000ULL)

inline static uint64_t realtime_now()
{
struct timespec now;
clock_gettime(CLOCK_REALTIME, &now);
return TIMESPEC_NSEC(&now);
}

struct hl_item
{
uint64_t credit;
uint64_t prev;
};

struct hashlimit
{
unsigned size;

uint64_t credit_max;
uint64_t touch_cost;
uint8_t key[16];

struct hl_item items[0];
};

struct hashlimit *hashlimit_alloc(unsigned size, double rate_pps, double burst)
{
struct hashlimit *hl = calloc(1, sizeof(struct hashlimit) +
size * sizeof(struct hl_item));

hl->size = size;
hl->touch_cost = (double)(MSEC_NSEC(1000ULL)) / rate_pps;
hl->credit_max = burst * hl->touch_cost;

/* Random numbers for poor */
uint64_t a = realtime_now() | getpid();
memcpy(&hl->key[0], &a, 8);
a = realtime_now() | getppid();
memcpy(&hl->key[8], &a, 8);

return hl;
}

void hashlimit_free(struct hashlimit *hl) { free(hl); }

int hashlimit_touch(struct hashlimit *hl, unsigned idx)
{
struct hl_item *item = &hl->items[idx];

uint64_t now = realtime_now();
uint64_t delta = now - item->prev;
item->credit += delta;
item->prev = now;

if (item->credit > hl->credit_max) {
item->credit = hl->credit_max;
}

if (item->credit >= hl->touch_cost) {
item->credit -= hl->touch_cost;
return 1;
}
return 0;
}

int hashlimit_touch_hash(struct hashlimit *hl, const uint8_t *h, int h_len)
{

uint64_t hash = siphash24(h, h_len, hl->key);
return hashlimit_touch(hl, hash % hl->size);
}

0 comments on commit 7fecec9

Please sign in to comment.