| 1 |
#include "protoApp.h" |
| 2 |
#include "protoVif.h" |
| 3 |
#include "protoCap.h" |
| 4 |
#include "protoPktIP.h" |
| 5 |
#include "protoPktETH.h" |
| 6 |
#include "protoPktARP.h" |
| 7 |
#include "protoTimer.h" |
| 8 |
#include "protoPipe.h" |
| 9 |
#include "protoNet.h" |
| 10 |
|
| 11 |
#include <stdlib.h> |
| 12 |
#include <stdio.h> |
| 13 |
#include <string.h> |
| 14 |
#include <ctype.h> //for "isspace()" |
| 15 |
#ifdef WIN32 |
| 16 |
#include <Iptypes.h> // For MAX_ADAPTER_NAME_LENGTH |
| 17 |
#endif //WIN32 |
| 18 |
// rxlimit smoothing factors -- weights for new data |
| 19 |
#define RXLIMIT_SIZE_ALPHA 0.1 // for average packet length |
| 20 |
#define RXLIMIT_RATE_ALPHA 0.1 // for average rate measurement |
| 21 |
|
| 22 |
// rxlimit alpha modifiers |
| 23 |
// if sample datarate is > factor*rxlimit or < rxlimit/factor, |
| 24 |
// alpha will be increased to more quickly adjust average rate |
| 25 |
#define RXLIMIT_ALPHA_RATEFACTOR 2.0 |
| 26 |
|
| 27 |
// minimum time (sec) to update average datarate |
| 28 |
// if this is set too small, packets received very close together |
| 29 |
// may have an abnormally high instantaneous rate, throwing off the |
| 30 |
// average rate measurement despite the smoothing factor. This |
| 31 |
// serves to further smooth out the instantaneous rate. Another way |
| 32 |
// to look at this is that the drop probability will be updated at |
| 33 |
// most 1/RXLIMIT_MIN_UPDATE_TIME times per second. |
| 34 |
# define RXLIMIT_MIN_UPDATE_TIME 0.01 |
| 35 |
|
| 36 |
/** |
| 37 |
* @class VifExampleApp |
| 38 |
* |
| 39 |
* @brief Example using protoVif |
| 40 |
* |
| 41 |
*/ |
| 42 |
class VifExampleApp : public ProtoApp |
| 43 |
{ |
| 44 |
public: |
| 45 |
VifExampleApp(); |
| 46 |
~VifExampleApp(); |
| 47 |
|
| 48 |
bool OnStartup(int argc, const char*const* argv); |
| 49 |
bool ProcessCommands(int argc, const char*const* argv); |
| 50 |
void OnShutdown(); |
| 51 |
|
| 52 |
enum Mode |
| 53 |
{ |
| 54 |
BRIDGE, |
| 55 |
CLONE, |
| 56 |
REPLACE |
| 57 |
}; |
| 58 |
|
| 59 |
private: |
| 60 |
enum CmdType {CMD_INVALID, CMD_ARG, CMD_NOARG}; |
| 61 |
static const char* const CMD_LIST[]; |
| 62 |
static CmdType GetCmdType(const char* string); |
| 63 |
bool OnCommand(const char* cmd, const char* val); |
| 64 |
void Usage(); |
| 65 |
|
| 66 |
// Generate random number in range 0..max |
| 67 |
double UniformRand(double max = 1.0) |
| 68 |
{return (max * ((double)rand() / (double)RAND_MAX));} |
| 69 |
|
| 70 |
bool OnTxRateTimeout(ProtoTimer& theTimer); |
| 71 |
|
| 72 |
void OnControlMsg(ProtoSocket& thePipe, |
| 73 |
ProtoSocket::Event theEvent); |
| 74 |
|
| 75 |
void PeekPkt(ProtoPktETH& ethPkt, bool inbound); |
| 76 |
|
| 77 |
void OnOutboundPkt(ProtoChannel& theChannel, |
| 78 |
ProtoChannel::Notification notifyType); |
| 79 |
void OnInboundPkt(ProtoChannel& theChannel, |
| 80 |
ProtoChannel::Notification notifyType); |
| 81 |
|
| 82 |
ProtoVif* vif; |
| 83 |
ProtoCap* cap; |
| 84 |
|
| 85 |
char vif_name[ProtoVif::VIF_NAME_MAX]; |
| 86 |
ProtoAddress vif_addr; |
| 87 |
ProtoAddress vif_hwaddr; |
| 88 |
unsigned int vif_mask_len; |
| 89 |
char if_name[ProtoVif::VIF_NAME_MAX]; |
| 90 |
#ifdef WIN32 |
| 91 |
char if_friendly_name[MAX_ADAPTER_NAME_LENGTH]; |
| 92 |
bool dhcp_enabled; |
| 93 |
#endif //WIN32 |
| 94 |
Mode vif_mode; |
| 95 |
double txrate_limit; // in kbps |
| 96 |
double rxrate_limit; // in kbps |
| 97 |
double rxrate_limit_uppernominal; // kbps; above this level alpha will be increased |
| 98 |
double rxrate_limit_lowernominal; // kbps; below this level alpha will be increased |
| 99 |
ProtoTimer txrate_timer; |
| 100 |
ProtoTime rxlimit_prev_pkt_time; |
| 101 |
bool rxlimit_prev_pkt_time_initialized; |
| 102 |
double rxlimit_avg_rate; |
| 103 |
double rxlimit_drop_prob; |
| 104 |
double rxlimit_avg_size; |
| 105 |
unsigned int rxlimit_rxbytes; |
| 106 |
double tx_loss; |
| 107 |
double rx_loss; |
| 108 |
|
| 109 |
ProtoPipe control_pipe; // pipe I listen to |
| 110 |
|
| 111 |
/** |
| 112 |
* We cc the smf "mne blocking" support here as |
| 113 |
* a hack to use vifExample in MNE as a virtual "Mobile" |
| 114 |
* interface |
| 115 |
*/ |
| 116 |
enum {MNE_BLOCK_MAX = 100}; |
| 117 |
bool MneIsBlocking(const char* macAddr) const; |
| 118 |
char mne_block_list[6*MNE_BLOCK_MAX]; |
| 119 |
unsigned int mne_block_list_len; |
| 120 |
|
| 121 |
}; // end class VifExampleApp |
| 122 |
|
| 123 |
void VifExampleApp::Usage() |
| 124 |
{ |
| 125 |
fprintf(stderr, "Usage: vifExample vif <vif_name> [bridge|clone|replace] interface <if_name>\n" |
| 126 |
" [addr <ifAddr>/<maskLength>][mac <macAddr>]\n" |
| 127 |
" [rxrate <kbps>][txrate <kbps>][txloss <percent>]\n" |
| 128 |
" [rxloss <percent>][instance <name>][help] \n\n" |
| 129 |
"Note: Cloned interfaces use the real interface MAC address, while\n" |
| 130 |
" Bridged interfaces (default) use the virtual interface MAC addr\n" |
| 131 |
" (randomly assigned, unless \"mac\" command is given)\n"); |
| 132 |
} |
| 133 |
|
| 134 |
const char* const VifExampleApp::CMD_LIST[] = |
| 135 |
{ |
| 136 |
"-help", // print help info an exit |
| 137 |
"+vif", // virtual interface name |
| 138 |
"+addr", // <addr/maskLength> virtual interface address |
| 139 |
"+mac", // MAC address (for bridge mode only) |
| 140 |
"+interface", // name of real interface to which vif is "bridged" |
| 141 |
"-bridge", // denotes a "bridged" interface (uses vif MAC) |
| 142 |
"-clone", // denotes a "cloned" interface (uses real interface MAC) |
| 143 |
"-replace", // vif overtakes interface configuration (addressing, etc), i.e., replaces the interface (original iface restored on exit) |
| 144 |
"+txrate", // <kbps> set outbound data rate limit of vif (default unlimited) |
| 145 |
"+rxrate", // <kbps> set inbound data rate limit of vif (default unlimited) |
| 146 |
"+txloss", // <percent> packet drop probability applied to outbound pkts |
| 147 |
"+rxloss", // <percent> packet drop probability applied to inbound pkts |
| 148 |
"+instance", // <instanceName> of "control pipe" to listen for commands |
| 149 |
"+debug", // <debugLevel> |
| 150 |
NULL |
| 151 |
}; |
| 152 |
|
| 153 |
/** |
| 154 |
* This macro creates our ProtoApp derived application instance |
| 155 |
*/ |
| 156 |
PROTO_INSTANTIATE_APP(VifExampleApp) |
| 157 |
|
| 158 |
VifExampleApp::VifExampleApp() |
| 159 |
: vif(NULL), cap(NULL), vif_mask_len(0), |
| 160 |
#ifdef WIN32 |
| 161 |
dhcp_enabled(false), |
| 162 |
#endif //WIN32 |
| 163 |
vif_mode(BRIDGE), txrate_limit(-1.0), rxrate_limit(-1.0), |
| 164 |
rxrate_limit_uppernominal(-1.0), rxrate_limit_lowernominal(-1.0), |
| 165 |
rxlimit_prev_pkt_time_initialized(false), rxlimit_avg_rate(0.0), |
| 166 |
rxlimit_drop_prob(0.0), rxlimit_avg_size(0.0), rxlimit_rxbytes(0), |
| 167 |
tx_loss(0.0), rx_loss(0.0), |
| 168 |
control_pipe(ProtoPipe::MESSAGE), mne_block_list_len(0) |
| 169 |
{ |
| 170 |
vif_name[0] = '\0'; |
| 171 |
if_name[0] = '\0'; |
| 172 |
#ifdef WIN32 |
| 173 |
if_friendly_name[0] = '\0'; |
| 174 |
#endif //WIN32 |
| 175 |
txrate_timer.SetListener(this, &VifExampleApp::OnTxRateTimeout); |
| 176 |
|
| 177 |
control_pipe.SetNotifier(&GetSocketNotifier()); |
| 178 |
control_pipe.SetListener(this, &VifExampleApp::OnControlMsg); |
| 179 |
} |
| 180 |
|
| 181 |
VifExampleApp::~VifExampleApp() |
| 182 |
{ |
| 183 |
} |
| 184 |
|
| 185 |
VifExampleApp::CmdType VifExampleApp::GetCmdType(const char* cmd) |
| 186 |
{ |
| 187 |
if (!cmd) return CMD_INVALID; |
| 188 |
unsigned int len = strlen(cmd); |
| 189 |
bool matched = false; |
| 190 |
CmdType type = CMD_INVALID; |
| 191 |
const char* const* nextCmd = CMD_LIST; |
| 192 |
while (*nextCmd) |
| 193 |
{ |
| 194 |
if (!strncmp(cmd, *nextCmd+1, len)) |
| 195 |
{ |
| 196 |
if (matched) |
| 197 |
{ |
| 198 |
// ambiguous command (command should match only once) |
| 199 |
return CMD_INVALID; |
| 200 |
} |
| 201 |
else |
| 202 |
{ |
| 203 |
matched = true; |
| 204 |
if ('+' == *nextCmd[0]) |
| 205 |
type = CMD_ARG; |
| 206 |
else |
| 207 |
type = CMD_NOARG; |
| 208 |
} |
| 209 |
} |
| 210 |
nextCmd++; |
| 211 |
} |
| 212 |
return type; |
| 213 |
} // end VifExampleApp::GetCmdType() |
| 214 |
|
| 215 |
|
| 216 |
bool VifExampleApp::OnStartup(int argc, const char*const* argv) |
| 217 |
{ |
| 218 |
// Seed rand() with time of day usec |
| 219 |
// (comment out for repeatable results) |
| 220 |
struct timeval currentTime; |
| 221 |
ProtoSystemTime(currentTime); |
| 222 |
srand((unsigned int)currentTime.tv_usec); |
| 223 |
|
| 224 |
if (!ProcessCommands(argc, argv)) |
| 225 |
{ |
| 226 |
PLOG(PL_ERROR, "VifExampleApp::OnStartup() error processing command line options\n"); |
| 227 |
Usage(); |
| 228 |
return false; |
| 229 |
} |
| 230 |
|
| 231 |
// Check for valid parameters. |
| 232 |
if (0 == strlen(vif_name)) |
| 233 |
{ |
| 234 |
PLOG(PL_ERROR, "VifExampleApp::OnStartup() error: missing required 'vif' command!\n"); |
| 235 |
Usage(); |
| 236 |
return false; |
| 237 |
} |
| 238 |
if (0 == strlen(if_name)) |
| 239 |
{ |
| 240 |
PLOG(PL_ERROR, "VifExampleApp::OnStartup() error: missing required 'interface' command!\n"); |
| 241 |
Usage(); |
| 242 |
return false; |
| 243 |
} |
| 244 |
|
| 245 |
// Create our vif instance and initialize ... |
| 246 |
if (!(vif = ProtoVif::Create())) |
| 247 |
{ |
| 248 |
PLOG(PL_ERROR, "VifExampleApp::OnStartup() new ProtoVif error: %s\n", GetErrorString()); |
| 249 |
return false; |
| 250 |
} |
| 251 |
|
| 252 |
// Create our cap instance and initialize ... |
| 253 |
if (!(cap = ProtoCap::Create())) |
| 254 |
{ |
| 255 |
PLOG(PL_ERROR, "VifExampleApp::OnStartup() new ProtoCap error: %s\n", GetErrorString()); |
| 256 |
return false; |
| 257 |
} |
| 258 |
|
| 259 |
vif->SetNotifier(static_cast<ProtoChannel::Notifier*>(&dispatcher)); |
| 260 |
vif->SetListener(this,&VifExampleApp::OnOutboundPkt); |
| 261 |
|
| 262 |
cap->SetNotifier(static_cast<ProtoChannel::Notifier*>(&dispatcher)); |
| 263 |
cap->SetListener(this,&VifExampleApp::OnInboundPkt); |
| 264 |
|
| 265 |
if (!vif->Open(vif_name, vif_addr, vif_mask_len)) |
| 266 |
{ |
| 267 |
PLOG(PL_ERROR,"VifExampleApp::OnStartup() ProtoVif::Open(\"%s\") error\n", vif_name); |
| 268 |
Usage(); |
| 269 |
return false; |
| 270 |
} |
| 271 |
|
| 272 |
if (!cap->Open(if_name)) |
| 273 |
{ |
| 274 |
PLOG(PL_ERROR,"VifExampleApp::OnStartup() ProtoCap::Open(\"%s\") error\n", if_name); |
| 275 |
Usage(); |
| 276 |
return false; |
| 277 |
} |
| 278 |
|
| 279 |
switch (vif_mode) |
| 280 |
{ |
| 281 |
case BRIDGE: |
| 282 |
// use vif MAC addr or one explicitly provided on command-line |
| 283 |
if (vif_hwaddr.IsValid()) |
| 284 |
{ |
| 285 |
if (!vif->SetHardwareAddress(vif_hwaddr)) |
| 286 |
{ |
| 287 |
PLOG(PL_ERROR, "VifExampleApp::OnStartup() ProtoVif::SetHardwareAddress() error\n"); |
| 288 |
Usage(); |
| 289 |
return false; |
| 290 |
} |
| 291 |
// Seems we need to reopen vif after setting hardware address? |
| 292 |
// Note that SetHardwareAddress is not currently working on windows 7 |
| 293 |
// so this open is redundant on windows 7. TBD: Remove? |
| 294 |
#ifdef WIN32 |
| 295 |
/* |
| 296 |
if (!vif->Open(vif_name, vif_addr, vif_mask_len)) |
| 297 |
{ |
| 298 |
PLOG(PL_ERROR, "VifExampleApp::OnStartup() ProtoVif::Open(\"%s\") error\n", vif_name); |
| 299 |
Usage(); |
| 300 |
return false; |
| 301 |
} |
| 302 |
*/ |
| 303 |
#endif |
| 304 |
// Snag the virtual interface hardware address |
| 305 |
ProtoAddress tmp_hw_addr; |
| 306 |
if (!ProtoNet::GetInterfaceAddress(vif_name, ProtoAddress::ETH, tmp_hw_addr)) |
| 307 |
PLOG(PL_ERROR, "Win32Vif::Open(%s) error: unable to get ETH address!\n", vif_name); |
| 308 |
|
| 309 |
PLOG(PL_INFO,"Snagged>%s from vif>%s\n", tmp_hw_addr.GetHostString(), vif_name); |
| 310 |
|
| 311 |
|
| 312 |
} |
| 313 |
break; |
| 314 |
case CLONE: |
| 315 |
// Will transmit frames using "interface" MAC addr |
| 316 |
break; |
| 317 |
case REPLACE: |
| 318 |
{ |
| 319 |
// 1) Set vif MAC addr to same as "interface" |
| 320 |
if (!ProtoNet::GetInterfaceAddress(if_name, ProtoAddress::ETH, vif_hwaddr)) |
| 321 |
{ |
| 322 |
PLOG(PL_ERROR, "VifExampleApp::OnStartup() error getting interface MAC address\n"); |
| 323 |
Usage(); |
| 324 |
return false; |
| 325 |
} |
| 326 |
if (!vif->SetHardwareAddress(vif_hwaddr)) |
| 327 |
{ |
| 328 |
PLOG(PL_ERROR, "VifExampleApp::OnStartup() ProtoVif::SetHardwareAddress() error\n"); |
| 329 |
Usage(); |
| 330 |
return false; |
| 331 |
} |
| 332 |
// TBD: Seems like we need to reopen after changing hardware address? |
| 333 |
// Note that SetHardwareAddress is not currently working on windows 7 |
| 334 |
// so this open is redundant on windows 7. |
| 335 |
#ifdef WIN32 |
| 336 |
/* |
| 337 |
if (!vif->Open(vif_name, vif_addr, vif_mask_len)) |
| 338 |
{ |
| 339 |
PLOG(PL_ERROR, "VifExampleApp::OnStartup() ProtoVif::Open(\"%s\") error\n", vif_name); |
| 340 |
Usage(); |
| 341 |
return false; |
| 342 |
} |
| 343 |
*/ |
| 344 |
#endif |
| 345 |
ProtoAddressList addrList; |
| 346 |
if (!ProtoNet::GetInterfaceAddressList(if_name, ProtoAddress::IPv4, addrList)) |
| 347 |
PLOG(PL_WARN, "VifExampleApp::OnStartup() warning: no interface IPv4 addresses?\n"); |
| 348 |
if (!ProtoNet::GetInterfaceAddressList(if_name, ProtoAddress::IPv6, addrList)) |
| 349 |
PLOG(PL_WARN, "VifExampleApp::OnStartup() warning: no interface IPv6 addresses?\n"); |
| 350 |
#ifdef WIN32 |
| 351 |
// On WIN32 after we move the interface ip address to the vif |
| 352 |
// interface subsequent calls using the original ifAddr ip address |
| 353 |
// will get the vif adapter - use the friendly name to prevent this. |
| 354 |
ProtoAddress ifAddr; |
| 355 |
if (ifAddr.ConvertFromString(if_name)) |
| 356 |
{ |
| 357 |
if (!ProtoNet::GetInterfaceFriendlyName(ifAddr, if_friendly_name, MAX_ADAPTER_NAME_LENGTH)) |
| 358 |
{ |
| 359 |
PLOG(PL_ERROR, "ProtoNet::GetInterfaceAddressList(%s) error: no matching interface name found\n", if_name); |
| 360 |
return false; |
| 361 |
} |
| 362 |
PLOG(PL_INFO, "ProtoNet::GetInterfaceAddressList() friendly if_name>%s\n", if_friendly_name); |
| 363 |
} |
| 364 |
|
| 365 |
// Also squirrel away the dhcp enabled boolean so we can restore correctly |
| 366 |
dhcp_enabled = ProtoNet::GetInterfaceAddressDhcp(if_name,ifAddr); |
| 367 |
#endif //WIN32 |
| 368 |
|
| 369 |
// TBD - check for empty addrList? |
| 370 |
ProtoAddress addr; |
| 371 |
ProtoAddressList::Iterator iterator(addrList); |
| 372 |
#ifndef WIN32 |
| 373 |
while (iterator.GetNextAddress(addr)) |
| 374 |
{ |
| 375 |
if (addr.IsLinkLocal()) continue; |
| 376 |
unsigned int maskLen = ProtoNet::GetInterfaceAddressMask(if_name, addr); |
| 377 |
// TODO: continue? |
| 378 |
if (maskLen == 0) |
| 379 |
continue; |
| 380 |
|
| 381 |
// Remove address from "interface" |
| 382 |
#ifdef WIN32 |
| 383 |
// On WIN32 after we move the interface ip address to the vif |
| 384 |
// address subsequent calls using the ifAddr will get the vif adapter - |
| 385 |
// use the friendly name to prevent this. |
| 386 |
if (!ProtoNet::RemoveInterfaceAddress(if_friendly_name, addr, maskLen)) |
| 387 |
#else |
| 388 |
if (!ProtoNet::RemoveInterfaceAddress(if_name,addr,maskLen)) |
| 389 |
#endif //if/else WIN32 |
| 390 |
{ |
| 391 |
PLOG(PL_ERROR, "VifExampleApp::OnStartup() error removing address %s from interface %s\n", addr.GetHostString(), if_name); |
| 392 |
Usage(); |
| 393 |
return false; |
| 394 |
} |
| 395 |
// Assign address to vif |
| 396 |
if (!ProtoNet::AddInterfaceAddress(vif_name, addr, maskLen)) |
| 397 |
{ |
| 398 |
PLOG(PL_ERROR, "VifExampleApp::OnStartup() error adding address %s to vif %s\n", addr.GetHostString(), vif_name); |
| 399 |
Usage(); |
| 400 |
return false; |
| 401 |
} |
| 402 |
#ifdef WIN32 |
| 403 |
// Set vif_addr to the new addr |
| 404 |
vif_addr = addr; |
| 405 |
#endif //WIN32 |
| 406 |
} |
| 407 |
#endif // !WIN32 |
| 408 |
break; |
| 409 |
} |
| 410 |
} |
| 411 |
|
| 412 |
PLOG(PL_INFO, "vifExample: running on virtual interface: %s\n", vif_name); |
| 413 |
|
| 414 |
return true; |
| 415 |
} // end VifExampleApp::OnStartup() |
| 416 |
|
| 417 |
void VifExampleApp::OnShutdown() |
| 418 |
{ |
| 419 |
if (NULL != vif) |
| 420 |
{ |
| 421 |
if (REPLACE == vif_mode) |
| 422 |
{ |
| 423 |
// Assign vif address(es) back to "interface" |
| 424 |
ProtoAddressList addrList; |
| 425 |
if (!ProtoNet::GetInterfaceAddressList(vif_name, ProtoAddress::IPv4, addrList)) |
| 426 |
PLOG(PL_ERROR, "VifExampleApp::OnShutdown() error getting vif IPv4 addresses\n"); |
| 427 |
if (!ProtoNet::GetInterfaceAddressList(vif_name, ProtoAddress::IPv6, addrList)) |
| 428 |
PLOG(PL_ERROR, "VifExampleApp::OnShutdown() error getting vif IPv6 addresses\n"); |
| 429 |
|
| 430 |
ProtoAddress addr; |
| 431 |
ProtoAddressList::Iterator iterator(addrList); |
| 432 |
#ifndef WIN32 |
| 433 |
while (iterator.GetNextAddress(addr)) |
| 434 |
{ |
| 435 |
if (addr.IsLinkLocal()) continue; |
| 436 |
unsigned int maskLen = 0; |
| 437 |
#ifdef WIN32 |
| 438 |
// TODO: add option to GetInterfaceAddressMask to accept vif_name for lookup |
| 439 |
maskLen = ProtoNet::GetInterfaceAddressMask(vif_addr.GetHostString(), addr); |
| 440 |
#else |
| 441 |
maskLen = ProtoNet::GetInterfaceAddressMask(vif_name, addr); |
| 442 |
#endif //WIN32 |
| 443 |
// TBD: continue? |
| 444 |
if (maskLen == 0) |
| 445 |
continue; |
| 446 |
// Remove address from vif |
| 447 |
if (!ProtoNet::RemoveInterfaceAddress(vif_name, addr, maskLen)) |
| 448 |
PLOG(PL_ERROR, "VifExampleApp::OnShutdown() error removing address %s from vif %s\n", addr.GetHostString(), vif_name); |
| 449 |
// Assign address to "interface" |
| 450 |
#ifdef WIN32 |
| 451 |
// TODO: Make common function prototypes |
| 452 |
if (!ProtoNet::AddInterfaceAddress(if_friendly_name, addr, maskLen, dhcp_enabled)) |
| 453 |
PLOG(PL_ERROR, "VifExampleApp::OnShutdown() error adding address %s to interface %s\n", addr.GetHostString(), if_friendly_name); |
| 454 |
#else |
| 455 |
if (!ProtoNet::AddInterfaceAddress(if_name, addr, maskLen)) |
| 456 |
PLOG(PL_ERROR, "VifExampleApp::OnShutdown() error adding address %s to interface %s\n", addr.GetHostString(), if_name); |
| 457 |
#endif //WIN32 |
| 458 |
} |
| 459 |
#endif // !WIN32 |
| 460 |
} |
| 461 |
vif->Close(); |
| 462 |
delete vif; |
| 463 |
vif = NULL; |
| 464 |
} |
| 465 |
|
| 466 |
if (NULL != cap) |
| 467 |
{ |
| 468 |
cap->Close(); |
| 469 |
delete cap; |
| 470 |
cap = NULL; |
| 471 |
} |
| 472 |
|
| 473 |
PLOG(PL_INFO, "vifExample: Done.\n"); |
| 474 |
|
| 475 |
} // end VifExampleApp::OnShutdown() |
| 476 |
|
| 477 |
bool VifExampleApp::ProcessCommands(int argc, const char*const* argv) |
| 478 |
{ |
| 479 |
// Dispatch command-line commands to our OnCommand() method |
| 480 |
int i = 1; |
| 481 |
while ( i < argc) |
| 482 |
{ |
| 483 |
// Is it a vifExample command? |
| 484 |
switch (GetCmdType(argv[i])) |
| 485 |
{ |
| 486 |
case CMD_INVALID: |
| 487 |
{ |
| 488 |
PLOG(PL_ERROR, "VifExampleApp::ProcessCommands() Invalid command:%s\n", |
| 489 |
argv[i]); |
| 490 |
Usage(); |
| 491 |
return false; |
| 492 |
} |
| 493 |
case CMD_NOARG: |
| 494 |
if (!OnCommand(argv[i], NULL)) |
| 495 |
{ |
| 496 |
PLOG(PL_ERROR, "VifExampleApp::ProcessCommands() ProcessCommand(%s) error\n", |
| 497 |
argv[i]); |
| 498 |
Usage(); |
| 499 |
return false; |
| 500 |
} |
| 501 |
i++; |
| 502 |
break; |
| 503 |
case CMD_ARG: |
| 504 |
if (!OnCommand(argv[i], argv[i+1])) |
| 505 |
{ |
| 506 |
PLOG(PL_ERROR, "VifExampleApp::ProcessCommands() ProcessCommand(%s, %s) error\n", |
| 507 |
argv[i], argv[i+1]); |
| 508 |
Usage(); |
| 509 |
return false; |
| 510 |
} |
| 511 |
i += 2; |
| 512 |
break; |
| 513 |
} |
| 514 |
} |
| 515 |
return true; |
| 516 |
} // end VifExampleApp::ProcessCommands() |
| 517 |
|
| 518 |
bool VifExampleApp::OnCommand(const char* cmd, const char* val) |
| 519 |
{ |
| 520 |
// (TBD) move command processing into Mgen class ??? |
| 521 |
CmdType type = GetCmdType(cmd); |
| 522 |
ASSERT(CMD_INVALID != type); |
| 523 |
size_t len = strlen(cmd); |
| 524 |
if ((CMD_ARG == type) && !val) |
| 525 |
{ |
| 526 |
PLOG(PL_ERROR, "VifExampleApp::ProcessCommand(%s) missing argument\n", cmd); |
| 527 |
Usage(); |
| 528 |
return false; |
| 529 |
} |
| 530 |
else if (!strncmp("help", cmd, len)) |
| 531 |
{ |
| 532 |
Usage(); |
| 533 |
exit(0); |
| 534 |
} |
| 535 |
else if (!strncmp("vif", cmd, len)) |
| 536 |
{ |
| 537 |
strncpy(vif_name, val, ProtoVif::VIF_NAME_MAX); |
| 538 |
|
| 539 |
} |
| 540 |
else if (!strncmp("address", cmd, len)) |
| 541 |
{ |
| 542 |
int addrLen = strcspn(val,"/"); |
| 543 |
char addrString[32] = "\0"; |
| 544 |
strncpy(addrString,val,addrLen); |
| 545 |
|
| 546 |
const char* maskLenPtr = strchr(val,'/'); |
| 547 |
if (NULL != maskLenPtr) |
| 548 |
{ |
| 549 |
maskLenPtr++; |
| 550 |
} |
| 551 |
else |
| 552 |
{ |
| 553 |
PLOG(PL_ERROR, "VifExampleApp::OnCommand() Error: missing <maskLength>\n"); |
| 554 |
return false; |
| 555 |
} |
| 556 |
if (!vif_addr.ResolveFromString(addrString)) |
| 557 |
{ |
| 558 |
PLOG(PL_ERROR, "VifExampleApp::OnCommand() Error: invalid <vifAddr>\n"); |
| 559 |
return false; |
| 560 |
} |
| 561 |
|
| 562 |
if (1 != sscanf(maskLenPtr, "%u", &vif_mask_len)) |
| 563 |
{ |
| 564 |
PLOG(PL_ERROR, "VifExampleApp::OnCommand() Error: invalid <maskLength>\n"); |
| 565 |
return false; |
| 566 |
} |
| 567 |
|
| 568 |
if ((vif_mask_len < 1) || (vif_mask_len > 32)) |
| 569 |
{ |
| 570 |
PLOG(PL_ERROR, "VifExampleApp::OnCommand() Error: invalid <maskLength>\n"); |
| 571 |
return false; |
| 572 |
} |
| 573 |
PLOG(PL_DEBUG, "VifExampleApp::OnCommand(addr) value: %s\n",vif_addr.GetHostString()); |
| 574 |
|
| 575 |
} |
| 576 |
else if (!strncmp("mac", cmd, len)) |
| 577 |
{ |
| 578 |
if (!vif_hwaddr.ResolveEthFromString(val)) |
| 579 |
{ |
| 580 |
PLOG(PL_ERROR, "VifExampleApp::OnCommand() Error: invalid <macAddr>\n"); |
| 581 |
return false; |
| 582 |
} |
| 583 |
PLOG(PL_DEBUG, "VifExampleApp::OnCommand(mac) value: %s\n",vif_hwaddr.GetHostString()); |
| 584 |
} |
| 585 |
else if (!strncmp("interface", cmd, len)) |
| 586 |
{ |
| 587 |
strncpy(if_name, val, ProtoVif::VIF_NAME_MAX); |
| 588 |
} |
| 589 |
else if (!strncmp("bridge", cmd, len)) |
| 590 |
{ |
| 591 |
vif_mode = BRIDGE; |
| 592 |
} |
| 593 |
else if (!strncmp("clone", cmd, len)) |
| 594 |
{ |
| 595 |
vif_mode = CLONE; |
| 596 |
} |
| 597 |
else if (!strncmp("replace", cmd, len)) |
| 598 |
{ |
| 599 |
vif_mode = REPLACE; |
| 600 |
} |
| 601 |
else if (!strncmp("txrate", cmd, len)) |
| 602 |
{ |
| 603 |
double rateLimit; |
| 604 |
if (1 != sscanf(val, "%lf", &rateLimit)) |
| 605 |
{ |
| 606 |
PLOG(PL_ERROR, "VifExampleApp::OnCommand(txrate) error: invalid value: %s\n", val); |
| 607 |
return false; |
| 608 |
} |
| 609 |
if (rateLimit < 0.0) |
| 610 |
{ |
| 611 |
PLOG(PL_ERROR, "VifExampleApp::OnCommand(txrate) error: invalid value: %s\n", val); |
| 612 |
return false; |
| 613 |
} |
| 614 |
txrate_limit = 1000.0 * rateLimit; |
| 615 |
} |
| 616 |
else if (!strncmp("rxrate", cmd, len)) |
| 617 |
{ |
| 618 |
double rateLimit; |
| 619 |
if (1 != sscanf(val, "%lf", &rateLimit)) |
| 620 |
{ |
| 621 |
PLOG(PL_ERROR, "VifExampleApp::OnCommand(rxrate) error: invalid value: %s\n", val); |
| 622 |
return false; |
| 623 |
} |
| 624 |
if (rateLimit < 0.0) |
| 625 |
{ |
| 626 |
PLOG(PL_ERROR, "VifExampleApp::OnCommand(txrate) error: invalid value: %s\n", val); |
| 627 |
return false; |
| 628 |
} |
| 629 |
rxrate_limit = 1000.0 * rateLimit; |
| 630 |
rxrate_limit_uppernominal = RXLIMIT_ALPHA_RATEFACTOR * rxrate_limit; |
| 631 |
rxrate_limit_lowernominal = rxrate_limit / RXLIMIT_ALPHA_RATEFACTOR; |
| 632 |
} |
| 633 |
else if (!strncmp("txloss", cmd, len)) |
| 634 |
{ |
| 635 |
double lossPercent; |
| 636 |
if (1 != sscanf(val, "%lf", &lossPercent)) |
| 637 |
{ |
| 638 |
PLOG(PL_ERROR, "VifExampleApp::OnCommand(txloss) error: invalid value: %s\n", val); |
| 639 |
return false; |
| 640 |
} |
| 641 |
tx_loss = lossPercent; |
| 642 |
} |
| 643 |
else if (!strncmp("rxloss", cmd, len)) |
| 644 |
{ |
| 645 |
double lossPercent; |
| 646 |
if (1 != sscanf(val, "%lf", &lossPercent)) |
| 647 |
{ |
| 648 |
PLOG(PL_ERROR, "VifExampleApp::OnCommand(rxloss) error: invalid value: %s\n", val); |
| 649 |
return false; |
| 650 |
} |
| 651 |
rx_loss = lossPercent; |
| 652 |
} |
| 653 |
else if (!strncmp("instance", cmd, len)) |
| 654 |
{ |
| 655 |
if (control_pipe.IsOpen()) control_pipe.Close(); |
| 656 |
if (!control_pipe.Listen(val)) |
| 657 |
{ |
| 658 |
PLOG(PL_ERROR, "VifExampleApp::OnCommand(instance) error opening control pipe\n"); |
| 659 |
return false; |
| 660 |
} |
| 661 |
} |
| 662 |
else if (!strncmp("debug", cmd, len)) |
| 663 |
{ |
| 664 |
SetDebugLevel(atoi(val)); |
| 665 |
} |
| 666 |
else |
| 667 |
{ |
| 668 |
PLOG(PL_ERROR, "vifExample error: invalid command\n"); |
| 669 |
Usage(); |
| 670 |
return false; |
| 671 |
} |
| 672 |
return true; |
| 673 |
} // end VifExampleApp::OnCommand() |
| 674 |
|
| 675 |
void VifExampleApp::OnControlMsg(ProtoSocket& thePipe, ProtoSocket::Event theEvent) |
| 676 |
{ |
| 677 |
if (ProtoSocket::RECV == theEvent) |
| 678 |
{ |
| 679 |
char buffer[8192]; |
| 680 |
unsigned int len = 8191; |
| 681 |
if (thePipe.Recv(buffer, len)) |
| 682 |
{ |
| 683 |
buffer[len] = '\0'; |
| 684 |
// Parse received message from controller and populate |
| 685 |
// our forwarding table |
| 686 |
if (0 != len) |
| 687 |
PLOG(PL_INFO,"VifExampleApp::OnControlMsg() recv'd %d byte message from controller: \"%s\"\n", |
| 688 |
len, buffer); |
| 689 |
char* cmd = buffer; |
| 690 |
char* arg = NULL; |
| 691 |
for (unsigned int i = 0; i < len; i++) |
| 692 |
{ |
| 693 |
if ('\0' == buffer[i]) |
| 694 |
{ |
| 695 |
break; |
| 696 |
} |
| 697 |
else if (isspace(buffer[i])) |
| 698 |
{ |
| 699 |
buffer[i] = '\0'; |
| 700 |
arg = buffer+i+1; |
| 701 |
break; |
| 702 |
} |
| 703 |
} |
| 704 |
unsigned int cmdLen = strlen(cmd); |
| 705 |
unsigned int argLen = len - (arg - cmd); |
| 706 |
// Check for a pipe only commands first |
| 707 |
if (!strncmp(cmd, "mneMacBlock", cmdLen) || !strncmp(cmd, "mneBlock", cmdLen)) |
| 708 |
{ |
| 709 |
// The "arg" points to the current set of MPR mneBlock MAC addresses |
| 710 |
// Overwrite our current mneBlock list |
| 711 |
if (argLen > (6*MNE_BLOCK_MAX)) |
| 712 |
{ |
| 713 |
PLOG(PL_ERROR, "VifExampleApp::OnControlMsg(mneBlock) error: mac list too long!\n"); |
| 714 |
// (TBD) record this error indication permanently |
| 715 |
argLen = (6*MNE_BLOCK_MAX); |
| 716 |
} |
| 717 |
memcpy(mne_block_list, arg, argLen); |
| 718 |
mne_block_list_len = argLen; |
| 719 |
} |
| 720 |
else |
| 721 |
{ |
| 722 |
// Maybe it's a regular command |
| 723 |
if (!OnCommand(cmd, arg)) |
| 724 |
PLOG(PL_ERROR, "VifExampleApp::OnControlMsg() invalid command: \"%s\"\n", cmd); |
| 725 |
} |
| 726 |
} |
| 727 |
} |
| 728 |
} // end VifExampleApp::OnControlMsg() |
| 729 |
|
| 730 |
bool VifExampleApp::MneIsBlocking(const char* macAddr) const |
| 731 |
{ |
| 732 |
const size_t MAC_ADDR_LEN = 6; |
| 733 |
const char *ptr = mne_block_list; |
| 734 |
const char* endPtr = mne_block_list + mne_block_list_len; |
| 735 |
while (ptr < endPtr) |
| 736 |
{ |
| 737 |
if (!memcmp(macAddr, ptr, MAC_ADDR_LEN)) |
| 738 |
return true; |
| 739 |
ptr += MAC_ADDR_LEN; |
| 740 |
} |
| 741 |
return false; |
| 742 |
} // end VifExampleApp::MneIsBlocking() |
| 743 |
|
| 744 |
void VifExampleApp::PeekPkt(ProtoPktETH& ethPkt, bool inbound) |
| 745 |
{ |
| 746 |
switch (ethPkt.GetType()) |
| 747 |
{ |
| 748 |
case ProtoPktETH::IP: |
| 749 |
case ProtoPktETH::IPv6: |
| 750 |
{ |
| 751 |
unsigned int payloadLength = ethPkt.GetPayloadLength(); |
| 752 |
ProtoPktIP ipPkt; |
| 753 |
if (!ipPkt.InitFromBuffer(payloadLength, (UINT32*)ethPkt.AccessPayload(), payloadLength)) |
| 754 |
{ |
| 755 |
PLOG(PL_ERROR, "vifExample::PeekPkt() error: bad %sbound IP packet\n", |
| 756 |
inbound ? "in" : "out"); |
| 757 |
break; |
| 758 |
} |
| 759 |
ProtoAddress dstAddr; |
| 760 |
ProtoAddress srcAddr; |
| 761 |
switch (ipPkt.GetVersion()) |
| 762 |
{ |
| 763 |
case 4: |
| 764 |
{ |
| 765 |
ProtoPktIPv4 ip4Pkt(ipPkt); |
| 766 |
ip4Pkt.GetDstAddr(dstAddr); |
| 767 |
ip4Pkt.GetSrcAddr(srcAddr); |
| 768 |
break; |
| 769 |
} |
| 770 |
case 6: |
| 771 |
{ |
| 772 |
ProtoPktIPv6 ip6Pkt(ipPkt); |
| 773 |
ip6Pkt.GetDstAddr(dstAddr); |
| 774 |
ip6Pkt.GetSrcAddr(srcAddr); |
| 775 |
break; |
| 776 |
} |
| 777 |
default: |
| 778 |
{ |
| 779 |
PLOG(PL_ERROR,"VifExampleApp::PeekPkt() Error: Invalid IP pkt version.\n"); |
| 780 |
break; |
| 781 |
} |
| 782 |
} |
| 783 |
PLOG(PL_ALWAYS, "VifExampleApp::PeekPkt() %sbound packet IP dst>%s ", |
| 784 |
inbound ? "in" : "out", dstAddr.GetHostString()); |
| 785 |
PLOG(PL_ALWAYS, "src>%s length>%d\n", srcAddr.GetHostString(), ipPkt.GetLength()); |
| 786 |
break; |
| 787 |
} |
| 788 |
case ProtoPktETH::ARP: |
| 789 |
{ |
| 790 |
ProtoPktARP arp; |
| 791 |
if (!arp.InitFromBuffer((UINT32*)ethPkt.AccessPayload(), ethPkt.GetPayloadLength())) |
| 792 |
{ |
| 793 |
PLOG(PL_ERROR, "VifExampleApp::PeekPkt() received bad ARP packet?\n"); |
| 794 |
break; |
| 795 |
} |
| 796 |
PLOG(PL_ALWAYS,"VifExampleApp::PeekPkt() %sbound ARP ", |
| 797 |
inbound ? "in" : "out"); |
| 798 |
switch(arp.GetOpcode()) |
| 799 |
{ |
| 800 |
case ProtoPktARP::ARP_REQ: |
| 801 |
PLOG(PL_ALWAYS, "request "); |
| 802 |
break; |
| 803 |
case ProtoPktARP::ARP_REP: |
| 804 |
PLOG(PL_ALWAYS, "reply "); |
| 805 |
break; |
| 806 |
default: |
| 807 |
PLOG(PL_ALWAYS, "??? "); |
| 808 |
break; |
| 809 |
} |
| 810 |
ProtoAddress addr; |
| 811 |
arp.GetSenderHardwareAddress(addr); |
| 812 |
PLOG(PL_ALWAYS, "from eth:%s ", addr.GetHostString()); |
| 813 |
arp.GetSenderProtocolAddress(addr); |
| 814 |
PLOG(PL_ALWAYS, "ip:%s ", addr.GetHostString()); |
| 815 |
arp.GetTargetProtocolAddress(addr); |
| 816 |
PLOG(PL_ALWAYS, "for ip:%s ", addr.GetHostString()); |
| 817 |
if (ProtoPktARP::ARP_REP == arp.GetOpcode()) |
| 818 |
{ |
| 819 |
|
| 820 |
arp.GetTargetHardwareAddress(addr); |
| 821 |
PLOG(PL_ALWAYS, "eth:%s ", addr.GetHostString()); |
| 822 |
} |
| 823 |
PLOG(PL_ALWAYS, "\n"); |
| 824 |
break; |
| 825 |
} |
| 826 |
default: |
| 827 |
PLOG(PL_ERROR, "VifExampleApp::PeekPkt() unknown %s packet type\n", inbound ? "inbound" : "outbound"); |
| 828 |
break; |
| 829 |
} |
| 830 |
} // end VifExampleApp::PeekPkt() |
| 831 |
|
| 832 |
/** |
| 833 |
* @note We offset the buffer here by 2 bytes since |
| 834 |
* Ethernet header is 14 bytes |
| 835 |
* (i.e. not a multiple of 4 (sizeof(UINT32)) |
| 836 |
* This gives us a properly aligned buffer for |
| 837 |
* 32-bit aligned IP packets |
| 838 |
*/ |
| 839 |
void VifExampleApp::OnOutboundPkt(ProtoChannel& theChannel, |
| 840 |
ProtoChannel::Notification notifyType) |
| 841 |
{ |
| 842 |
const int BUFFER_MAX = 4096; |
| 843 |
UINT32 alignedBuffer[BUFFER_MAX/sizeof(UINT32)]; |
| 844 |
// offset by 2-bytes so IP content is 32-bit aligned |
| 845 |
UINT16* ethBuffer = ((UINT16*)alignedBuffer) + 1; |
| 846 |
unsigned int numBytes = BUFFER_MAX - 2; |
| 847 |
|
| 848 |
while (vif->Read((char*)ethBuffer, numBytes)) |
| 849 |
{ |
| 850 |
if (numBytes == 0) break; // no more packets to receive |
| 851 |
|
| 852 |
TRACE("read %u bytes from vif ...\n", numBytes); |
| 853 |
|
| 854 |
ProtoPktETH ethPkt((UINT32*)ethBuffer, BUFFER_MAX - 2); |
| 855 |
if (!ethPkt.InitFromBuffer(numBytes)) |
| 856 |
{ |
| 857 |
PLOG(PL_ERROR, "VifExampleApp::OnOutboundPkt() error: bad Ether frame\n"); |
| 858 |
continue; |
| 859 |
} |
| 860 |
|
| 861 |
// Implement "tx_loss" random packet dropping, if applicable |
| 862 |
// (Note we still enforce the "rate_limit" as if packet was sent |
| 863 |
if (!((tx_loss > 0.0) && (UniformRand(100.0) < tx_loss))) |
| 864 |
{ |
| 865 |
if (CLONE == vif_mode) |
| 866 |
cap->Forward((char*)ethBuffer, numBytes); // send using interface MAC addr |
| 867 |
else |
| 868 |
cap->Send((char*)ethBuffer, numBytes); // send using vif MAC addr (same as iface if REPLACE mode) |
| 869 |
if (GetDebugLevel() >= 2) PeekPkt(ethPkt, false); |
| 870 |
} |
| 871 |
else |
| 872 |
{ |
| 873 |
PLOG(PL_DETAIL, "VifExampleApp::OnOutboundPkt() dropped outbound packet\n"); |
| 874 |
} |
| 875 |
|
| 876 |
// If applicable, enforce rate limiting |
| 877 |
if (txrate_limit >= 0.0) |
| 878 |
{ |
| 879 |
vif->StopInputNotification(); |
| 880 |
double pktTime = 8*numBytes / txrate_limit; |
| 881 |
txrate_timer.SetInterval(pktTime); |
| 882 |
txrate_timer.SetRepeat(0); |
| 883 |
ActivateTimer(txrate_timer); |
| 884 |
break; |
| 885 |
} |
| 886 |
numBytes = BUFFER_MAX; |
| 887 |
} |
| 888 |
} // end VifExampleApp::OnOutboundPkt |
| 889 |
|
| 890 |
/** |
| 891 |
* (TBD) perhaps we could "test" the vif to see if |
| 892 |
* it has a packet already and just go ahead and read |
| 893 |
* it and immediately reshedule our timer, thus keeping |
| 894 |
* things "a little more real" so to speak |
| 895 |
* One way to do this would be to use "vif->SetBlocking(false)" |
| 896 |
* _after_ "vif->StopInputNotification()" above and then we |
| 897 |
* can use "vif->Read()" to look for a packet ready to go |
| 898 |
* upon our rate timeout here ... |
| 899 |
*/ |
| 900 |
bool VifExampleApp::OnTxRateTimeout(ProtoTimer& /*theTimer*/) |
| 901 |
{ |
| 902 |
vif->StartInputNotification(); |
| 903 |
return true; |
| 904 |
} // end VifExampleApp::OnTxRateTimeout() |
| 905 |
|
| 906 |
/** |
| 907 |
* @note We offset the buffer here by 2 bytes since |
| 908 |
* Ethernet header is 14 bytes |
| 909 |
* (i.e. not a multiple of 4 (sizeof(UINT32)) |
| 910 |
* This gives us a properly aligned buffer for |
| 911 |
* 32-bit aligned IP packets |
| 912 |
*/ |
| 913 |
void VifExampleApp::OnInboundPkt(ProtoChannel& theChannel, |
| 914 |
ProtoChannel::Notification notifyType) |
| 915 |
{ |
| 916 |
ProtoTime currentTime; |
| 917 |
if (ProtoChannel::NOTIFY_INPUT != notifyType) return; |
| 918 |
while(1) |
| 919 |
{ |
| 920 |
ProtoCap::Direction direction; |
| 921 |
|
| 922 |
const int BUFFER_MAX = 4096; |
| 923 |
UINT32 alignedBuffer[BUFFER_MAX/sizeof(UINT32)]; |
| 924 |
// offset by 2-bytes so IP content is 32-bit aligned |
| 925 |
UINT16* ethBuffer = ((UINT16*)alignedBuffer) + 1; |
| 926 |
unsigned int numBytes = BUFFER_MAX - 2; |
| 927 |
|
| 928 |
if (!cap->Recv((char*)ethBuffer, numBytes, &direction)) |
| 929 |
{ |
| 930 |
PLOG(PL_ERROR, "VifExampleApp::OnInboundPkt() ProtoCap::Recv() error\n"); |
| 931 |
break; |
| 932 |
} |
| 933 |
|
| 934 |
if (numBytes == 0) break; // no more packets to receive |
| 935 |
|
| 936 |
if ((ProtoCap::OUTBOUND != direction)) |
| 937 |
{ |
| 938 |
// Map ProtoPktETH instance into buffer and init for processing |
| 939 |
ProtoPktETH ethPkt((UINT32*)ethBuffer, BUFFER_MAX - 2); |
| 940 |
if (!ethPkt.InitFromBuffer(numBytes)) |
| 941 |
{ |
| 942 |
PLOG(PL_ERROR, "VifExampleApp::OnInboundPkt() error: bad Ether frame\n"); |
| 943 |
continue; |
| 944 |
} |
| 945 |
|
| 946 |
// In "MNE" environment, ignore packets from blocked MAC sources |
| 947 |
ProtoAddress srcMacAddr; |
| 948 |
ethPkt.GetSrcAddr(srcMacAddr); |
| 949 |
if ((0 != mne_block_list_len) && |
| 950 |
(MneIsBlocking(srcMacAddr.GetRawHostAddress()))) |
| 951 |
continue; // ignore packets blocked by MNE |
| 952 |
|
| 953 |
// Implement inbound rate limit |
| 954 |
if (rxrate_limit == 0.0) |
| 955 |
continue; |
| 956 |
else if (rxrate_limit > 0.0) |
| 957 |
{ |
| 958 |
rxlimit_rxbytes += numBytes; |
| 959 |
if (0 == rxlimit_avg_size) |
| 960 |
rxlimit_avg_size = numBytes; |
| 961 |
else // compute exponentially weighted moving average packet size |
| 962 |
rxlimit_avg_size = RXLIMIT_SIZE_ALPHA*numBytes + (1.0 - RXLIMIT_SIZE_ALPHA)*rxlimit_avg_size; |
| 963 |
if (rxlimit_drop_prob > 0.0) |
| 964 |
{ |
| 965 |
// weight drop probability based on packet size vs avg. |
| 966 |
double p = rxlimit_drop_prob * (double)numBytes/rxlimit_avg_size; |
| 967 |
if (UniformRand(1.0) < p) |
| 968 |
{ |
| 969 |
PLOG(PL_DETAIL, "VifExampleApp::OnInboundPkt() dropped inbound packet due to rate limit\n"); |
| 970 |
continue; |
| 971 |
} |
| 972 |
} |
| 973 |
} |
| 974 |
|
| 975 |
// Implement "rx_loss" random packet dropping, if applicable |
| 976 |
if ((rx_loss > 0.0) && (UniformRand(100.0) < rx_loss)) |
| 977 |
{ |
| 978 |
PLOG(PL_DETAIL, "VifExampleApp::OnInboundPkt() dropped inbound packet due to txloss\n"); |
| 979 |
continue; |
| 980 |
} |
| 981 |
|
| 982 |
// Only process IP & ARP packets (skip others) |
| 983 |
switch (ethPkt.GetType()) |
| 984 |
{ |
| 985 |
case ProtoPktETH::IP: |
| 986 |
case ProtoPktETH::IPv6: |
| 987 |
case ProtoPktETH::ARP: |
| 988 |
if (GetDebugLevel() >= 2) PeekPkt(ethPkt, true); |
| 989 |
vif->Write((char*)ethPkt.GetBuffer(), ethPkt.GetLength()); |
| 990 |
break; |
| 991 |
default: |
| 992 |
break; |
| 993 |
} |
| 994 |
} |
| 995 |
} // end while(1) |
| 996 |
if (rxrate_limit > 0.0) |
| 997 |
{ |
| 998 |
currentTime.GetCurrentTime(); |
| 999 |
if (rxlimit_prev_pkt_time_initialized == false) // first time through |
| 1000 |
{ |
| 1001 |
rxlimit_prev_pkt_time = currentTime; |
| 1002 |
rxlimit_prev_pkt_time_initialized = true; |
| 1003 |
} |
| 1004 |
else |
| 1005 |
{ |
| 1006 |
double deltaTime = ProtoTime::Delta(currentTime, rxlimit_prev_pkt_time); |
| 1007 |
if (deltaTime >= RXLIMIT_MIN_UPDATE_TIME) |
| 1008 |
{ |
| 1009 |
double currentRateSample = 8.0*rxlimit_rxbytes / deltaTime; |
| 1010 |
if (0.0 == rxlimit_avg_rate) |
| 1011 |
rxlimit_avg_rate = currentRateSample; |
| 1012 |
else |
| 1013 |
{ |
| 1014 |
double alpha = RXLIMIT_RATE_ALPHA; |
| 1015 |
// outside of nominal rates, increase alpha for faster response |
| 1016 |
if (currentRateSample > rxrate_limit_uppernominal) |
| 1017 |
alpha *= currentRateSample/rxrate_limit; |
| 1018 |
else if (currentRateSample < rxrate_limit_lowernominal) |
| 1019 |
alpha *= rxrate_limit/currentRateSample; |
| 1020 |
if (alpha > 1.0) alpha = 1.0; |
| 1021 |
rxlimit_avg_rate = alpha*currentRateSample + (1.0 - alpha)*rxlimit_avg_rate; |
| 1022 |
} |
| 1023 |
|
| 1024 |
if (rxlimit_avg_rate <= rxrate_limit) |
| 1025 |
rxlimit_drop_prob = 0.0; |
| 1026 |
else |
| 1027 |
rxlimit_drop_prob = 1.0 - (rxrate_limit / rxlimit_avg_rate); |
| 1028 |
|
| 1029 |
rxlimit_prev_pkt_time = currentTime; |
| 1030 |
rxlimit_rxbytes = 0; |
| 1031 |
} |
| 1032 |
} |
| 1033 |
} |
| 1034 |
} // end VifExampleApp::OnInboundPkt |
| 1035 |
|