存档

‘网络技术’ 分类的存档

FreeBSD: Redundant DNS with CARP

2010年12月11日 16hot 没有评论

http://zi0r.com/2010/02/28/freebsd-redundant-dns-with-carp.html

Improve reliability with CARP and redundant DNS on two boxes. Almost everyone already has (at least) two nameservers, so why not add CARP into the mix to make it appear as if they’re always available?

This setup assumes two nameservers. Both nameservers will have a total of 3 IPs assigned to them, two of which are the floating CARP IPs and one of which is a unique management IP for each box. Each nameserver will be the CARP backup for the other nameservers primary nameserver IP. If one goes down, the other will assume responsibility for the IP.

You’ll need to assign your nameservers two new IPs (to be used for management) and take the original pair of IPs and we’ll use those for the carp interfaces.

1. Rebuild/install your kernel with:
device carp

2. Edit /etc/rc.conf to add:
cloned_interfaces=“carp0 carp1”
ifconfig_carp0=“create”
ifconfig_carp1=“create”

3. Setup the CARP interfaces:

I set the vhid’s to be the last octet of the floaty IP, however, you can set them to be whatever you want as long as they match on both boxes.

On ns1:
Create /etc/start_if.carp0 with the following:
#!/bin/sh
ifconfig carp0 vhid XX advbase 1 advskew 10 pass supersecretpasswordhere netmask

Create /etc/start_if.carp1 with the following:
#!/bin/sh
ifconfig carp1 vhid YY advbase 2 advskew 10 pass othersupersecretpasswordhere netmask

Then run:
chmod go-rwx /etc/start_if.carp*;chmod +x /etc/start_if.carp*

On ns2:
Create /etc/start_if.carp0 with the following:
#!/bin/sh
ifconfig carp0 vhid YY advbase 1 advskew 10 pass supersecretpasswordhere netmask

Create /etc/start_if.carp1 with the following:
#!/bin/sh
ifconfig carp1 vhid XX advbase 2 advskew 10 pass othersupersecretpasswordhere netmask

Then run:
chmod go-rwx /etc/start_if.carp*;chmod +x /etc/start_if.carp*

  • carp0 on both boxes will be its PRIMARY IP whereas carp1 will be the SECONDARY IP on both. In a non-failover scenario, this means that carp0 on both boxes should show up as MASTER and carp1 should show up as BACKUP.

4. Ensure named is configured to bind to the management IP (for zone transfers, etc.), ns1.ip and ns2.ip (on both boxes!) or ensure that it listens on *.

5. Add net.inet.carp.log=2 to /etc/sysctl.conf for some extra logging info.

6. Reboot. Your primary box should come up with ns1.ip as MASTER and ns2.ip as BACKUP. Your secondary box should come up with ns2.ip as MASTER and ns1.ip as BACKUP. Check ifconfig and dmesg to confirm.

  • Note: CARP traffic is multicast and you may need to alter firewalls as appropriate to allow it. The destination is VRRP.MCAST.NET/224.0.0.18.
    For defining masters/slaves, etc. in bind you will want to reference the management IPs of the boxes, not the floating CARP addresses.
  • Note: You can actually skip using the start_if.* files if you elect to put the ifconfig statements into rc.conf. In order to limit access to your CARP authentication key, you would need to change the permissions on rc.conf which could be bad in certain situations.
  • ESX Note: If you’re trying to do this with a box in VMware, you’ll need to disable the vSwitch security features (accept: promisc, forged transmits, mac changes). This is not advisable in production as any VM on that switch can sniff traffic from any other VM. For my setup at home, I simply allocated a second NIC and a second vSwitch and made the security changes on the dedicated vSwitch. No other VMs should share this other vSwitch where the security features have been disabled.
分类: BSD/linux, DNS, 转载 标签: , ,

FreeBSD下使用puttygen key登录其他服务器

2010年11月27日 16hot 没有评论

先帖个man,回头再仔细整理。

# man puttygen
Formatting page, please wait…Done.
puttygen(1)                    PuTTY tool suite                    puttygen(1)

NAME
puttygen – public-key generator for the PuTTY tools

SYNOPSIS
puttygen ( keyfile | -t keytype [ -b bits ] )
[ -C new-comment ] [ -P ] [ -q ]
[ -O output-type | -l | -L | -p ]
[ -o output-file ]

DESCRIPTION
puttygen  is  a  tool to generate and manipulate SSH public and private
key pairs. It is part of the PuTTY suite, although it can also interop-
erate with the private key formats used by some other SSH clients.

When  you  run puttygen, it does three things. Firstly, it either loads
an existing key file (if you specified keyfile), or generates a new key
(if  you specified keytype). Then, it optionally makes modifications to
the key (changing the comment and/or the passphrase); finally, it  out-
puts the key, or some information about the key, to a file.

All  three  of  these phases are controlled by the options described in
the following section.

OPTIONS
In the first phase, puttygen either  loads  or  generates  a  key.  The
options to control this are:

keyfile
Specify  a  private key file to be loaded. This private key file
can be in the (de  facto  standard)  SSH-1  key  format,  or  in
PuTTY’s  SSH-2 key format, or in either of the SSH-2 private key
formats used by OpenSSH and ssh.com’s implementation.

-t keytype
Specify a type of key to generate. The  acceptable  values  here
are  rsa and dsa (to generate SSH-2 keys), and rsa1 (to generate
SSH-1 keys).

-b bits
Specify the size of the key to generate,  in  bits.  Default  is
1024.

-q     Suppress the progress display when generating a new key.

In  the  second phase, puttygen optionally alters properties of the key
it has loaded or generated. The options to control this are:

-C new-comment
Specify a comment string  to  describe  the  key.  This  comment
string  will  be  used by PuTTY to identify the key to you (when
asking you to enter the passphrase, for  example,  so  that  you
know which passphrase to type).

-P     Indicate  that  you want to change the key’s passphrase. This is
automatic when you are generating a new key, but  not  when  you
are modifying an existing key.

In the third phase, puttygen saves the key or information about it. The
options to control this are:

-O output-type
Specify the type of output you want puttygen to produce. Accept-
able options are:

private
Save  the  private  key in a format usable by PuTTY. This
will either be the standard SSH-1 key format, or  PuTTY’s
own SSH-2 key format.

public Save  the  public  key only. For SSH-1 keys, the standard
public key format will be used  (`1024  37  5698745…’).
For SSH-2 keys, the public key will be output in the for-
mat specified by RFC 4716, which  is  a  multi-line  text
file  beginning with the line `—- BEGIN SSH2 PUBLIC KEY
—-’.

public-openssh
Save the public key only, in a format usable by  OpenSSH.
For SSH-1 keys, this output format behaves identically to
public. For SSH-2 keys, the public key will be output  in
the  OpenSSH  format,  which  is  a single line (`ssh-rsa
AAAAB3NzaC1yc2…’).

fingerprint
Print the fingerprint of the public key. All fingerprint-
ing algorithms are believed compatible with OpenSSH.

private-openssh
Save  an  SSH-2  private  key  in  OpenSSH’s format. This
option is not permitted for SSH-1 keys.

private-sshcom
Save an SSH-2  private  key  in  ssh.com’s  format.  This
option is not permitted for SSH-1 keys.

If no output type is specified, the default is private.

-o output-file
Specify the file where puttygen should write its output. If this
option is not specified, puttygen will assume you want to  over-
write  the  original file if the input and output file types are
the same (changing a comment or passphrase), and will assume you
want  to  output to stdout if you are asking for a public key or
fingerprint. Otherwise, the -o option is required.

-l     Synonym for `-O fingerprint’.

-L     Synonym for `-O public-openssh’.

-p     Synonym for `-O public’.

The following options do not run PuTTYgen as normal, but print informa-
tional messages and then quit:

-h, –help
Display a message summarizing the available options.

-V, –version
Display the version of PuTTYgen.

–pgpfp
Display the fingerprints of the PuTTY PGP Master Keys, to aid in
verifying new files released by the PuTTY team.

EXAMPLES
To generate an SSH-2 RSA key pair and save it  in  PuTTY’s  own  format
(you will be prompted for the passphrase):

puttygen -t rsa -C “my home key” -o mykey.ppk

To generate a larger (2048-bit) key:

puttygen -t rsa -b 2048 -C “my home key” -o mykey.ppk

To change the passphrase on a key (you will be prompted for the old and
new passphrases):

puttygen -P mykey.ppk

To change the comment on a key:

puttygen -C “new comment” mykey.ppk

To convert a key into OpenSSH’s private key format:

puttygen mykey.ppk -O private-openssh -o my-openssh-key

To convert a key  from  another  format  (puttygen  will  automatically
detect the input key type):

puttygen my-ssh.com-key -o mykey.ppk

To  display  the  fingerprint  of  a  key  (some  key  types  require a
passphrase to extract even this much information):

puttygen -l mykey.ppk

To add the OpenSSH-format public half of a key to your authorised  keys
file:

puttygen -L mykey.ppk >> $HOME/.ssh/authorized_keys

BUGS
There’s  currently  no way to supply passphrases in batch mode, or even
just to specify that you don’t want a passphrase at all.

PuTTY tool suite                  2004-03-24                       puttygen(1)

http://www.electrictoolbox.com/putty-rsa-dsa-keys/

分类: BSD/linux, 网络技术 标签: , ,

freebsd8 mpd5配置pptp

2010年11月19日 16hot 没有评论

今天配置了个pptp,自己懒得写文档,就转一篇网文。

这是防火墙规则,做NAT转发:

lop_if = “lo0″
ext_if = “re0″
sync_if= “re0″
ext_carp = “carp0″

set block-policy drop
set loginterface $ext_if

set skip on $lop_if

scrub in

# nat/rdr
nat on $ext_if from !($ext_if) -> ($ext_if:0)
nat-anchor “ftp-proxy/*”
rdr-anchor “ftp-proxy/*”
#block in

#pass in keep state
pass out keep state

anchor “ftp-proxy/*”

pass quick on { $sync_if } proto pfsync keep state (no-sync)
pass on { $ext_if } proto carp keep state

下面是转载内容:

---------------------------

mpd5 on FreeBSD 8.0-RELEASE-p2
步驟1.安裝 mpd5 套件

切換至 Ports Tree 路徑安裝 mpd 套件

#cd  /usr/ports/net/mpd5          //切換到安裝路徑
#make install clean               //安裝套件並清除安裝過程中產生不必要檔案

步驟2.修改 mpd 設定檔 (mpd.conf)

修改 mpd 設定檔 (mpd.conf) 內容如下

#vi /usr/local/etc/mpd5/mpd.conf          //修改內容如下
startup:
set user weithenn weithenn1688     //使用 Web 登入查看 VPN 狀態時的登入帳號及密碼
set web self 61.60.59.58 5006      //Web 查看 VPN 狀態的 IP 及 Port (此例為 http://61.60.59.58:5006)
set web open                       //開啟 Web
default:
load pptp_server
### Server IP 為 Gateway IP (192.168.88.1)
### Client IP 為 VPN Client 屆時連通後發給的 IP (192.168.88.56 ~ 60)
pptp_server:
set ippool add LANPOOL 192.168.88.56 192.168.88.60
create bundle template VPN
set iface disable on-demand
set iface idle 0
set iface enable proxy-arp
set iface enable tcpmssfix
set ipcp yes vjcomp
set ipcp ranges 192.168.88.1/32 ippool LANPOOL
set bundle enable compression
set ccp yes mppc
set mppc no e40
set mppc yes e128
set mppc yes stateless
create link template VPNLINK pptp
set link action bundle VPN
set link enable multilink
set link yes acfcomp protocomp
set link no pap chap
set link enable chap
set link keep-alive 30 300
set link mtu 1460
set pptp self 61.60.59.58
set link enable incoming

步驟3.修改 mpd 密碼檔 (mpd.secret)

修改 mpd 密碼檔 (mpd.secret) 內容如下,由於此密碼檔為 明碼 的文字檔案,因此設定完成後建議將檔案權限設定為 600。

#/usr/local/etc/mpd/mpd.secret        //修改內容如下
weithenn    “vpn123″                  //設定 VPN 帳號及密碼
#chmod 600 mpd.secret                 //設定檔案權限

步驟4.修改 rc.conf

修改 /etc/rc.conf 檔以便系統重新開機時能自動啟動 mpd 服務

#vi /etc/rc.conf                      //修改 rc.conf 內容如下
gateway_enable=”YES”                  //啟動 Forwarding IP Packet (net.inet.ip.forwarding:1)
mpd_flags=”-b”                        //加入此行,背景執行
mpd_enable=”YES”                      //加入此行

步驟 5.建立 mpd.log

建立 mpd.log 及修改 /etc/syslog.conf 檔以便將 mpd5 運作相關訊息寫入 mpd.log 內

#vi /etc/syslog.conf                  //加入下列二行
!mpd
*.*                                             /var/log/mpd.log
#touch /var/log/mpd.log               //建立 Log 檔
#/etc/rc.d/syslogd reload             //重新載入 syslog.conf 設定

步驟6.啟動 mpd 服務

鍵入如下指令啟動 mpd 服務

#/usr/local/etc/rc.d/mpd5   start     //啟動
stop      //停止
restart   //重新啟動
rcvar     //顯示可加入至 rc.conf 的參數
status    //目前執行狀態

檢查 mpd 執行序是否執行

#ps aux |grep mpd
root      1751  0.0  0.3 13452  5596  ??  Is   12:26PM   0:00.24 /usr/local/sbin/mpd5 -p /var/run/mpd5.pid -b

檢查 mpd 服務是否開啟對應 Port 1723 (可參考 /etc/service) 及剛才指定 Web 觀看 VPN 狀態的 Port 5006

#sockstat | grep mpd
root     mpd5        23468 15 tcp4   61.60.59.58:1723      *:*
root     mpd5        23468 15 tcp4   61.60.59.58:5006      *:*

分类: BSD/linux, 网络技术, 转载 标签: ,

线程模式下工作

2010年11月8日 16hot 没有评论

这几天将memcached的线程模式移植到ednsd来,性能有所提升,但是没有以为中的高倍提升。下面是不连redis的统计。

Queries per second:   51600.484825 qps
Queries per second:   51664.028522 qps
Queries per second:   49371.465948 qps
Queries per second:   48488.016842 qps
Queries per second:   46953.006773 qps

Queries sent:         4485137 queries
Queries completed:    4485137 queries

经过测试数据表明,不是开的线程越多,性能越高,而是跟CPU(或者内核)数对应,性能最高。比如4核的CPU,4个线程,性能是最优的。另外,在10万个数据请求时,只有3.6万QPS。在440万个数据请求时,能上到5万QPS左右。

另一方面也说明了,线程模式在高负载的情况下,才能发挥性能。否则有点杀鸡用牛刀之嫌。

分类: C/C++, DNS 标签:

ednsd-0.0.1 test

2010年10月25日 16hot 没有评论

% /opt/bin/queryperf -s 127.0.0.1 -p 5053 -d /tmp/redis-test.conf

DNS Query Performance Testing Tool
Version: $Id: queryperf.c,v 1.12 2007/09/05 07:36:04 marka Exp $

[Status] Processing input data
[Status] Sending queries (beginning with 127.0.0.1)
[Status] Testing complete

Statistics:

Parse input file:     once
Ended due to:         reaching end of file

Queries sent:         1711643 queries
Queries completed:    1711643 queries
Queries lost:         0 queries
Queries delayed(?):   0 queries

RTT max:              0.137409 sec
RTT min:              0.000249 sec
RTT average:          0.001683 sec
RTT std deviation:    0.000562 sec
RTT out of range:     0 queries

Percentage completed: 100.00%
Percentage lost:        0.00%

Started at:           Mon Oct 25 18:57:00 2010
Finished at:          Mon Oct 25 18:59:26 2010
Ran for:              145.735860 seconds

Queries per second:   11744.830682 qps

% /opt/bin/queryperf -s 127.0.0.1 -p 5053 -d /tmp/redis-test.conf

DNS Query Performance Testing Tool
Version: $Id: queryperf.c,v 1.12 2007/09/05 07:36:04 marka Exp $

[Status] Processing input data
[Status] Sending queries (beginning with 127.0.0.1)
[Status] Testing complete

Statistics:

Parse input file:     once
Ended due to:         reaching end of file

Queries sent:         2857092 queries
Queries completed:    2857092 queries
Queries lost:         0 queries
Queries delayed(?):   0 queries

RTT max:              0.101593 sec
RTT min:              0.000266 sec
RTT average:          0.001774 sec
RTT std deviation:    0.000632 sec
RTT out of range:     0 queries

Percentage completed: 100.00%
Percentage lost:        0.00%

Started at:           Mon Oct 25 23:36:57 2010
Finished at:          Mon Oct 25 23:41:13 2010
Ran for:              256.322268 seconds

Queries per second:   11146.483769 qps

% /opt/bin/queryperf -s 127.0.0.1 -p 5053 -d /tmp/redis-test.conf

DNS Query Performance Testing Tool
Version: $Id: queryperf.c,v 1.12 2007/09/05 07:36:04 marka Exp $

[Status] Processing input data
[Status] Sending queries (beginning with 127.0.0.1)
[Status] Testing complete

Statistics:

Parse input file:     once
Ended due to:         reaching end of file

Queries sent:         4485137 queries
Queries completed:    4485137 queries
Queries lost:         0 queries
Queries delayed(?):   0 queries

RTT max:              0.102150 sec
RTT min:              0.000549 sec
RTT average:          0.001719 sec
RTT std deviation:    0.000334 sec
RTT out of range:     0 queries

Percentage completed: 100.00%
Percentage lost:        0.00%

Started at:           Tue Oct 26 08:41:45 2010
Finished at:          Tue Oct 26 08:48:15 2010
Ran for:              389.838687 seconds

Queries per second:   11505.110061 qps

酷睿双核2.5GCPU,2G内存,670万记录,占用1.5G左右内存,开始使用交换分区。

/opt/bin/queryperf -s 127.0.0.1 -d /tmp/redis-test.conf

DNS Query Performance Testing Tool
Version: $Id: queryperf.c,v 1.12 2007/09/05 07:36:04 marka Exp $

[Status] Processing input data
[Status] Sending queries (beginning with 61.158.132.223)
[Timeout] Query timed out: msg id 1
[Status] Testing complete

Statistics:

Parse input file:     once
Ended due to:         reaching end of file

Queries sent:         6728279 queries
Queries completed:    6728278 queries
Queries lost:         1 queries
Queries delayed(?):   0 queries

RTT max:              4.468220 sec
RTT min:              0.000157 sec
RTT average:          0.001339 sec
RTT std deviation:    0.002454 sec
RTT out of range:     0 queries

Percentage completed: 100.00%
Percentage lost:        0.00%

Started at:           Fri Oct 29 11:22:11 2010
Finished at:          Fri Oct 29 11:29:48 2010
Ran for:              456.384615 seconds

Queries per second:   14742.560943 qps

分类: DNS 标签:

DNS查询统计图

2010年10月23日 16hot 没有评论

采用rrdtool生成的统计图,可以点击查看完整尺寸图片。

1、总统计图

2、记录类型统计图

分类: DNS, PHP 标签:

DNS报文格式

2010年10月22日 16hot 没有评论
Query Identifier(16) QR OPCodes Flags Reserved RCodes
QDCount(16) ANCount(16)
NSCount(16) ARCount(16)
Question Section(32)
Answer Section(32)
Authority Section(32)
Additional Records Section(32)

以下为各栏位的简要说明:
QID
DNS 查询封包编号,作为确认依据。
QR
查询封包为 0 ﹔回应为 1 。长度为 1 byte 。
OPCodes
封包类别(QUERY, IQUERY, STATUS, Reserved)。长度为 4 bytes。
Flags
共 4 bytes ,各表示:AA(Authoritative Answer)、TC(Truncation)、RD(Recursion Desired)、RA(Recursion Avalable)。
Reserved
保留未用。
RCodes
回应讯息,长 4 bytes ,除 0 及 6-15 保留未用外,1-5 分别为:Format Error、Server Failure、Name Error、Not Implemented、Refused。
Question Section、Answer Section、Authority Section、Additional Records Section
每一 Section 分为 NAME、TYPE、CLASS 三个子栏位,分别作为查询、应答、授权、额外记录等封包之资讯,及各自长度。
好了﹐关于 DNS 协定的讨论﹐暂时介绍到这里﹐相信上面说的这些也够您消化的了。
DNS 协定之 RFC 文件
RFC-822﹑RFC-883﹑RFC-920﹑RFC-973﹑RFC-974﹑RFC-1032﹑RFC-1033﹑RFC-1034﹑RFC-1035﹑RFC-1101﹑RFC-1296

分类: C/C++, DNS 标签:

对DNS报文的理解

2010年3月30日 16hot 没有评论

在这段时间的学习中,我发现通过抓包来学习的方法不错,这样有助于更深刻的理解。下面是我对dns报文的一些理解。
学习了《tcp/ip 卷一》 dns域名系统这一章,下面通过spynet capturenet抓一些dns通信报文进行一下分析,以加深理解。
本人是在windows 上使用nslookup工具来产生的dns通信,见上图。
阅读全文…

分类: DNS, 转载 标签:

DNS漏洞解析

2008年9月1日 16hot 没有评论

http://blog.delphij.net/archives/2008/07/dns-1.html

分类: DNS 标签:

netperf 与网络性能测量

2008年8月1日 16hot 没有评论

2004 年 7 月 01 日

本文首先介绍网络性能测量的一些基本概念和方法,然后结合 netperf 工具的使用,具体的讨论如何测试不同情况下的网络性能。

在构建或管理一个网络系统时,我们更多的是关心网络的可用性,即网络是否连通,而对于其整体的性能往往考虑不多,或者即使考虑到性能的问题,但是却发现没有合适的手段去测试网络的性能。

当开发出一个网络应用程序后,我们会发现,在实际的网络环境使用中,网络应用程序的使用效果不是很理想,问题可能出现在程序的开发上面,也有可能由于实际的网络环境中存在着瓶颈。面对这种问题,程序员一般会一筹莫展,原因就在于不掌握一些网络性能测量的工具。

在本文中,首先介绍网络性能测量的一些基本概念和方法,然后结合 netperf 工具的使用,具体的讨论如何测试不同情况下的网络性能。

网络性能测试概述

网络性能测量的五项指标

测量网络性能的五项指标是:

  • 可用性(availability)
  • 响应时间(response time)
  • 网络利用率(network utilization)
  • 网络吞吐量(network throughput)
  • 网络带宽容量(network bandwidth capacity)

1. 可用性

测试网络性能的第一步是确定网络是否正常工作,最简单的方法是使用 ping 命令。通过向远端的机器发送 icmp echo request,并等待接收 icmp echo reply 来判断远端的机器是否连通,网络是否正常工作。

Ping 命令有非常丰富的命令选项,比如 -c 可以指定发送 echo request 的个数,-s 可以指定每次发送的 ping 包大小。

网 络设备内部一般有多个缓冲池,不同的缓冲池使用不同的缓冲区大小,分别用来处理不同大小的分组(packet)。例如交换机中通常具有三种类型的包缓冲: 一类针对小的分组,一类针对中等大小的分组,还有一类针对大的分组。为了测试这样的网络设备,测试工具必须要具有发送不同大小分组的能力。Ping 命令的 -s 就可以使用在这种场合。

2. 响应时间

Ping 命令的 echo request/reply 一次往返所花费时间就是响应时间。有很多因素会影响到响应时间,如网段的负荷,网络主机的负荷,广播风暴,工作不正常的网络设备等等。

在网络工作正常时,记录下正常的响应时间。当用户抱怨网络的反应时间慢时,就可以将现在的响应时间与正常的响应时间对比,如果两者差值的波动很大,就能说明网络设备存在故障。

3. 网络利用率

网络利用率是指网络被使用的时间占总时间(即被使用的时间+空闲的时间)的比例。比如,Ethernet 虽然是共享的,但同时却只能有一个报文在传输。因此在任一时刻,Ethernet 或者是 100% 的利用率,或者是 0% 的利用率。

计算一个网段的网络利用率相对比较容易,但是确定一个网络的利用率就比较复杂。因此,网络测试工具一般使用网络吞吐量和网络带宽容量来确定网络中两个节点之间的性能。

4. 网络吞吐量

网络吞吐量是指在某个时刻,在网络中的两个节点之间,提供给网络应用的剩余带宽。

网 络吞吐量可以帮组寻找网络路径中的瓶颈。比如,即使 client 和 server 都被分别连接到各自的 100M Ethernet 上,但是如果这两个 100M 的Ethernet 被 10M 的 Ethernet 连接起来,那么 10M 的 Ethernet 就是网络的瓶颈。

网络吞吐量非常依赖于当前的网络负载情况。因此,为了得到正确的网络吞吐量,最好在不同时间(一天中的不同时刻,或者一周中不同的天)分别进行测试,只有这样才能得到对网络吞吐量的全面认识。

有些网络应用程序在开发过程的测试中能够正常运行,但是到实际的网络环境中却无法正常工作(由于没有足够的网络吞吐量)。这是因为测试只是在空闲的网络环境中,没有考虑到实际的网络环境中还存在着其它的各种网络流量。所以,网络吞吐量定义为剩余带宽是有实际意义的。

5. 网络带宽容量

与网络吞吐量不同,网络带宽容量指的是在网络的两个节点之间的最大可用带宽。这是由组成网络的设备的能力所决定的。

测试网络带宽容量有两个困难之处:在网络存在其它网络流量的时候,如何得知网络的最大可用带宽;在测试过程中,如何对现有的网络流量不造成影响。网络测试工具一般采用 packet pairs 和 packet trains 技术来克服这样的困难。

收集网络性能数据的方式

当确定了网络性能的测试指标以后,就需要使用网络测试工具收集相应的性能数据,分别有三种从网络获取数据的方式:

1. 通过snmp协议直接到网络设备中获取,如net-snmp工具

2. 侦听相关的网络性能数据,典型的工具是tcpdump

3. 自行产生相应的测试数据,如本文中使用的netperf工具


回页首

Netperf

Netperf 是一种网络性能的测量工具,主要针对基于TCP或UDP的传输。Netperf根据应用的不同,可以进行不同模式的网络性能测试,即批量数据传输 (bulk data transfer)模式和请求/应答(request/reponse)模式。Netperf测试结果所反映的是一个系统能够以多快的速度向另外一个系统 发送数据,以及另外一个系统能够以多块的速度接收数据。

Netperf工具以client/server方式 工作。server端是netserver,用来侦听来自client端的连接,client端是netperf,用来向server发起网络测试。在 client与server之间,首先建立一个控制连接,传递有关测试配置的信息,以及测试的结果;在控制连接建立并传递了测试配置信息以后, client与server之间会再建立一个测试连接,用来来回传递着特殊的流量模式,以测试网络的性能。

TCP网络性能

由于TCP协议能够提供端到端的可靠传输,因此被大量的网络应用程序使用。但是,可靠性的建立是要付出代价的。TCP协议保证可靠性的措施,如建立并维护连接、控制数据有序的传递等都会消耗一定的网络带宽。

Netperf可以模拟三种不同的TCP流量模式:

1) 单个TCP连接,批量(bulk)传输大量数据

2) 单个TCP连接,client请求/server应答的交易(transaction)方式

3) 多个TCP连接,每个连接中一对请求/应答的交易方式

UDP网络性能

UDP没有建立连接的负担,但是UDP不能保证传输的可靠性,所以使用UDP的应用程序需要自行跟踪每个发出的分组,并重发丢失的分组。

Netperf可以模拟两种UDP的流量模式:

1) 从client到server的单向批量传输

2) 请求/应答的交易方式

由于UDP传输的不可靠性,在使用netperf时要确保发送的缓冲区大小不大于接收缓冲区大小,否则数据会丢失,netperf将给出错误的结果。因此,对于接收到分组的统计不一定准确,需要结合发送分组的统计综合得出结论。

Netperf的命令行参数

在unix系统中,可以直接运行可执行程序来启动netserver,也可以让inetd或xinetd来自动启动netserver。

当netserver 在server端启动以后,就可以在client端运行netperf来测试网络的性能。netperf通过命令行参数来控制测试的类型和具体的测试选 项。根据作用范围的不同,netperf的命令行参数可以分为两大类:全局命令行参数、测试相关的局部参数,两者之间使用–分隔:

netperf [global options]-- [test-specific options]

这里我们只解释那些常用的命令行参数,其它的参数读者可以查询netperf的man手册。

-H host :指定远端运行netserver的server IP地址。

-l testlen:指定测试的时间长度(秒)

-t testname:指定进行的测试类型,包括TCP_STREAM,UDP_STREAM,TCP_RR,TCP_CRR,UDP_RR,在下文中分别对它们说明。

在后面的测试中,netserver运行在192.168.0.28,server与client通过局域网连接(100M Hub)。

Netperf测试网络性能

测试批量(bulk)网络流量的性能

批量数据传输典型的例子有ftp和其它类似的网络应用(即一次传输整个文件)。根据使用传输协议的不同,批量数据传输又分为TCP批量传输和UDP批量传输。

1. TCP_STREAM

Netperf缺省情况下进行TCP批量传输,即-t TCP_STREAM。测试过程中,netperf向netserver发送批量的TCP数据分组,以确定数据传输过程中的吞吐量:

 ./netperf -H 192.168.0.28 -l 60TCP STREAM TEST to 192.168.0.28Recv   Send    SendSocket Socket  Message  ElapsedSize   Size    Size     Time     Throughputbytes  bytes   bytes    secs.    10^6bits/sec

 87380  16384  16384    60.00      88.00

从netperf的结果输出中,我们可以知道以下的一些信息:

1) 远端系统(即server)使用大小为87380字节的socket接收缓冲

2) 本地系统(即client)使用大小为16384字节的socket发送缓冲

3) 向远端系统发送的测试分组大小为16384字节

4) 测试经历的时间为60秒

5) 吞吐量的测试结果为88Mbits/秒

在缺省情况下,netperf向发送的测试分组大小设置为本地系统所使用的socket发送缓冲大小。

TCP_STREAM方式下与测试相关的局部参数如下表所示:

参数 说明
-s size 设置本地系统的socket发送与接收缓冲大小
-S size 设置远端系统的socket发送与接收缓冲大小
-m size 设置本地系统发送测试分组的大小
-M size 设置远端系统接收测试分组的大小
-D 对本地与远端系统的socket设置TCP_NODELAY选项

通过修改以上的参数,并观察结果的变化,我们可以确定是什么因素影响了连接的吞吐量。例如,如果怀疑路由器由于缺乏足够的缓冲区空间,使得转发大的分组时存在问题,就可以增加测试分组(-m)的大小,以观察吞吐量的变化:

 ./netperf -H 192.168.0.28 -l 60 -- -m 2048TCP STREAM TEST to 192.168.0.28Recv   Send    SendSocket Socket  Message  ElapsedSize   Size    Size     Time     Throughputbytes  bytes   bytes    secs.    10^6bits/sec

 87380  16384   2048    60.00      87.62

在这里,测试分组的大小减少到2048字节,而吞吐量却没有很大的变化(与前面例子中测试分组大小为16K字节相比)。相反,如果吞吐量有了较大的提升,则说明在网络中间的路由器确实存在缓冲区的问题。

2. UDP_STREAM

UDP_STREAM用来测试进行UDP批量传输时的网络性能。需要特别注意的是,此时测试分组的大小不得大于socket的发送与接收缓冲大小,否则netperf会报出错提示:

./netperf -t UDP_STREAM -H 192.168.0.28 -l 60UDP UNIDIRECTIONAL SEND TEST to 192.168.0.28udp_send: data send error: Message too long

为了避免这样的情况,可以通过命令行参数限定测试分组的大小,或者增加socket的发送/接收缓冲大小。UDP_STREAM方式使用与TCP_STREAM方式相同的局部命令行参数,因此,这里可以使用-m来修改测试中使用分组的大小:

 ./netperf -t UDP_STREAM -H 192.168.0.28 -- -m 1024UDP UNIDIRECTIONAL SEND TEST to 192.168.0.28Socket  Message  Elapsed      MessagesSize    Size     Time         Okay Errors   Throughputbytes   bytes    secs            #      #   10^6bits/sec

 65535    1024   9.99       114127      0      93.55 65535           9.99       114122             93.54

UDP_STREAM方式的结果中有两行测试数据,第一行显示的是本地系统的发送统计,这里的吞吐量表示netperf向本地socket发送分组的能力。但是,我们知道,UDP是不可靠的传输协议,发送出去的分组数量不一定等于接收到的分组数量。

第 二行显示的就是远端系统接收的情况,由于client与server直接连接在一起,而且网络中没有其它的流量,所以本地系统发送过去的分组几乎都被远端 系统正确的接收了,远端系统的吞吐量也几乎等于本地系统的发送吞吐量。但是,在实际环境中,一般远端系统的socket缓冲大小不同于本地系统的 socket缓冲区大小,而且由于UDP协议的不可靠性,远端系统的接收吞吐量要远远小于发送出去的吞吐量。

测试请求/应答(request/response)网络流量的性能

另一类常见的网络流量类型是应用在client/server结构中的request/response模式。在每次交易(transaction)中,client向server发出小的查询分组,server接收到请求,经处理后返回大的结果数据。如下图所示:

1. TCP_RR

TCP_RR方式的测试对象是多次TCP request和response的交易过程,但是它们发生在同一个TCP连接中,这种模式常常出现在数据库应用中。数据库的client程序与server程序建立一个TCP连接以后,就在这个连接中传送数据库的多次交易过程。

./netperf -t TCP_RR -H 192.168.0.28TCP REQUEST/RESPONSE TEST to 192.168.0.28Local /RemoteSocket Size   Request  Resp.   Elapsed  Trans.Send   Recv   Size     Size    Time     Ratebytes  Bytes  bytes    bytes   secs.    per sec

16384  87380  1        1       10.00    9502.7316384  87380

Netperf输出的结果也是由两行组 成。第一行显示本地系统的情况,第二行显示的是远端系统的信息。平均的交易率(transaction rate)为9502.73次/秒。注意到这里每次交易中的request和response分组的大小都为1个字节,不具有很大的实际意义。用户可以通 过测试相关的参数来改变request和response分组的大小,TCP_RR方式下的参数如下表所示:

参数 说明
-r req,resp 设置request和reponse分组的大小
-s size 设置本地系统的socket发送与接收缓冲大小
-S size 设置远端系统的socket发送与接收缓冲大小
-D 对本地与远端系统的socket设置TCP_NODELAY选项

通过使用-r参数,我们可以进行更有实际意义的测试:

./netperf -t TCP_RR -H 192.168.0.28 -- -r 32,1024TCP REQUEST/RESPONSE TEST to 192.168.0.28Local /RemoteSocket Size   Request  Resp.   Elapsed  Trans.Send   Recv   Size     Size    Time     Ratebytes  Bytes  bytes    bytes   secs.    per sec

16384  87380  32       1024    10.00    4945.9716384  87380

从结果中可以看出,由于request/reponse分组的大小增加了,导致了交易率明显的下降。 注:相对于实际的系统,这里交易率的计算没有充分考虑到交易过程中的应用程序处理时延,因此结果往往会高于实际情况。

2. TCP_CRR

与TCP_RR不同,TCP_CRR为每次交易建立一个新的TCP连接。最典型的应用就是HTTP,每次HTTP交易是在一条单独的TCP连接中进行的。因此,由于需要不停地建立新的TCP连接,并且在交易结束后拆除TCP连接,交易率一定会受到很大的影响。

./netperf -t TCP_CRR -H 192.168.0.28 TCP Connect/Request/Response TEST to 192.168.0.28Local /RemoteSocket Size   Request  Resp.   Elapsed  Trans.Send   Recv   Size     Size    Time     Ratebytes  Bytes  bytes    bytes   secs.    per sec

131070 131070 1        1       9.99     2662.2016384  87380

即使是使用一个字节的request/response分组,交易率也明显的降低了,只有2662.20次/秒。TCP_CRR使用与TCP_RR相同的局部参数。

3. UDP_RR

UDP_RR方式使用UDP分组进行request/response的交易过程。由于没有TCP连接所带来的负担,所以我们推测交易率一定会有相应的提升。

./netperf -t UDP_RR -H 192.168.0.28 UDP REQUEST/RESPONSE TEST to 192.168.0.28Local /RemoteSocket Size   Request  Resp.   Elapsed  Trans.Send   Recv   Size     Size    Time     Ratebytes  Bytes  bytes    bytes   secs.    per sec

65535  65535  1        1       9.99     10141.1665535  65535

结果证实了我们的推测,交易率为10141.16次/秒,高过TCP_RR的数值。不过,如果出现了相反的结果,即交易率反而降低了,也不需要担心,因为这说明了在网络中,路由器或其它的网络设备对UDP采用了与TCP不同的缓冲区空间和处理技术。


回页首

结束语

除 了netperf以外,还有很多其它的网络性能测试工具,如dbs, iperf, pathrate, nettest, netlogger, tcptrace, ntop等。这些工具有其各自的特色和不同的侧重点,我们可以根据具体的应用环境,有选择的使用它们,这样就可以使这些工具发挥出最大的功效。虽然都是开 放源代码的软件,但是这些工具的功能与商业的网络测试工具同样强大,而且也得到了广泛的应用,熟悉这些工具对我们的实际工作一定会有很大的帮助。

参考资料

  1. Network Performance Open Source Toolkit, Richard Blum, Wiley Publishing, Inc.
  2. netperf website, http://www.netperf.org

关于作者

汤凯,通过 tangk73@hotmail.com可以跟他联系。
分类: 网络技术 标签: