HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/shared/ethtool-util.c

46,544 bytesGit mode 100644Download original fileLGPL-2.1-or-later

Git blob: 44e9c49e817c280cd0d5bb5f064910522a72d3dd

Lines 1001–1349 of 1,349

}
 
int ethtool_set_eee_settings(
                int *ethtool_fd,
                const char *ifname,
                int enabled,
                int tx_lpi_enabled,
                usec_t tx_lpi_timer_usec,
                uint32_t advertise) {
 
        int r;
 
        assert(ethtool_fd);
        assert(ifname);
 
        if (enabled < 0 &&
            tx_lpi_enabled < 0 &&
            tx_lpi_timer_usec == USEC_INFINITY &&
            advertise == 0)
                return 0; /* Nothing requested. */
 
        r = ethtool_connect(ethtool_fd);
        if (r < 0)
                return r;
 
        struct ethtool_eee ecmd = {
                .cmd = ETHTOOL_GEEE,
        };
        struct ifreq ifr = {
                .ifr_data = (void*) &ecmd,
        };
 
        strscpy(ifr.ifr_name, sizeof(ifr.ifr_name), ifname);
 
        if (ioctl(*ethtool_fd, SIOCETHTOOL, &ifr) < 0)
                return -errno;
 
        if (ecmd.supported == 0)
                return 0; /* Unsupported. */
 
        bool need_update = false;
 
        if (enabled >= 0)
                UPDATE(ecmd.eee_enabled, (uint32_t) enabled, need_update);
        if (tx_lpi_enabled >= 0)
                UPDATE(ecmd.tx_lpi_enabled, (uint32_t) tx_lpi_enabled, need_update);
        if (tx_lpi_timer_usec != USEC_INFINITY)
                UPDATE(ecmd.tx_lpi_timer, (uint32_t) MIN(DIV_ROUND_UP(tx_lpi_timer_usec, USEC_PER_MSEC), (usec_t) UINT32_MAX), need_update);
        if (advertise != 0)
                UPDATE(ecmd.advertised, advertise & ecmd.supported, need_update);
 
        if (!need_update)
                return 0; /* Nothing changed. */
 
        ecmd.cmd = ETHTOOL_SEEE;
        return RET_NERRNO(ioctl(*ethtool_fd, SIOCETHTOOL, &ifr));
}
 
int config_parse_advertise(
                const char *unit,
                const char *filename,
                unsigned line,
                const char *section,
                unsigned section_line,
                const char *lvalue,
                int ltype,
                const char *rvalue,
                void *data,
                void *userdata) {
 
        uint32_t *advertise = ASSERT_PTR(data);
        int r;
 
        assert(filename);
        assert(section);
        assert(lvalue);
        assert(rvalue);
 
        if (isempty(rvalue)) {
                /* Empty string resets the value. */
                memzero(advertise, sizeof(uint32_t) * N_ADVERTISE);
                return 0;
        }
 
        for (const char *p = rvalue;;) {
                _cleanup_free_ char *w = NULL;
                enum ethtool_link_mode_bit_indices mode;
 
                r = extract_first_word(&p, &w, NULL, 0);
                if (r == -ENOMEM)
                        return log_oom();
                if (r < 0) {
                        log_syntax(unit, LOG_WARNING, filename, line, r,
                                   "Failed to split advertise modes '%s', ignoring assignment: %m", rvalue);
                        return 0;
                }
                if (r == 0)
                        return 0;
 
                mode = ethtool_link_mode_bit_from_string(w);
                /* We reuse the kernel provided enum which does not contain negative value. So, the cast
                 * below is mandatory. Otherwise, the check below always passes and access an invalid address. */
                if ((int) mode < 0) {
                        log_syntax(unit, LOG_WARNING, filename, line, mode,
                                   "Failed to parse advertise mode, ignoring: %s", w);
                        continue;
                }
 
                advertise[mode / 32] |= 1UL << (mode % 32);
        }
}
 
int config_parse_mdi(
                const char *unit,
                const char *filename,
                unsigned line,
                const char *section,
                unsigned section_line,
                const char *lvalue,
                int ltype,
                const char *rvalue,
                void *data,
                void *userdata) {
 
        uint8_t *mdi = ASSERT_PTR(data);
 
        assert(filename);
        assert(rvalue);
 
        if (isempty(rvalue)) {
                *mdi = ETH_TP_MDI_INVALID;
                return 0;
        }
 
        if (STR_IN_SET(rvalue, "mdi", "straight")) {
                *mdi = ETH_TP_MDI;
                return 0;
        }
 
        if (STR_IN_SET(rvalue, "mdi-x", "mdix", "crossover")) {
                *mdi = ETH_TP_MDI_X;
                return 0;
        }
 
        if (streq(rvalue, "auto")) {
                *mdi = ETH_TP_MDI_AUTO;
                return 0;
        }
 
        log_syntax(unit, LOG_WARNING, filename, line, 0,
                   "Failed to parse %s= setting, ignoring assignment: %s", lvalue, rvalue);
        return 0;
}
 
int config_parse_ring_buffer_or_channel(
                const char *unit,
                const char *filename,
                unsigned line,
                const char *section,
                unsigned section_line,
                const char *lvalue,
                int ltype,
                const char *rvalue,
                void *data,
                void *userdata) {
 
        u32_opt *dst = ASSERT_PTR(data);
        uint32_t k;
        int r;
 
        assert(filename);
        assert(section);
        assert(lvalue);
        assert(rvalue);
 
        if (isempty(rvalue)) {
                dst->value = 0;
                dst->set = false;
                return 0;
        }
 
        if (streq(rvalue, "max")) {
                dst->value = 0;
                dst->set = true;
                return 0;
        }
 
        r = safe_atou32(rvalue, &k);
        if (r < 0) {
                log_syntax(unit, LOG_WARNING, filename, line, r,
                           "Failed to parse %s=, ignoring: %s", lvalue, rvalue);
                return 0;
        }
        if (k < 1) {
                log_syntax(unit, LOG_WARNING, filename, line, 0,
                           "Invalid %s= value, ignoring: %s", lvalue, rvalue);
                return 0;
        }
 
        dst->value = k;
        dst->set = true;
        return 0;
}
 
int config_parse_wol(
                const char *unit,
                const char *filename,
                unsigned line,
                const char *section,
                unsigned section_line,
                const char *lvalue,
                int ltype,
                const char *rvalue,
                void *data,
                void *userdata) {
 
        uint32_t new_opts = 0, *opts = data;
        int r;
 
        assert(filename);
        assert(section);
        assert(lvalue);
        assert(rvalue);
        assert(data);
 
        if (isempty(rvalue)) {
                *opts = UINT32_MAX; /* Do not update WOL option. */
                return 0;
        }
 
        if (streq(rvalue, "off")) {
                *opts = 0; /* Disable WOL. */
                return 0;
        }
 
        for (const char *p = rvalue;;) {
                _cleanup_free_ char *w = NULL;
                bool found = false;
 
                r = extract_first_word(&p, &w, NULL, 0);
                if (r == -ENOMEM)
                        return log_oom();
                if (r < 0) {
                        log_syntax(unit, LOG_WARNING, filename, line, r,
                                   "Failed to split wake-on-lan modes '%s', ignoring assignment: %m", rvalue);
                        return 0;
                }
                if (r == 0)
                        break;
 
                FOREACH_ELEMENT(option, wol_option_map)
                        if (streq(w, option->name)) {
                                new_opts |= option->opt;
                                found = true;
                                break;
                        }
 
                if (!found)
                        log_syntax(unit, LOG_WARNING, filename, line, 0,
                                   "Unknown wake-on-lan mode '%s', ignoring.", w);
        }
 
        if (*opts == UINT32_MAX)
                *opts = new_opts;
        else
                *opts |= new_opts;
 
        return 0;
}
 
int config_parse_coalesce_u32(
                const char *unit,
                const char *filename,
                unsigned line,
                const char *section,
                unsigned section_line,
                const char *lvalue,
                int ltype,
                const char *rvalue,
                void *data,
                void *userdata) {
        u32_opt *dst = data;
        uint32_t k;
        int r;
 
        if (isempty(rvalue)) {
                dst->value = 0;
                dst->set = false;
                return 0;
        }
 
        r = safe_atou32(rvalue, &k);
        if (r < 0) {
                log_syntax(unit, LOG_WARNING, filename, line, r,
                           "Failed to parse %s=, ignoring: %s", lvalue, rvalue);
                return 0;
        }
 
        dst->value = k;
        dst->set = true;
        return 0;
}
 
int config_parse_coalesce_sec(
                const char *unit,
                const char *filename,
                unsigned line,
                const char *section,
                unsigned section_line,
                const char *lvalue,
                int ltype,
                const char *rvalue,
                void *data,
                void *userdata) {
        u32_opt *dst = data;
        usec_t usec;
        int r;
 
        if (isempty(rvalue)) {
                dst->value = 0;
                dst->set = false;
                return 0;
        }
 
        r = parse_sec(rvalue, &usec);
        if (r < 0) {
                log_syntax(unit, LOG_WARNING, filename, line, r,
                           "Failed to parse coalesce setting value, ignoring: %s", rvalue);
                return 0;
        }
 
        if (usec > UINT32_MAX) {
                log_syntax(unit, LOG_WARNING, filename, line, 0,
                           "Too large %s= value, ignoring: %s", lvalue, rvalue);
                return 0;
        }
 
        if (STR_IN_SET(lvalue, "StatisticsBlockCoalesceSec", "CoalescePacketRateSampleIntervalSec") && usec < 1) {
                log_syntax(unit, LOG_WARNING, filename, line, 0,
                           "Invalid %s= value, ignoring: %s", lvalue, rvalue);
                return 0;
        }
 
        dst->value = (uint32_t) usec;
        dst->set = true;
 
        return 0;
}