2015-09-14 07:49:52 -05:00
|
|
|
/*
|
|
|
|
Copyright (C) 2015 Srivats P.
|
|
|
|
|
|
|
|
This file is part of "Ostinato"
|
|
|
|
|
|
|
|
This is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "devicemanager.h"
|
|
|
|
|
|
|
|
#include "abstractport.h"
|
2018-07-19 08:24:01 -05:00
|
|
|
#include "emuldevice.h"
|
2016-03-07 10:10:48 -06:00
|
|
|
#include "../common/emulation.h"
|
2018-07-26 10:23:33 -05:00
|
|
|
#include "hostdevice.h"
|
|
|
|
#include "interfaceinfo.h"
|
|
|
|
#include "nulldevice.h"
|
2015-09-14 07:49:52 -05:00
|
|
|
#include "packetbuffer.h"
|
|
|
|
|
2015-09-20 07:19:15 -05:00
|
|
|
#include "../common/emulproto.pb.h"
|
|
|
|
|
2015-09-14 07:49:52 -05:00
|
|
|
#include <qendian.h>
|
|
|
|
|
2016-01-08 08:31:42 -06:00
|
|
|
const quint64 kBcastMac = 0xffffffffffffULL;
|
|
|
|
|
2015-12-31 08:47:56 -06:00
|
|
|
inline UInt128 UINT128(OstEmul::Ip6Address x)
|
|
|
|
{
|
|
|
|
return UInt128(x.hi(), x.lo());
|
|
|
|
}
|
|
|
|
|
2016-01-08 08:31:42 -06:00
|
|
|
inline bool isMacMcast(quint64 mac)
|
|
|
|
{
|
2016-06-01 10:21:29 -05:00
|
|
|
return ((mac >> 40) & 0x01) == 0x01;
|
2016-01-08 08:31:42 -06:00
|
|
|
}
|
|
|
|
|
2015-09-20 07:19:15 -05:00
|
|
|
|
2015-11-14 05:36:43 -06:00
|
|
|
// XXX: Port owning DeviceManager already uses locks, so we don't use any
|
|
|
|
// locks within DeviceManager to protect deviceGroupList_ et.al.
|
2015-09-14 07:49:52 -05:00
|
|
|
|
|
|
|
DeviceManager::DeviceManager(AbstractPort *parent)
|
|
|
|
{
|
|
|
|
port_ = parent;
|
|
|
|
}
|
|
|
|
|
2018-07-26 10:23:33 -05:00
|
|
|
void DeviceManager::createHostDevices(void)
|
|
|
|
{
|
|
|
|
const InterfaceInfo *ifInfo = port_->interfaceInfo();
|
|
|
|
|
|
|
|
if (!ifInfo)
|
|
|
|
return;
|
|
|
|
|
|
|
|
NullDevice bcastDevice(this);
|
|
|
|
bcastDevice.setMac(kBcastMac);
|
|
|
|
|
2018-08-15 00:21:04 -05:00
|
|
|
int count = ifInfo->ip4.size();
|
2018-07-26 10:23:33 -05:00
|
|
|
for (int i = 0; i < count; i++) {
|
2018-12-11 07:23:07 -06:00
|
|
|
// XXX: Since we can't support multiple IPs with same mac,
|
2018-08-15 00:21:04 -05:00
|
|
|
// skip link-local IP - unless it is the only one
|
|
|
|
if (((ifInfo->ip4.at(i).address & 0xffff0000) == 0xa9fe0000)
|
|
|
|
&& (count > 1))
|
|
|
|
continue;
|
2018-07-26 10:23:33 -05:00
|
|
|
Device *device = HostDevice::create(port_->name(), this);
|
|
|
|
device->setMac(ifInfo->mac);
|
|
|
|
device->setIp4(ifInfo->ip4.at(i).address,
|
|
|
|
ifInfo->ip4.at(i).prefixLength,
|
|
|
|
ifInfo->ip4.at(i).gateway);
|
|
|
|
|
|
|
|
if (deviceList_.contains(device->key())) {
|
|
|
|
qWarning("%s: error adding host device %s (EEXIST)",
|
|
|
|
__FUNCTION__, qPrintable(device->config()));
|
|
|
|
delete device;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
hostDeviceList_.append(device);
|
|
|
|
deviceList_.insert(device->key(), device);
|
|
|
|
sortedDeviceList_.insert(device->key(), device);
|
|
|
|
|
|
|
|
bcastList_.insert(bcastDevice.key(), device);
|
|
|
|
qDebug("host(add): %s", qPrintable(device->config()));
|
2018-08-15 00:21:04 -05:00
|
|
|
|
2018-12-11 07:23:07 -06:00
|
|
|
break; // TODO: support multiple IPs with same mac
|
2018-08-15 00:21:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
count = ifInfo->ip6.size();
|
|
|
|
for (int i = 0; i < count; i++) {
|
2018-12-11 07:23:07 -06:00
|
|
|
// XXX: Since we can't support multiple IPs with same mac,
|
2018-08-15 00:21:04 -05:00
|
|
|
// skip link-local IP - unless it is the only one
|
|
|
|
if (((ifInfo->ip6.at(i).address.hi64() >> 48) == 0xfe80)
|
|
|
|
&& (count > 1))
|
|
|
|
continue;
|
|
|
|
NullDevice dk(this);
|
|
|
|
dk.setMac(ifInfo->mac);
|
|
|
|
Device *device = deviceList_.value(dk.key());
|
|
|
|
if (!device) {
|
|
|
|
device = HostDevice::create(port_->name(), this);
|
|
|
|
device->setMac(ifInfo->mac);
|
|
|
|
device->setIp6(ifInfo->ip6.at(i).address,
|
|
|
|
ifInfo->ip6.at(i).prefixLength,
|
|
|
|
ifInfo->ip6.at(i).gateway);
|
|
|
|
hostDeviceList_.append(device);
|
|
|
|
deviceList_.insert(device->key(), device);
|
|
|
|
sortedDeviceList_.insert(device->key(), device);
|
|
|
|
|
|
|
|
bcastList_.insert(bcastDevice.key(), device);
|
|
|
|
qDebug("host(add): %s", qPrintable(device->config()));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
device->setIp6(ifInfo->ip6.at(i).address,
|
|
|
|
ifInfo->ip6.at(i).prefixLength,
|
|
|
|
ifInfo->ip6.at(i).gateway);
|
|
|
|
qDebug("host(update): %s", qPrintable(device->config()));
|
|
|
|
}
|
|
|
|
|
2018-12-11 07:23:07 -06:00
|
|
|
break; // TODO: support multiple IPs with same mac
|
2018-07-26 10:23:33 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-14 07:49:52 -05:00
|
|
|
DeviceManager::~DeviceManager()
|
|
|
|
{
|
|
|
|
foreach(Device *dev, deviceList_)
|
|
|
|
delete dev;
|
2015-09-20 07:19:15 -05:00
|
|
|
|
|
|
|
foreach(OstProto::DeviceGroup *devGrp, deviceGroupList_)
|
|
|
|
delete devGrp;
|
2018-07-26 10:23:33 -05:00
|
|
|
|
|
|
|
foreach(Device *dev, hostDeviceList_)
|
|
|
|
delete dev;
|
2015-09-14 07:49:52 -05:00
|
|
|
}
|
|
|
|
|
2015-09-20 07:19:15 -05:00
|
|
|
int DeviceManager::deviceGroupCount()
|
2015-09-14 07:49:52 -05:00
|
|
|
{
|
2015-09-20 07:19:15 -05:00
|
|
|
return deviceGroupList_.size();
|
2015-09-14 07:49:52 -05:00
|
|
|
}
|
|
|
|
|
2015-09-20 07:19:15 -05:00
|
|
|
const OstProto::DeviceGroup* DeviceManager::deviceGroupAtIndex(int index)
|
2015-09-14 07:49:52 -05:00
|
|
|
{
|
2015-09-20 07:19:15 -05:00
|
|
|
if ((index < 0) || (index >= deviceGroupCount())) {
|
|
|
|
qWarning("%s: index %d out of range (0 - %d)", __FUNCTION__,
|
|
|
|
index, deviceGroupCount() - 1);
|
2015-09-14 07:49:52 -05:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-09-20 07:19:15 -05:00
|
|
|
// Sort List by 'id', get the id at 'index' and then corresponding devGrp
|
|
|
|
return deviceGroupList_.value(deviceGroupList_.uniqueKeys().value(index));
|
2015-09-14 07:49:52 -05:00
|
|
|
}
|
|
|
|
|
2015-09-20 07:19:15 -05:00
|
|
|
const OstProto::DeviceGroup* DeviceManager::deviceGroup(uint deviceGroupId)
|
2015-09-14 07:49:52 -05:00
|
|
|
{
|
2015-09-20 07:19:15 -05:00
|
|
|
return deviceGroupList_.value(deviceGroupId);
|
2015-09-14 07:49:52 -05:00
|
|
|
}
|
|
|
|
|
2015-09-20 07:19:15 -05:00
|
|
|
bool DeviceManager::addDeviceGroup(uint deviceGroupId)
|
2015-09-14 07:49:52 -05:00
|
|
|
{
|
2015-09-20 07:19:15 -05:00
|
|
|
OstProto::DeviceGroup *deviceGroup;
|
2015-09-14 07:49:52 -05:00
|
|
|
|
2015-09-20 07:19:15 -05:00
|
|
|
if (deviceGroupList_.contains(deviceGroupId)) {
|
2015-11-14 05:36:43 -06:00
|
|
|
qWarning("%s: deviceGroup id %u already exists", __FUNCTION__,
|
2015-09-20 07:19:15 -05:00
|
|
|
deviceGroupId);
|
2015-09-14 07:49:52 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-03-07 10:10:48 -06:00
|
|
|
deviceGroup = newDeviceGroup(port_->id());
|
2015-09-20 07:19:15 -05:00
|
|
|
deviceGroup->mutable_device_group_id()->set_id(deviceGroupId);
|
|
|
|
deviceGroupList_.insert(deviceGroupId, deviceGroup);
|
2015-09-14 07:49:52 -05:00
|
|
|
|
2015-09-20 07:19:15 -05:00
|
|
|
enumerateDevices(deviceGroup, kAdd);
|
|
|
|
|
|
|
|
// Start emulation when first device is added
|
2015-09-14 07:49:52 -05:00
|
|
|
if ((deviceCount() == 1) && port_)
|
|
|
|
port_->startDeviceEmulation();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-09-20 07:19:15 -05:00
|
|
|
bool DeviceManager::deleteDeviceGroup(uint deviceGroupId)
|
2015-09-14 07:49:52 -05:00
|
|
|
{
|
2015-09-20 07:19:15 -05:00
|
|
|
OstProto::DeviceGroup *deviceGroup;
|
|
|
|
if (!deviceGroupList_.contains(deviceGroupId)) {
|
2015-11-14 05:36:43 -06:00
|
|
|
qWarning("%s: deviceGroup id %u does not exist", __FUNCTION__,
|
2015-09-20 07:19:15 -05:00
|
|
|
deviceGroupId);
|
2015-09-14 07:49:52 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-09-20 07:19:15 -05:00
|
|
|
deviceGroup = deviceGroupList_.take(deviceGroupId);
|
|
|
|
enumerateDevices(deviceGroup, kDelete);
|
|
|
|
delete deviceGroup;
|
2015-09-14 07:49:52 -05:00
|
|
|
|
2015-09-20 07:19:15 -05:00
|
|
|
// Stop emulation if no devices remain
|
2015-09-14 07:49:52 -05:00
|
|
|
if ((deviceCount() == 0) && port_)
|
|
|
|
port_->stopDeviceEmulation();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-09-20 07:19:15 -05:00
|
|
|
bool DeviceManager::modifyDeviceGroup(const OstProto::DeviceGroup *deviceGroup)
|
2015-09-14 07:49:52 -05:00
|
|
|
{
|
2015-09-20 07:19:15 -05:00
|
|
|
quint32 id = deviceGroup->device_group_id().id();
|
|
|
|
OstProto::DeviceGroup *myDeviceGroup = deviceGroupList_.value(id);
|
|
|
|
if (!myDeviceGroup) {
|
|
|
|
qWarning("%s: deviceGroup id %u does not exist", __FUNCTION__, id);
|
2015-09-14 07:49:52 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-09-20 07:19:15 -05:00
|
|
|
enumerateDevices(myDeviceGroup, kDelete);
|
2015-12-31 08:47:56 -06:00
|
|
|
|
2015-09-20 07:19:15 -05:00
|
|
|
myDeviceGroup->CopyFrom(*deviceGroup);
|
2015-12-31 08:47:56 -06:00
|
|
|
// If mac step is 0, silently override to 1 - otherwise we won't have
|
|
|
|
// unique DeviceKeys
|
|
|
|
if (myDeviceGroup->GetExtension(OstEmul::mac).step() == 0)
|
|
|
|
myDeviceGroup->MutableExtension(OstEmul::mac)->set_step(1);
|
|
|
|
// Default value for ip6 step should be 1 (not 0)
|
|
|
|
if (myDeviceGroup->HasExtension(OstEmul::ip6)
|
|
|
|
&& !myDeviceGroup->GetExtension(OstEmul::ip6).has_step())
|
|
|
|
myDeviceGroup->MutableExtension(OstEmul::ip6)
|
|
|
|
->mutable_step()->set_lo(1);
|
|
|
|
|
2015-09-20 07:19:15 -05:00
|
|
|
enumerateDevices(myDeviceGroup, kAdd);
|
2015-09-14 07:49:52 -05:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-09-20 07:19:15 -05:00
|
|
|
int DeviceManager::deviceCount()
|
|
|
|
{
|
2018-12-12 10:30:09 -06:00
|
|
|
return deviceList_.size() + hostDeviceList_.size();
|
2015-09-20 07:19:15 -05:00
|
|
|
}
|
|
|
|
|
2015-11-11 01:35:15 -06:00
|
|
|
void DeviceManager::getDeviceList(
|
|
|
|
OstProto::PortDeviceList *deviceList)
|
|
|
|
{
|
2016-03-15 07:45:35 -05:00
|
|
|
foreach(Device *device, sortedDeviceList_) {
|
2015-11-11 01:35:15 -06:00
|
|
|
OstEmul::Device *dev =
|
2016-03-22 09:48:53 -05:00
|
|
|
deviceList->AddExtension(OstEmul::device);
|
2015-11-11 01:35:15 -06:00
|
|
|
device->getConfig(dev);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-14 07:49:52 -05:00
|
|
|
void DeviceManager::receivePacket(PacketBuffer *pktBuf)
|
|
|
|
{
|
2015-09-20 07:19:15 -05:00
|
|
|
uchar *pktData = pktBuf->data();
|
|
|
|
int offset = 0;
|
2018-07-19 08:24:01 -05:00
|
|
|
EmulDevice dk(this);
|
2015-09-20 07:19:15 -05:00
|
|
|
Device *device;
|
|
|
|
quint64 dstMac;
|
|
|
|
quint16 ethType;
|
|
|
|
quint16 vlan;
|
|
|
|
int idx = 0;
|
|
|
|
|
|
|
|
// We assume pkt is ethernet
|
|
|
|
// TODO: extend for other link layer types
|
|
|
|
|
2015-11-14 05:36:43 -06:00
|
|
|
// All frames we are interested in should be at least 32 bytes
|
|
|
|
if (pktBuf->length() < 32) {
|
|
|
|
qWarning("short frame of %d bytes, skipping ...", pktBuf->length());
|
2016-01-01 00:29:31 -06:00
|
|
|
goto _exit;
|
2015-11-14 05:36:43 -06:00
|
|
|
}
|
2015-09-20 07:19:15 -05:00
|
|
|
|
|
|
|
// Extract dstMac
|
|
|
|
dstMac = qFromBigEndian<quint32>(pktData + offset);
|
|
|
|
offset += 4;
|
|
|
|
dstMac = (dstMac << 16) | qFromBigEndian<quint16>(pktData + offset);
|
2016-01-08 08:31:42 -06:00
|
|
|
|
2018-05-20 03:03:38 -05:00
|
|
|
qDebug("dstMac %012llx", dstMac);
|
2016-01-08 08:31:42 -06:00
|
|
|
|
|
|
|
// XXX: Treat multicast as bcast
|
|
|
|
if (isMacMcast(dstMac))
|
|
|
|
dstMac = kBcastMac;
|
|
|
|
|
2015-09-20 07:19:15 -05:00
|
|
|
dk.setMac(dstMac);
|
|
|
|
offset += 2;
|
|
|
|
|
|
|
|
// Skip srcMac - don't care
|
|
|
|
offset += 6;
|
|
|
|
|
|
|
|
_eth_type:
|
|
|
|
// Extract EthType
|
|
|
|
ethType = qFromBigEndian<quint16>(pktData + offset);
|
|
|
|
qDebug("%s: ethType 0x%x", __PRETTY_FUNCTION__, ethType);
|
|
|
|
|
2015-12-20 08:03:02 -06:00
|
|
|
if (tpidList_.contains(ethType)) {
|
2015-09-20 07:19:15 -05:00
|
|
|
offset += 2;
|
|
|
|
vlan = qFromBigEndian<quint16>(pktData + offset);
|
|
|
|
dk.setVlan(idx++, vlan);
|
|
|
|
offset += 2;
|
2015-10-03 02:48:44 -05:00
|
|
|
qDebug("%s: idx: %d vlan %d", __FUNCTION__, idx, vlan);
|
2015-09-20 07:19:15 -05:00
|
|
|
goto _eth_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
pktBuf->pull(offset);
|
|
|
|
|
|
|
|
if (dstMac == kBcastMac) {
|
|
|
|
QList<Device*> list = bcastList_.values(dk.key());
|
2015-10-03 02:48:44 -05:00
|
|
|
// FIXME: We need to clone the pktBuf before passing to each
|
|
|
|
// device, otherwise only the first device gets the original
|
|
|
|
// packet - all subsequent ones get the modified packet!
|
|
|
|
// NOTE: modification may not be in the pkt data buffer but
|
|
|
|
// in the HDTE pointers - which is bad as well!
|
2015-09-20 07:19:15 -05:00
|
|
|
foreach(Device *device, list)
|
|
|
|
device->receivePacket(pktBuf);
|
|
|
|
goto _exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Is it destined for us?
|
|
|
|
device = deviceList_.value(dk.key());
|
|
|
|
if (!device) {
|
|
|
|
qDebug("%s: dstMac %012llx is not us", __FUNCTION__, dstMac);
|
|
|
|
goto _exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
device->receivePacket(pktBuf);
|
|
|
|
|
|
|
|
_exit:
|
2016-01-01 00:29:31 -06:00
|
|
|
delete pktBuf;
|
2015-09-14 07:49:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void DeviceManager::transmitPacket(PacketBuffer *pktBuf)
|
|
|
|
{
|
|
|
|
port_->sendEmulationPacket(pktBuf);
|
|
|
|
}
|
2015-09-20 07:19:15 -05:00
|
|
|
|
2016-01-28 08:31:19 -06:00
|
|
|
void DeviceManager::resolveDeviceGateways()
|
|
|
|
{
|
|
|
|
foreach(Device *device, deviceList_) {
|
|
|
|
device->resolveGateway();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-17 09:42:13 -05:00
|
|
|
void DeviceManager::clearDeviceNeighbors(Device::NeighborSet set)
|
2015-11-04 07:20:08 -06:00
|
|
|
{
|
|
|
|
foreach(Device *device, deviceList_)
|
2016-03-17 09:42:13 -05:00
|
|
|
device->clearNeighbors(set);
|
2015-11-04 07:20:08 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void DeviceManager::getDeviceNeighbors(
|
2015-11-10 23:22:29 -06:00
|
|
|
OstProto::PortNeighborList *neighborList)
|
2015-11-04 07:20:08 -06:00
|
|
|
{
|
|
|
|
int count = 0;
|
|
|
|
|
2016-03-15 07:45:35 -05:00
|
|
|
foreach(Device *device, sortedDeviceList_) {
|
2015-11-10 23:22:29 -06:00
|
|
|
OstEmul::DeviceNeighborList *neighList =
|
2016-03-22 09:48:53 -05:00
|
|
|
neighborList->AddExtension(OstEmul::device_neighbor);
|
2015-11-04 07:20:08 -06:00
|
|
|
neighList->set_device_index(count++);
|
|
|
|
device->getNeighbors(neighList);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DeviceManager::resolveDeviceNeighbor(PacketBuffer *pktBuf)
|
2015-11-10 08:10:32 -06:00
|
|
|
{
|
|
|
|
Device *device = originDevice(pktBuf);
|
|
|
|
|
|
|
|
if (device)
|
|
|
|
device->resolveNeighbor(pktBuf);
|
|
|
|
}
|
|
|
|
|
|
|
|
quint64 DeviceManager::deviceMacAddress(PacketBuffer *pktBuf)
|
|
|
|
{
|
|
|
|
Device *device = originDevice(pktBuf);
|
|
|
|
|
|
|
|
return device ? device->mac() : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
quint64 DeviceManager::neighborMacAddress(PacketBuffer *pktBuf)
|
|
|
|
{
|
|
|
|
Device *device = originDevice(pktBuf);
|
|
|
|
|
|
|
|
return device ? device->neighborMac(pktBuf) : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------ //
|
|
|
|
// Private Methods
|
|
|
|
// ------------------------------------ //
|
|
|
|
|
|
|
|
Device* DeviceManager::originDevice(PacketBuffer *pktBuf)
|
2015-11-04 07:20:08 -06:00
|
|
|
{
|
|
|
|
uchar *pktData = pktBuf->data();
|
2015-11-10 08:10:32 -06:00
|
|
|
int offset = 12; // start parsing after mac addresses
|
2018-07-19 08:24:01 -05:00
|
|
|
EmulDevice dk(this);
|
2015-11-04 07:20:08 -06:00
|
|
|
quint16 ethType;
|
|
|
|
quint16 vlan;
|
|
|
|
int idx = 0;
|
|
|
|
|
2016-01-28 09:51:24 -06:00
|
|
|
// Do we have any devices at all?
|
|
|
|
if (!deviceCount())
|
|
|
|
return NULL;
|
|
|
|
|
2015-11-10 08:10:32 -06:00
|
|
|
// pktBuf will not have the correct dstMac populated, so use bcastMac
|
|
|
|
// and search for device by IP
|
2015-11-04 07:20:08 -06:00
|
|
|
|
2015-11-10 08:10:32 -06:00
|
|
|
dk.setMac(kBcastMac);
|
2015-11-04 07:20:08 -06:00
|
|
|
|
|
|
|
_eth_type:
|
|
|
|
ethType = qFromBigEndian<quint16>(pktData + offset);
|
|
|
|
qDebug("%s: ethType 0x%x", __PRETTY_FUNCTION__, ethType);
|
|
|
|
|
2015-12-20 08:03:02 -06:00
|
|
|
if (tpidList_.contains(ethType)) {
|
2015-11-04 07:20:08 -06:00
|
|
|
offset += 2;
|
|
|
|
vlan = qFromBigEndian<quint16>(pktData + offset);
|
|
|
|
dk.setVlan(idx++, vlan);
|
|
|
|
offset += 2;
|
|
|
|
qDebug("%s: idx: %d vlan %d", __FUNCTION__, idx, vlan);
|
|
|
|
goto _eth_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
pktBuf->pull(offset);
|
|
|
|
|
2015-11-10 08:10:32 -06:00
|
|
|
foreach(Device *device, bcastList_.values(dk.key())) {
|
|
|
|
if (device->isOrigin(pktBuf))
|
|
|
|
return device;
|
2015-11-04 07:20:08 -06:00
|
|
|
}
|
|
|
|
|
2015-11-10 08:10:32 -06:00
|
|
|
qDebug("couldn't find origin device for packet");
|
|
|
|
return NULL;
|
2015-11-04 07:20:08 -06:00
|
|
|
}
|
|
|
|
|
2015-09-20 07:19:15 -05:00
|
|
|
void DeviceManager::enumerateDevices(
|
|
|
|
const OstProto::DeviceGroup *deviceGroup,
|
|
|
|
Operation oper)
|
|
|
|
{
|
2018-07-19 08:24:01 -05:00
|
|
|
EmulDevice dk(this);
|
2015-12-16 09:18:17 -06:00
|
|
|
OstEmul::VlanEmulation pbVlan = deviceGroup->encap()
|
|
|
|
.GetExtension(OstEmul::vlan);
|
2015-09-20 07:19:15 -05:00
|
|
|
int numTags = pbVlan.stack_size();
|
2015-12-14 09:34:58 -06:00
|
|
|
int n = 1;
|
|
|
|
QList<int> vlanCount;
|
2015-09-20 07:19:15 -05:00
|
|
|
|
2016-01-01 00:29:31 -06:00
|
|
|
bool hasIp4 = deviceGroup->HasExtension(OstEmul::ip4);
|
|
|
|
bool hasIp6 = deviceGroup->HasExtension(OstEmul::ip6);
|
2015-11-10 23:22:29 -06:00
|
|
|
OstEmul::MacEmulation mac = deviceGroup->GetExtension(OstEmul::mac);
|
|
|
|
OstEmul::Ip4Emulation ip4 = deviceGroup->GetExtension(OstEmul::ip4);
|
2015-12-31 08:47:56 -06:00
|
|
|
OstEmul::Ip6Emulation ip6 = deviceGroup->GetExtension(OstEmul::ip6);
|
2015-11-10 23:22:29 -06:00
|
|
|
|
2015-12-14 09:34:58 -06:00
|
|
|
/*
|
|
|
|
* vlanCount[] stores the number of unique vlans at each tag level
|
|
|
|
* e.g. for a 3-tag config with 2, 3, 4 vlans at each level respectively
|
|
|
|
* vlanCount = [24, 12, 4]
|
|
|
|
* 0 - 0, 0, 0
|
|
|
|
* 1 - 0, 0, 1
|
|
|
|
* 2 - 0, 0, 2
|
|
|
|
* 3 - 0, 0, 3
|
|
|
|
* 4 - 0, 1, 0
|
|
|
|
* 5 - 0, 1, 1
|
|
|
|
* 6 - 0, 1, 2
|
|
|
|
* 7 - 0, 1, 3
|
|
|
|
* 8 - 0, 2, 0
|
|
|
|
* 9 - 0, 2, 1
|
|
|
|
* 10 - 0, 2, 2
|
|
|
|
* 11 - 0, 2, 3
|
|
|
|
* 12 - 1, 0, 0
|
|
|
|
* 13 - 1, 0, 1
|
|
|
|
* 14 - 1, 0, 2
|
|
|
|
* 15 - 1, 0, 3
|
|
|
|
* 16 - 1, 1, 0
|
|
|
|
* 17 - 1, 1, 1
|
|
|
|
* 18 - 1, 1, 2
|
|
|
|
* 19 - 1, 1, 3
|
|
|
|
* 21 - 1, 2, 0
|
|
|
|
* 21 - 1, 2, 1
|
|
|
|
* 22 - 1, 2, 2
|
|
|
|
* 23 - 1, 2, 3
|
|
|
|
*
|
|
|
|
* Note that vlanCount[0] repesents total-number-of-vlans
|
|
|
|
*
|
|
|
|
* Another way to think about this is that at a particular vlan tag
|
|
|
|
* level, we need to repeat a particular vlan-id as many times as the
|
|
|
|
* next level's count before we can increment the vlan-id at that level
|
|
|
|
*
|
|
|
|
* We use this list to calculate the vlan ids for each tag level for
|
|
|
|
* all the vlans.
|
|
|
|
*
|
|
|
|
* For implementation convenience we append a '1' as the last element
|
|
|
|
*/
|
|
|
|
vlanCount.append(n);
|
|
|
|
for (int i = numTags - 1; i >= 0 ; i--) {
|
2015-12-20 08:03:02 -06:00
|
|
|
OstEmul::VlanEmulation::Vlan vlan = pbVlan.stack(i);
|
|
|
|
n *= vlan.count();
|
2015-12-14 09:34:58 -06:00
|
|
|
vlanCount.prepend(n);
|
2015-12-20 08:03:02 -06:00
|
|
|
|
|
|
|
// Update TPID list
|
|
|
|
switch (oper) {
|
|
|
|
case kAdd:
|
|
|
|
tpidList_[vlan.tpid()]++;
|
|
|
|
break;
|
|
|
|
case kDelete:
|
|
|
|
tpidList_[vlan.tpid()]--;
|
|
|
|
if (tpidList_[vlan.tpid()] == 0)
|
|
|
|
tpidList_.remove(vlan.tpid());
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
Q_ASSERT(0); // Unreachable
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QHash<quint16, uint>::const_iterator iter = tpidList_.constBegin();
|
|
|
|
qDebug("Port %s TPID List:", port_->name());
|
|
|
|
while (iter != tpidList_.constEnd()) {
|
|
|
|
qDebug("tpid: %x (%d)", iter.key(), iter.value());
|
|
|
|
iter++;
|
2015-12-14 09:34:58 -06:00
|
|
|
}
|
2015-09-20 07:19:15 -05:00
|
|
|
|
2015-12-14 09:34:58 -06:00
|
|
|
for (int i = 0; i < vlanCount.at(0); i++) {
|
2015-09-20 07:19:15 -05:00
|
|
|
for (int j = 0; j < numTags; j++) {
|
|
|
|
OstEmul::VlanEmulation::Vlan vlan = pbVlan.stack(j);
|
2015-12-14 09:34:58 -06:00
|
|
|
quint16 vlanAdd = (i/vlanCount.at(j+1) % vlan.count())*vlan.step();
|
2015-09-20 07:19:15 -05:00
|
|
|
|
2015-12-20 08:03:02 -06:00
|
|
|
dk.setVlan(j, vlan.vlan_tag() + vlanAdd, vlan.tpid());
|
2015-09-20 07:19:15 -05:00
|
|
|
}
|
|
|
|
|
2015-11-10 23:22:29 -06:00
|
|
|
for (uint k = 0; k < deviceGroup->device_count(); k++) {
|
2015-09-20 07:19:15 -05:00
|
|
|
Device *device;
|
2015-11-10 23:22:29 -06:00
|
|
|
quint64 macAdd = k * mac.step();
|
|
|
|
quint32 ip4Add = k * ip4.step();
|
2015-12-31 08:47:56 -06:00
|
|
|
UInt128 ip6Add = UINT128(ip6.step()) * k;
|
2015-09-20 07:19:15 -05:00
|
|
|
|
2015-11-10 23:22:29 -06:00
|
|
|
dk.setMac(mac.address() + macAdd);
|
2016-01-01 00:29:31 -06:00
|
|
|
if (hasIp4)
|
|
|
|
dk.setIp4(ip4.address() + ip4Add,
|
|
|
|
ip4.prefix_length(),
|
|
|
|
ip4.default_gateway());
|
|
|
|
if (hasIp6)
|
|
|
|
dk.setIp6(UINT128(ip6.address()) + ip6Add,
|
|
|
|
ip6.prefix_length(),
|
|
|
|
UINT128(ip6.default_gateway()));
|
2015-09-20 07:19:15 -05:00
|
|
|
|
|
|
|
switch (oper) {
|
|
|
|
case kAdd:
|
2016-03-07 07:12:14 -06:00
|
|
|
if (deviceList_.contains(dk.key())) {
|
|
|
|
qWarning("%s: error adding device %s (EEXIST)",
|
|
|
|
__FUNCTION__, qPrintable(dk.config()));
|
|
|
|
break;
|
|
|
|
}
|
2018-07-19 08:24:01 -05:00
|
|
|
device = new EmulDevice(this);
|
2015-09-20 07:19:15 -05:00
|
|
|
*device = dk;
|
|
|
|
deviceList_.insert(dk.key(), device);
|
2016-03-15 07:45:35 -05:00
|
|
|
sortedDeviceList_.insert(dk.key(), device);
|
2015-09-20 07:19:15 -05:00
|
|
|
|
|
|
|
dk.setMac(kBcastMac);
|
|
|
|
bcastList_.insert(dk.key(), device);
|
2018-07-19 08:24:01 -05:00
|
|
|
qDebug("enumerate(add): %p %s", device, qPrintable(device->config()));
|
2015-09-20 07:19:15 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
case kDelete:
|
|
|
|
device = deviceList_.take(dk.key());
|
2016-03-07 07:12:14 -06:00
|
|
|
if (!device) {
|
|
|
|
qWarning("%s: error deleting device %s (NOTFOUND)",
|
|
|
|
__FUNCTION__, qPrintable(dk.config()));
|
|
|
|
break;
|
|
|
|
}
|
2018-07-19 08:24:01 -05:00
|
|
|
qDebug("enumerate(del): %p %s", device, qPrintable(device->config()));
|
2015-09-20 07:19:15 -05:00
|
|
|
delete device;
|
2016-03-15 07:45:35 -05:00
|
|
|
sortedDeviceList_.take(dk.key()); // already freed above
|
2015-09-20 07:19:15 -05:00
|
|
|
|
|
|
|
dk.setMac(kBcastMac);
|
|
|
|
bcastList_.take(dk.key()); // device already freed above
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
Q_ASSERT(0); // Unreachable
|
|
|
|
}
|
|
|
|
} // foreach device
|
|
|
|
} // foreach vlan
|
|
|
|
}
|