Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 33c0b884 authored by Tomasz Wasilczyk's avatar Tomasz Wasilczyk Committed by Android (Google) Code Review
Browse files

Merge changes from topic "ethtool"

* changes:
  Implement ethtool get/set value operations
  Implement Netlink message printer
parents 0a0fbc84 a5c83a56
Loading
Loading
Loading
Loading
+14 −0
Original line number Diff line number Diff line
@@ -20,11 +20,25 @@ cc_library_static {
    vendor_available: true,
    relative_install_path: "hw",
    srcs: [
        "protocols/common/Empty.cpp",
        "protocols/common/Error.cpp",
        "protocols/generic/Ctrl.cpp",
        "protocols/generic/Generic.cpp",
        "protocols/generic/GenericMessageBase.cpp",
        "protocols/generic/Unknown.cpp",
        "protocols/route/Link.cpp",
        "protocols/route/Route.cpp",
        "protocols/MessageDefinition.cpp",
        "protocols/NetlinkProtocol.cpp",
        "protocols/all.cpp",
        "NetlinkRequest.cpp",
        "NetlinkSocket.cpp",
        "can.cpp",
        "common.cpp",
        "ethtool.cpp",
        "ifreqs.cpp",
        "libnetdevice.cpp",
        "printer.cpp",
        "vlan.cpp",
    ],
    export_include_dirs: ["include"],
+13 −9
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@

#pragma once

#include "types.h"

#include <android-base/macros.h>
#include <linux/rtnetlink.h>

@@ -23,12 +25,10 @@

namespace android::netdevice {

typedef unsigned short rtattrtype_t;  // as in rtnetlink.h
typedef __u16 nlmsgtype_t;            // as in netlink.h

/** Implementation details, do not use outside NetlinkRequest template. */
namespace impl {

// TODO(twasilczyk): use nlattr instead of rtattr
struct rtattr* addattr_l(struct nlmsghdr* n, size_t maxLen, rtattrtype_t type, const void* data,
                         size_t dataLen);
struct rtattr* addattr_nest(struct nlmsghdr* n, size_t maxLen, rtattrtype_t type);
@@ -36,6 +36,7 @@ void addattr_nest_end(struct nlmsghdr* n, struct rtattr* nest);

}  // namespace impl

// TODO(twasilczyk): rename to NetlinkMessage
/**
 * Wrapper around NETLINK_ROUTE messages, to build them in C++ style.
 *
@@ -44,6 +45,14 @@ void addattr_nest_end(struct nlmsghdr* n, struct rtattr* nest);
 */
template <class T, unsigned int BUFSIZE = 128>
struct NetlinkRequest {
    struct RequestData {
        struct nlmsghdr nlmsg;
        T data;
        char buf[BUFSIZE];
    };

    static constexpr size_t totalLength = sizeof(RequestData);

    /**
     * Create empty message.
     *
@@ -131,12 +140,7 @@ struct NetlinkRequest {

  private:
    bool mIsGood = true;

    struct {
        struct nlmsghdr nlmsg;
        T data;
        char buf[BUFSIZE];
    } mRequest = {};
    RequestData mRequest = {};

    struct rtattr* nestStart(rtattrtype_t type) {
        if (!mIsGood) return nullptr;
+20 −2
Original line number Diff line number Diff line
@@ -16,11 +16,18 @@

#include "NetlinkSocket.h"

#include <libnetdevice/printer.h>

#include <android-base/logging.h>

namespace android::netdevice {

NetlinkSocket::NetlinkSocket(int protocol) {
/**
 * Print all outbound/inbound Netlink messages.
 */
static constexpr bool kSuperVerbose = false;

NetlinkSocket::NetlinkSocket(int protocol) : mProtocol(protocol) {
    mFd.reset(socket(AF_NETLINK, SOCK_RAW, protocol));
    if (!mFd.ok()) {
        PLOG(ERROR) << "Can't open Netlink socket";
@@ -38,7 +45,13 @@ NetlinkSocket::NetlinkSocket(int protocol) {
    }
}

bool NetlinkSocket::send(struct nlmsghdr* nlmsg) {
bool NetlinkSocket::send(struct nlmsghdr* nlmsg, size_t totalLen) {
    if constexpr (kSuperVerbose) {
        nlmsg->nlmsg_seq = mSeq;
        LOG(VERBOSE) << (mFailed ? "(not)" : "")
                     << "sending Netlink message: " << toString(nlmsg, totalLen, mProtocol);
    }

    if (mFailed) return false;

    nlmsg->nlmsg_pid = 0;  // kernel
@@ -91,6 +104,11 @@ bool NetlinkSocket::receiveAck() {

    for (auto nlmsg = reinterpret_cast<struct nlmsghdr*>(buf); NLMSG_OK(nlmsg, remainingLen);
         nlmsg = NLMSG_NEXT(nlmsg, remainingLen)) {
        if constexpr (kSuperVerbose) {
            LOG(VERBOSE) << "received Netlink response: "
                         << toString(nlmsg, sizeof(buf), mProtocol);
        }

        // We're looking for error/ack message only, ignoring others.
        if (nlmsg->nlmsg_type != NLMSG_ERROR) {
            LOG(WARNING) << "Received unexpected Netlink message (ignored): " << nlmsg->nlmsg_type;
+4 −2
Original line number Diff line number Diff line
@@ -43,7 +43,7 @@ struct NetlinkSocket {
    template <class T, unsigned int BUFSIZE>
    bool send(NetlinkRequest<T, BUFSIZE>& req) {
        if (!req.isGood()) return false;
        return send(req.header());
        return send(req.header(), req.totalLength);
    }

    /**
@@ -54,11 +54,13 @@ struct NetlinkSocket {
    bool receiveAck();

  private:
    const int mProtocol;

    uint32_t mSeq = 0;
    base::unique_fd mFd;
    bool mFailed = false;

    bool send(struct nlmsghdr* msg);
    bool send(struct nlmsghdr* msg, size_t totalLen);

    DISALLOW_COPY_AND_ASSIGN(NetlinkSocket);
};
+25 −0
Original line number Diff line number Diff line
@@ -22,6 +22,8 @@

namespace android::netdevice {

socketparams::Params socketparams::current = general;

unsigned int nametoindex(const std::string& ifname) {
    const auto ifidx = if_nametoindex(ifname.c_str());
    if (ifidx != 0) return ifidx;
@@ -33,4 +35,27 @@ unsigned int nametoindex(const std::string& ifname) {
    return 0;
}

std::string sanitize(std::string str) {
    str.erase(std::find(str.begin(), str.end(), '\0'), str.end());

    const auto isInvalid = [](char c) { return !isprint(c); };
    std::replace_if(str.begin(), str.end(), isInvalid, '?');

    return str;
}

uint16_t crc16(const nlbuf<uint8_t> data, uint16_t crc) {
    for (const auto byte : data.getRaw()) {
        crc ^= byte;
        for (unsigned i = 0; i < 8; i++) {
            if (crc & 1) {
                crc = (crc >> 1) ^ 0xA001;
            } else {
                crc >>= 1;
            }
        }
    }
    return crc;
}

}  // namespace android::netdevice
Loading