• Stars
    star
    129
  • Rank 279,262 (Top 6 %)
  • Language
    C
  • Created about 13 years ago
  • Updated over 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Mocking ideally slow network that only allows reading and/or writing one byte at a time

Description

Build Status

This tool emulates an ideally slow network by mocking the polling and read/write syscalls exposed by glibc via the LD_PRELOAD technique.

By preloading this dynamic library to your network server process, it will intercept the "poll", "close", "send", and "writev" syscalls, only allow the writing syscalls to actually write a single byte at a time (without flushing), and returns EAGAIN until another "poll" called on the current socket fd.

Similarly, one can configure this library to intercept the "read", "recv", "recvfrom" calls on the C level to emulate extremely slow reading operations either with or without slow writes at the same time.

The socket fd must be called first by a "poll" call to mark itself to this tool as an "active fd" and trigger subsequence the writing syscalls to behave differently.

With this tool, one can emulate extreme network conditions even locally (with the loopback device).

This tool is intended to supersed the good old etcproxy tool.

Build

Just issue the following command to build the file mockeagain.so

make

The Gnu make and gcc compiler are required.

On *BSD, it's often required to run the command "gmake".

Usage

For now, only "poll" syscall and slow writing mocking are supported.

Here we take Nginx as an example:

  1. Ensure that you have passed the --with-poll_module option while invoking nginx's configure script to build nginx.

  2. Ensure that you've enabled the "poll" event model in your nginx.conf:

    events { use poll; worker_connections 1024; }

  3. Ensure that you've disabled nginx's own write buffer:

    postpone_output 1; # only postpone a single byte, default 1460 bytes

and also have the following line in your nginx.conf:

env LD_PRELOAD;
  1. Run your Nginx this way:

    MOCKEAGAIN=w LD_PRELOAD=/path/to/mockeagain.so /path/to/nginx ...

Environments

The following system environment variables can be used to control the behavior of this tool. You should use Nginx's standard "env" directive to enable the environments that you use in your nginx.conf file, as in

env MOCKEAGAIN_VERBOSE;
env MOCKEAGAIN;
env MOCKEAGAIN_WRITE_TIMEOUT_PATTERN;

If you're using the Test::Nginx test scaffold, then these directives are automatically configured for you.

MOCKEAGAIN

This environment controls whether to mocking reads or writes or both.

When the letter "r" or "R" appear in the value string, then reads will be mocked.

When the letter "w" or "W" appear in the value string, then writes will be mocked.

When this environment is either not set or set to something unrecognized, then no mocking will be performed.

MOCKEAGAIN_VERBOSE

You can set the environment MOCKEAGAIN_VERBOSE to 1 if you want more diagnostical outputs of this tool in stderr. But you'll also have to add the following line to your nginx.conf:

env MOCKEAGAIN_VERBOSE;

When setting MOCKEAGAIN_VERBOSE to 1, you'll get messages like the following in your nginx's error.log file:

mockeagain: mocking "writev" on fd 3 to signal EAGAIN.
mockeagain: mocking "writev" on fd 3 to emit 1 of 188 bytes.
mockeagain: mocking "writev" on fd 3 to emit 1 of 187 bytes.
mockeagain: mocking "recv" on fd 5 to read 1 byte of data only

MOCKEAGAIN_WRITE_TIMEOUT_PATTERN

When this environment is set to value "foo bar", then the when the string "foo bar" appears in the output stream (not necessarily in a single write call), then it will trigger an indefinite write timeout.

This feature is very useful in mocking a write timeout in a particular position in the output stream.

Note that this environment also requires that the MOCKEAGAIN variable value contains "w" or "W".

For now, this feature only supports the "writev" call.

Glibc API Mocked

Event API

  • poll

Writing API

  • writev
  • send

Reading API

  • read
  • recv
  • recvfrom

Tests

mockeagain has a simple testing suite.

To run tests, simply type make test. Note that a working standard Python is needed to run the tests.

To run tests with Valgrind, run make test VALGRIND=1 instead.

To write a new test:

Copy one of the existing test cases to get started. Test case files are always named in the format of ###-name.c. You need to implement minimum the run_test() function. run_test() function takes a single parameter, fd which refers to a valid TCP socket that has been connected to an echo server and set to non-blocking mode. You are expected to return zero when all tests passed and non zero when one or more tests failed.

Your test case is responsible for enabling read/write mocks. There is a helper function set_mocking() that accepts a combination of MOCKING_READS and MOCKING_WRITES to enable read and/or write mocking. There is another function set_write_timeout_pattern() that sets the MOCKEAGAIN_WRITE_TIMEOUT_PATTERN environment variable.

Note: In general, set_mocking() and set_write_timeout_pattern() should be called before invocation of any mocked functions above. This is due to the fact that mockeagain will lazy load those settings and cache them forever afterwards.

TODO

  • add support for write, sendto, and more writing syscalls.
  • add support for other event interfaces like epoll, select, kqueue, and more.

Success Stories

ngx_echo

The echo_after_body directive used to have a bug that the output filter failed to take into account the NGX_AGAIN constant returned from the downstream output filter chain. Enabling mockeagain's writing mode can trivially capture this issue even on localhost.

To reproduce, update ngx_http_echo_filter.c:165

#if 0

to

#if 1

and run ngx_echo's test file t/echo-after-body.t with mockeagain's writing mode enabled.

ngx_redis

ngx_redis 0.3.5 and earlier had a subtle bug in its redis reply parser as explained here:

https://groups.google.com/group/openresty/browse_thread/thread/b5ddf1b24d3c9677

Even though this bug was first captured by the etcproxy tool, but mockeagain's reading mode can also capture it reliably even on localhost.

To reproduce, just compile ngx_redis 0.3.5 with ngx_srcache, and then run the t/redis.t test file in ngx_srcache's test suite, and with mockeagain reading mode enabled.

libdrizzle

libdrizzle 0.8 and earlier had a subtle bug in its MySQL packet parser and mockeagain's reading mode can capture it reliably even on localhost.

To reproduce, just compile libdrizzle 0.8 with ngx_drizzle, and run the t/sanity.t test file in ngx_drizzle's test suite with mockeagain's reading mode enabled.

ngx_lua

The downstream cosocket API did have a bug in its receive(size) method call that was caught by mockeagain in its reading mode.

To reproduce, check out ngx_lua v0.5.0rc24 and run its t/067-req-socket.t test file with "MOCKEAGAIN=R" and TEST 1 will just hang.

Author

Yichun "agentzh" Zhang (η« δΊ¦ζ˜₯)

Copyright & License

This module is licenced under the BSD license.

Copyright (C) 2012-2017 by Yichun "agentzh" Zhang (η« δΊ¦ζ˜₯) [email protected], OpenResty 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.

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.

More Repositories

1

openresty

High Performance Web Platform Based on Nginx and LuaJIT
C
12,431
star
2

lua-nginx-module

Embed the Power of Lua into NGINX HTTP servers
C
11,224
star
3

nginx-tutorials

Nginx Tutorials
Perl
2,864
star
4

lua-resty-redis

Lua redis client driver for the ngx_lua based on the cosocket API
Lua
1,893
star
5

openresty-systemtap-toolkit

Real-time analysis and diagnostics tools for OpenResty (including NGINX, LuaJIT, ngx_lua, and more) based on SystemTap
Perl
1,653
star
6

headers-more-nginx-module

Set, add, and clear arbitrary output headers in NGINX http servers
C
1,636
star
7

openresty.org

Code and data for the openresty.org site
HTML
1,262
star
8

luajit2

OpenResty's Branch of LuaJIT 2
C
1,229
star
9

echo-nginx-module

An Nginx module for bringing the power of "echo", "sleep", "time" and more to Nginx's config file
C
1,162
star
10

docker-openresty

Docker tooling for OpenResty
Dockerfile
934
star
11

redis2-nginx-module

Nginx upstream module for the Redis 2.0 protocol
C
902
star
12

lua-resty-limit-traffic

Lua library for limiting and controlling traffic in OpenResty/ngx_lua
Lua
815
star
13

lua-resty-core

New FFI-based API for lua-nginx-module
Lua
797
star
14

stream-lua-nginx-module

Embed the power of Lua into NGINX TCP/UDP servers
C
723
star
15

lua-resty-mysql

Nonblocking Lua MySQL driver library for ngx_lua or OpenResty
Lua
703
star
16

stapxx

Simple macro language extentions to systemtap
Perl
692
star
17

sregex

A non-backtracking NFA/DFA-based Perl-compatible regex engine matching on large data streams
C
616
star
18

lua-resty-upstream-healthcheck

Health Checker for Nginx Upstream Servers in Pure Lua
Lua
513
star
19

lua-resty-websocket

WebSocket support for the ngx_lua module (and OpenResty)
Lua
503
star
20

lua-upstream-nginx-module

Nginx C module to expose Lua API to ngx_lua for Nginx upstreams
C
499
star
21

srcache-nginx-module

Transparent subrequest-based caching layout for arbitrary nginx locations.
C
474
star
22

opm

OpenResty Package Manager
Lua
458
star
23

test-nginx

Data-driven test scaffold for Nginx C module and OpenResty Lua library development
Perl
438
star
24

lua-resty-lrucache

Lua-land LRU Cache based on LuaJIT FFI
Lua
437
star
25

lua-resty-string

String utilities and common hash functions for ngx_lua and LuaJIT
Lua
426
star
26

lua-resty-upload

Streaming reader and parser for http file uploading based on ngx_lua cosocket
Lua
401
star
27

set-misc-nginx-module

Various set_xxx directives added to nginx's rewrite module (md5/sha1, sql/json quoting, and many more)
C
388
star
28

drizzle-nginx-module

an nginx upstream module that talks to mysql and drizzle by libdrizzle
C
336
star
29

openresty-gdb-utils

GDB Utilities for OpenResty (including Nginx, ngx_lua, LuaJIT, and more)
Python
335
star
30

lua-resty-balancer

A generic consistent hash implementation for OpenResty/Lua
Lua
325
star
31

lua-resty-dns

DNS resolver for the nginx lua module
Lua
321
star
32

programming-openresty

Programming OpenResty Book
Perl
317
star
33

lua-resty-lock

Simple nonblocking lock API for ngx_lua based on shared memory dictionaries
Lua
304
star
34

openresty-devel-utils

Utilities for nginx module development
Perl
266
star
35

resty-cli

Fancy command-line utilities for OpenResty
Perl
263
star
36

replace-filter-nginx-module

Streaming regular expression replacement in response bodies
C
258
star
37

memc-nginx-module

An extended version of the standard memcached module that supports set, add, delete, and many more memcached commands.
C
213
star
38

lua-resty-memcached

Lua memcached client driver for the ngx_lua based on the cosocket API
Lua
210
star
39

encrypted-session-nginx-module

encrypt and decrypt nginx variable values
C
198
star
40

openresty-packaging

Official OpenResty packaging source and scripts for various Linux distributions and other systems
Makefile
176
star
41

rds-json-nginx-module

An nginx output filter that formats Resty DBD Streams generated by ngx_drizzle and others to JSON
C
153
star
42

xss-nginx-module

Native support for cross-site scripting (XSS) in an nginx
C
148
star
43

lua-resty-shell

Lua module for nonblocking system shell command executions
Perl
124
star
44

lua-tablepool

Lua table recycling pools for LuaJIT
Perl
112
star
45

lua-redis-parser

Lua module for parsing raw redis responses
C
93
star
46

openresty-survey

OpenResty Web App for OpenResty User Survey
HTML
90
star
47

lua-ssl-nginx-module

NGINX C module that extends ngx_http_lua_module for enhanced SSL/TLS capabilities
Lua
86
star
48

opsboy

A rule-based sysadmin tool that helps setting up complex environment for blank machines
Perl
85
star
49

no-pool-nginx

replace nginx's pool mechanism with plain malloc & free to help tools like valgrind
Shell
77
star
50

stream-echo-nginx-module

TCP/stream echo module for NGINX (a port of ngx_http_echo_module)
C
69
star
51

meta-lua-nginx-module

Meta Lua Nginx Module supporting both Http Lua Module and Stream Lua Module
C
66
star
52

array-var-nginx-module

Add support for array-typed variables to nginx config files
C
66
star
53

lemplate

OpenResty/Lua template framework implementing Perl's TT2 templating language
Perl
54
star
54

openresty-con

JavaScript
48
star
55

nginx-dtrace

An nginx fork that adds dtrace USDT probes
C
44
star
56

orbpf-ko

The orbpf (eBPF+) Linux kernel module from OpenResty Inc.
C
37
star
57

lua-resty-memcached-shdict

Powerful memcached client with a shdict caching layer and many other features
Lua
33
star
58

lua-resty-signal

Lua library for killing or sending signals to UNIX processes
Perl
32
star
59

lua-resty-shdict-simple

Simple applicaton-oriented interface to the OpenResty shared dictionary API
Perl
32
star
60

luajit2-test-suite

OpenResty's LuaJIT test suite based on Mike Pall's LuaJIT tests
Lua
29
star
61

ngx_postgres

OpenResty's fork of FRiCKLE/ngx_postgres
C
26
star
62

rds-csv-nginx-module

Nginx output filter module to convert Resty-DBD-Streams (RDS) to Comma-Separated Values (CSV)
C
22
star
63

showman-samples

Sample screenplay files for generating our public video tutorials using OpenResty Showman
20
star
64

lua-rds-parser

Resty DBD Stream (RDS) parser for Lua written in C
C
19
star
65

redis-nginx-module

8
star
66

AB-test-http

test http requests between two systems.
Perl
5
star
67

transparency

2
star