Skip to content

Commit

Permalink
Introduce new options cluster_id and cpu_affinity
Browse files Browse the repository at this point in the history
  • Loading branch information
ates committed Oct 23, 2012
1 parent fa56620 commit fb36eae
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
11 changes: 10 additions & 1 deletion README.md
Expand Up @@ -41,7 +41,7 @@ epcap includes a small example program called sniff.
Types Args = [Options] Types Args = [Options]
Options = {chroot, string()} | {group, string()} | {interface, string()} | {promiscuous, boolean()} | Options = {chroot, string()} | {group, string()} | {interface, string()} | {promiscuous, boolean()} |
{user, string()} | {filter, string()} | {progname, string()} | {file, string()} | {user, string()} | {filter, string()} | {progname, string()} | {file, string()} |
{monitor, boolean()} {monitor, boolean() | {cpu_affinity, string()} | {cluster_id, non_neg_integer()}}


Packets are delivered as messages: Packets are delivered as messages:


Expand Down Expand Up @@ -70,6 +70,15 @@ epcap includes a small example program called sniff.


As a result epcap binary will be linked with the following flags: -static -lpfring -lpthread As a result epcap binary will be linked with the following flags: -static -lpfring -lpthread


To complete the configuration you need to set up the cluster_id option.
The value of the cluster_id option is integer and should be in range between 0 and 255.

epcap:start([{interface, "lo"}, {cluster_id, 2}]).

You can also specify the option cpu_affinity to set up CPU affinity for epcap port:

epcap:start([{interface, "lo"}, {cluster_id, 2}, {cpu_affinity, "1,3,5-7"}]).



## SCREENSHOT ## SCREENSHOT


Expand Down
30 changes: 25 additions & 5 deletions src/epcap.erl
Expand Up @@ -65,6 +65,7 @@ init([Pid, Options]) ->
_ -> 500 _ -> 500
end, end,
Cmd = make_args(Options ++ [{chroot, Chroot}, {timeout, Timeout}]), Cmd = make_args(Options ++ [{chroot, Chroot}, {timeout, Timeout}]),
io:format("CMD: ~p~n", [Cmd]),
Port = open_port({spawn, Cmd}, [{packet, 2}, binary, exit_status]), Port = open_port({spawn, Cmd}, [{packet, 2}, binary, exit_status]),
{ok, #state{pid = Pid, port = Port}}. {ok, #state{pid = Pid, port = Port}}.


Expand Down Expand Up @@ -108,7 +109,7 @@ make_args(PL) ->
true -> ""; true -> "";
false -> "sudo " false -> "sudo "
end, end,
proplists:get_value(progname, PL, Sudo ++ progname()) ++ " " ++ proplists:get_value(progname, PL, Sudo ++ pfring(PL) ++ cpu_affinity(PL) ++ progname()) ++ " " ++
string:join([get_switch(proplists:lookup(Arg, PL)) || Arg <- [ string:join([get_switch(proplists:lookup(Arg, PL)) || Arg <- [
chroot, chroot,
group, group,
Expand All @@ -120,9 +121,9 @@ make_args(PL) ->
snaplen, snaplen,
timeout, timeout,
verbose, verbose,

filter,
filter pfring
], proplists:lookup(Arg, PL) /= none ], " "). ], proplists:lookup(Arg, PL) /= none], " ").


get_switch({chroot, Arg}) -> "-d " ++ Arg; get_switch({chroot, Arg}) -> "-d " ++ Arg;
get_switch({file, Arg}) -> "-f " ++ Arg; get_switch({file, Arg}) -> "-f " ++ Arg;
Expand All @@ -136,9 +137,10 @@ get_switch({user, Arg}) -> "-u " ++ Arg;
get_switch({verbose, Arg}) -> string:copies("-v ", Arg); get_switch({verbose, Arg}) -> string:copies("-v ", Arg);
get_switch({filter, Arg}) -> "\"" ++ Arg ++ "\"". get_switch({filter, Arg}) -> "\"" ++ Arg ++ "\"".


-spec basedir() -> string().
basedir() -> basedir() ->
case code:priv_dir(?MODULE) of case code:priv_dir(?MODULE) of
{error,bad_name} -> {error, bad_name} ->
filename:join([ filename:join([
filename:dirname(code:which(?MODULE)), filename:dirname(code:which(?MODULE)),
"..", "..",
Expand All @@ -149,8 +151,26 @@ basedir() ->
Dir Dir
end. end.


-spec progname() -> string().
progname() -> progname() ->
filename:join([basedir(), ?MODULE]). filename:join([basedir(), ?MODULE]).


-spec chroot_path() -> string().
chroot_path() -> chroot_path() ->
filename:join([basedir(), "tmp"]). filename:join([basedir(), "tmp"]).

-spec pfring([proplists:property()]) -> string().
pfring(Options) ->
case proplists:get_value(cluster_id, Options) of
undefined -> "";
Value ->
"PCAP_PF_RING_CLUSTER_ID=" ++ integer_to_list(Value) ++ " "
end.

-spec cpu_affinity([proplists:property()]) -> string().
cpu_affinity(Options) ->
case proplists:get_value(cpu_affinity, Options) of
undefined -> "";
CPUs ->
"taskset -c " ++ CPUs ++ " "
end.

0 comments on commit fb36eae

Please sign in to comment.