From bef731a331bd0d3f302e5ff6a61c672eb6b399bf Mon Sep 17 00:00:00 2001 From: Augustin Cavalier Date: Wed, 26 Apr 2017 22:10:38 -0400 Subject: [PATCH] modem: Adapt to the new network stack. This is my first time using most of these API calls to the network stack, so, reviews most welcome. --- src/add-ons/kernel/network/ppp/Jamfile | 2 +- src/add-ons/kernel/network/ppp/modem/Modem.h | 9 ++- .../kernel/network/ppp/modem/ModemDevice.cpp | 71 +++++++++---------- .../kernel/network/ppp/modem/ModemDevice.h | 4 +- src/add-ons/kernel/network/ppp/modem/fcs.h | 2 +- .../kernel/network/ppp/modem/modem.cpp | 20 +++--- 6 files changed, 54 insertions(+), 54 deletions(-) diff --git a/src/add-ons/kernel/network/ppp/Jamfile b/src/add-ons/kernel/network/ppp/Jamfile index 0124e016cc..5a028f0625 100644 --- a/src/add-ons/kernel/network/ppp/Jamfile +++ b/src/add-ons/kernel/network/ppp/Jamfile @@ -1,7 +1,7 @@ SubDir HAIKU_TOP src add-ons kernel network ppp ; SubInclude HAIKU_TOP src add-ons kernel network ppp ipcp ; -# SubInclude HAIKU_TOP src add-ons kernel network ppp modem ; +SubInclude HAIKU_TOP src add-ons kernel network ppp modem ; SubInclude HAIKU_TOP src add-ons kernel network ppp pap ; SubInclude HAIKU_TOP src add-ons kernel network ppp pppoe ; SubInclude HAIKU_TOP src add-ons kernel network ppp shared ; diff --git a/src/add-ons/kernel/network/ppp/modem/Modem.h b/src/add-ons/kernel/network/ppp/modem/Modem.h index 9e9037332b..29e17feda1 100644 --- a/src/add-ons/kernel/network/ppp/modem/Modem.h +++ b/src/add-ons/kernel/network/ppp/modem/Modem.h @@ -8,7 +8,9 @@ #include #include -#include + +#include +#include class ModemDevice; @@ -30,12 +32,13 @@ class ModemDevice; #define MODEM_INIT_KEY "Init" #define MODEM_DIAL_KEY "Dial" -extern struct core_module_info *core; +extern net_stack_module_info *gStackModule; +extern net_buffer_module_info *gBufferModule; #if DEBUG // defined in ModemDevice.cpp -extern void dump_packet(struct mbuf *packet); +extern void dump_packet(net_buffer *packet); #endif // DEBUG diff --git a/src/add-ons/kernel/network/ppp/modem/ModemDevice.cpp b/src/add-ons/kernel/network/ppp/modem/ModemDevice.cpp index 8f37adb93d..01fd5fd9c4 100644 --- a/src/add-ons/kernel/network/ppp/modem/ModemDevice.cpp +++ b/src/add-ons/kernel/network/ppp/modem/ModemDevice.cpp @@ -9,7 +9,6 @@ #include "ACFCHandler.h" #include "fcs.h" -#include #include #include // for port settings @@ -21,7 +20,7 @@ #if DEBUG static char sDigits[] = "0123456789ABCDEF"; void -dump_packet(struct mbuf *packet) +dump_packet(net_buffer *packet) { if(!packet) return; @@ -404,49 +403,40 @@ ModemDevice::ConnectionLost() status_t -ModemDevice::Send(struct mbuf *packet, uint16 protocolNumber) +ModemDevice::Send(net_buffer *packet, uint16 protocolNumber) { #if DEBUG TRACE("ModemDevice: Send()\n"); dump_packet(packet); #endif - if(!packet) + if (!packet) return B_ERROR; - else if(InitCheck() != B_OK || protocolNumber != 0) { - m_freem(packet); + else if (InitCheck() != B_OK || protocolNumber != 0) { + gBufferModule->free(packet); return B_ERROR; - } else if(!IsUp()) { - m_freem(packet); + } else if (!IsUp()) { + gBufferModule->free(packet); return PPP_NO_CONNECTION; } - // we might need room for our header - if(fACFC->LocalState() != ACFC_ACCEPTED) { - M_PREPEND(packet, 2); - if(!packet) - return B_ERROR; - } - - int32 position = 0, length; - if(packet->m_flags & M_PKTHDR) - length = packet->m_pkthdr.len; - else - length = packet->m_len; - - // we need a contiguous chunk of memory - packet = m_pullup(packet, length); - if(!packet) - return B_ERROR; - - uint8 buffer[2 * (MODEM_MTU + PACKET_OVERHEAD)], *data = mtod(packet, uint8*); + uint8 buffer[2 * (MODEM_MTU + PACKET_OVERHEAD)]; // add header - if(fACFC->LocalState() != ACFC_ACCEPTED) { + if (fACFC->LocalState() != ACFC_ACCEPTED) { + NetBufferPrepend bufferHeader(packet, 2); + uint8* data = bufferHeader.operator->(); data[0] = ALL_STATIONS; data[1] = UI; } + int32 position = 0, length = packet->size; + uint8* data; + if (gBufferModule->direct_access(packet, 0, length + 2, (void**)&data) != B_OK) { + ERROR("ModemDevice: Failed to access buffer!\n"); + return B_ERROR; + } + // add FCS uint16 fcs = 0xffff; fcs = pppfcs16(fcs, data, length); @@ -468,11 +458,12 @@ ModemDevice::Send(struct mbuf *packet, uint16 protocolNumber) buffer[position++] = FLAG_SEQUENCE; // mark end of packet - m_freem(packet); + gBufferModule->free(packet); + data = NULL; // send to modem atomic_add((int32*) &fOutputBytes, position); - if(write(Handle(), buffer, position) < 0) + if (write(Handle(), buffer, position) < 0) return PPP_NO_CONNECTION; atomic_add((int32*) &fOutputBytes, -position); @@ -484,8 +475,7 @@ status_t ModemDevice::DataReceived(uint8 *buffer, uint32 length) { // TODO: report corrupted packets to KPPPInterface - - if(length < 3) + if (length < 3) return B_ERROR; // check FCS @@ -498,9 +488,12 @@ ModemDevice::DataReceived(uint8 *buffer, uint32 length) if(buffer[0] == ALL_STATIONS && buffer[1] == UI) buffer += 2; - mbuf *packet = m_gethdr(MT_DATA); - packet->m_len = packet->m_pkthdr.len = length - 2; - uint8 *data = mtod(packet, uint8*); + net_buffer* packet = gBufferModule->create(length - 2); + uint8* data; + if (gBufferModule->direct_access(packet, 0, length, (void**)&data) != B_OK) { + ERROR("ModemDevice: Failed to access buffer!\n"); + return B_ERROR; + } memcpy(data, buffer, length - 2); return Receive(packet); @@ -508,14 +501,14 @@ ModemDevice::DataReceived(uint8 *buffer, uint32 length) status_t -ModemDevice::Receive(struct mbuf *packet, uint16 protocolNumber) +ModemDevice::Receive(net_buffer *packet, uint16 protocolNumber) { // we do not need to lock because only the worker_thread calls this method - if(!packet) + if (!packet) return B_ERROR; - else if(InitCheck() != B_OK || !IsUp()) { - m_freem(packet); + else if (InitCheck() != B_OK || !IsUp()) { + gBufferModule->free(packet); return B_ERROR; } diff --git a/src/add-ons/kernel/network/ppp/modem/ModemDevice.h b/src/add-ons/kernel/network/ppp/modem/ModemDevice.h index 5c3ff0b1ef..111559fde5 100644 --- a/src/add-ons/kernel/network/ppp/modem/ModemDevice.h +++ b/src/add-ons/kernel/network/ppp/modem/ModemDevice.h @@ -59,10 +59,10 @@ class ModemDevice : public KPPPDevice { void FailedDialing(); void ConnectionLost(); - virtual status_t Send(struct mbuf *packet, uint16 protocolNumber = 0); + virtual status_t Send(net_buffer *packet, uint16 protocolNumber = 0); status_t DataReceived(uint8 *buffer, uint32 length); // this will put the data into an mbuf and call Receive() - virtual status_t Receive(struct mbuf *packet, uint16 protocolNumber = 0); + virtual status_t Receive(net_buffer *packet, uint16 protocolNumber = 0); private: const char *fPortName, *fInitString, *fDialString; diff --git a/src/add-ons/kernel/network/ppp/modem/fcs.h b/src/add-ons/kernel/network/ppp/modem/fcs.h index 291fd9ef7c..8fbc48533b 100644 --- a/src/add-ons/kernel/network/ppp/modem/fcs.h +++ b/src/add-ons/kernel/network/ppp/modem/fcs.h @@ -53,7 +53,7 @@ /* * Calculate a new fcs given the current fcs and the new data. */ - u16 pppfcs16(u16 fcs, unsigned char *cp, int len) + u16 pppfcs16(u16 fcs, const unsigned char *cp, int len) { while (len--) fcs = (fcs >> 8) ^ fcstab[(fcs ^ *cp++) & 0xff]; diff --git a/src/add-ons/kernel/network/ppp/modem/modem.cpp b/src/add-ons/kernel/network/ppp/modem/modem.cpp index 34caabf9f9..fc583f6294 100644 --- a/src/add-ons/kernel/network/ppp/modem/modem.cpp +++ b/src/add-ons/kernel/network/ppp/modem/modem.cpp @@ -5,8 +5,6 @@ #include #include -#include -#include #include #include @@ -16,12 +14,12 @@ #define MODEM_MODULE_NAME NETWORK_MODULES_ROOT "ppp/modem" -struct core_module_info *core = NULL; +net_stack_module_info *gStackModule = NULL; +net_buffer_module_info *gBufferModule = NULL; status_t std_ops(int32 op, ...); -static -bool +static bool add_to(KPPPInterface& mainInterface, KPPPInterface *subInterface, driver_parameter *settings, ppp_module_key_type type) { @@ -60,14 +58,20 @@ _EXPORT status_t std_ops(int32 op, ...) { - switch(op) { + switch (op) { case B_MODULE_INIT: - if(get_module(NET_CORE_MODULE_NAME, (module_info**)&core) != B_OK) + if (get_module(NET_STACK_MODULE_NAME, (module_info**) &gStackModule) != B_OK) return B_ERROR; + if (get_module(NET_BUFFER_MODULE_NAME, + (module_info **)&gBufferModule) != B_OK) { + put_module(NET_STACK_MODULE_NAME); + return B_ERROR; + } return B_OK; case B_MODULE_UNINIT: - put_module(NET_CORE_MODULE_NAME); + put_module(NET_BUFFER_MODULE_NAME); + put_module(NET_STACK_MODULE_NAME); break; default: