// SPDX-License-Identifier: GPL-2.0-or-later /* * linux/drivers/net/netconsole.c * * Copyright (C) 2001 Ingo Molnar <mingo@redhat.com> * * This file contains the implementation of an IRQ-safe, crash-safe * kernel console implementation that outputs kernel messages to the * network. * * Modification history: * * 2001-09-17 started by Ingo Molnar. * 2003-08-11 2.6 port by Matt Mackall * simplified options * generic card hooks * works non-modular * 2003-09-07 rewritten with netpoll api
*/
/* Linked list of all configured targets */ static LIST_HEAD(target_list); /* target_cleanup_list is used to track targets that need to be cleaned outside * of target_list_lock. It should be cleaned in the same function it is * populated.
*/ static LIST_HEAD(target_cleanup_list);
/* This needs to be a spinlock because write_msg() cannot sleep */ static DEFINE_SPINLOCK(target_list_lock); /* This needs to be a mutex because netpoll_cleanup might sleep */ static DEFINE_MUTEX(target_cleanup_list_lock);
/* * Console driver for netconsoles. Register only consoles that have * an associated target of the same type.
*/ staticstruct console netconsole_ext, netconsole;
/* Features enabled in sysdata. Contrary to userdata, this data is populated by * the kernel. The fields are designed as bitwise flags, allowing multiple * features to be set in sysdata_fields.
*/ enum sysdata_feature { /* Populate the CPU that sends the message */
SYSDATA_CPU_NR = BIT(0), /* Populate the task name (as in current->comm) in sysdata */
SYSDATA_TASKNAME = BIT(1), /* Kernel release/version as part of sysdata */
SYSDATA_RELEASE = BIT(2), /* Include a per-target message ID as part of sysdata */
SYSDATA_MSGID = BIT(3),
};
/** * struct netconsole_target - Represents a configured netconsole target. * @list: Links this target into the target_list. * @group: Links us into the configfs subsystem hierarchy. * @userdata_group: Links to the userdata configfs hierarchy * @extradata_complete: Cached, formatted string of append * @userdata_length: String length of usedata in extradata_complete. * @sysdata_fields: Sysdata features enabled. * @msgcounter: Message sent counter. * @stats: Packet send stats for the target. Used for debugging. * @enabled: On / off knob to enable / disable target. * Visible from userspace (read-write). * We maintain a strict 1:1 correspondence between this and * whether the corresponding netpoll is active or inactive. * Also, other parameters of a target may be modified at * runtime only when it is disabled (enabled == 0). * @extended: Denotes whether console is extended or not. * @release: Denotes whether kernel release version should be prepended * to the message. Depends on extended console. * @np: The netpoll structure for this target. * Contains the other userspace visible parameters: * dev_name (read-write) * local_port (read-write) * remote_port (read-write) * local_ip (read-write) * remote_ip (read-write) * local_mac (read-only) * remote_mac (read-write) * @buf: The buffer used to send the full msg to the network stack
*/ struct netconsole_target { struct list_head list; #ifdef CONFIG_NETCONSOLE_DYNAMIC struct config_group group; struct config_group userdata_group; char extradata_complete[MAX_EXTRADATA_ENTRY_LEN * MAX_EXTRADATA_ITEMS];
size_t userdata_length; /* bit-wise with sysdata_feature bits */
u32 sysdata_fields; /* protected by target_list_lock */
u32 msgcounter; #endif struct netconsole_target_stats stats; bool enabled; bool extended; bool release; struct netpoll np; /* protected by target_list_lock */ char buf[MAX_PRINT_CHUNK];
};
/* * Targets that were created by parsing the boot/module option string * do not exist in the configfs hierarchy (and have NULL names) and will * never go away, so make these a no-op for them.
*/ staticvoid netconsole_target_get(struct netconsole_target *nt)
{ if (config_item_name(&nt->group.cg_item))
config_group_get(&nt->group);
}
staticvoid netconsole_target_put(struct netconsole_target *nt)
{ if (config_item_name(&nt->group.cg_item))
config_group_put(&nt->group);
}
/* * No danger of targets going away from under us when dynamic * reconfigurability is off.
*/ staticvoid netconsole_target_get(struct netconsole_target *nt)
{
}
/* Allocate and initialize with defaults. * Note that these targets get their config_item fields zeroed-out.
*/ staticstruct netconsole_target *alloc_and_init(void)
{ struct netconsole_target *nt;
nt = kzalloc(sizeof(*nt), GFP_KERNEL); if (!nt) return nt;
if (IS_ENABLED(CONFIG_NETCONSOLE_EXTENDED_LOG))
nt->extended = true; if (IS_ENABLED(CONFIG_NETCONSOLE_PREPEND_RELEASE))
nt->release = true;
/* Clean up every target in the cleanup_list and move the clean targets back to * the main target_list.
*/ staticvoid netconsole_process_cleanups_core(void)
{ struct netconsole_target *nt, *tmp; unsignedlong flags;
/* The cleanup needs RTNL locked */
ASSERT_RTNL();
mutex_lock(&target_cleanup_list_lock);
list_for_each_entry_safe(nt, tmp, &target_cleanup_list, list) { /* all entries in the cleanup_list needs to be disabled */
WARN_ON_ONCE(nt->enabled);
do_netpoll_cleanup(&nt->np); /* moved the cleaned target to target_list. Need to hold both * locks
*/
spin_lock_irqsave(&target_list_lock, flags);
list_move(&nt->list, &target_list);
spin_unlock_irqrestore(&target_list_lock, flags);
}
WARN_ON_ONCE(!list_empty(&target_cleanup_list));
mutex_unlock(&target_cleanup_list_lock);
}
/* Do the list cleanup with the rtnl lock hold. rtnl lock is necessary because * netdev might be cleaned-up by calling __netpoll_cleanup(),
*/ staticvoid netconsole_process_cleanups(void)
{ /* rtnl lock is called here, because it has precedence over * target_cleanup_list_lock mutex and target_cleanup_list
*/
rtnl_lock();
netconsole_process_cleanups_core();
rtnl_unlock();
}
/* Get rid of possible trailing newline, returning the new length */ staticvoid trim_newline(char *s, size_t maxlen)
{
size_t len;
len = strnlen(s, maxlen); if (s[len - 1] == '\n')
s[len - 1] = '\0';
}
/* * Attribute operations for netconsole_target.
*/
/* Iterate in the list of target, and make sure we don't have any console * register without targets of the same type
*/ staticvoid unregister_netcons_consoles(void)
{ struct netconsole_target *nt;
u32 console_type_needed = 0; unsignedlong flags;
/* * This one is special -- targets created through the configfs interface * are not enabled (and the corresponding netpoll activated) by default. * The user is expected to set the desired parameters first (which * would enable him to dynamically add new netpoll targets for new * network interfaces as and when they come up).
*/ static ssize_t enabled_store(struct config_item *item, constchar *buf, size_t count)
{ struct netconsole_target *nt = to_target(item); unsignedlong flags; bool enabled;
ssize_t ret;
mutex_lock(&dynamic_netconsole_mutex);
ret = kstrtobool(buf, &enabled); if (ret) goto out_unlock;
ret = -EINVAL; if (enabled == nt->enabled) {
pr_info("network logging has already %s\n",
nt->enabled ? "started" : "stopped"); goto out_unlock;
}
if (nt->extended && !console_is_registered(&netconsole_ext)) {
netconsole_ext.flags |= CON_ENABLED;
register_console(&netconsole_ext);
}
/* User might be enabling the basic format target for the very * first time, make sure the console is registered.
*/ if (!nt->extended && !console_is_registered(&netconsole)) {
netconsole.flags |= CON_ENABLED;
register_console(&netconsole);
}
/* * Skip netconsole_parser_cmdline() -- all the attributes are * already configured via configfs. Just print them out.
*/
netconsole_print_banner(&nt->np);
ret = netpoll_setup(&nt->np); if (ret) goto out_unlock;
nt->enabled = true;
pr_info("network logging started\n");
} else { /* false */ /* We need to disable the netconsole before cleaning it up * otherwise we might end up in write_msg() with * nt->np.dev == NULL and nt->enabled == true
*/
mutex_lock(&target_cleanup_list_lock);
spin_lock_irqsave(&target_list_lock, flags);
nt->enabled = false; /* Remove the target from the list, while holding * target_list_lock
*/
list_move(&nt->list, &target_cleanup_list);
spin_unlock_irqrestore(&target_list_lock, flags);
mutex_unlock(&target_cleanup_list_lock); /* Unregister consoles, whose the last target of that type got * disabled.
*/
unregister_netcons_consoles();
}
mutex_lock(&dynamic_netconsole_mutex); if (nt->enabled) {
pr_err("target (%s) is enabled, disable to update parameters\n",
config_item_name(&nt->group.cg_item));
ret = -EINVAL; goto out_unlock;
}
ret = kstrtobool(buf, &release); if (ret) goto out_unlock;
nt->release = release;
ret = strnlen(buf, count);
out_unlock:
mutex_unlock(&dynamic_netconsole_mutex); return ret;
}
mutex_lock(&dynamic_netconsole_mutex); if (nt->enabled) {
pr_err("target (%s) is enabled, disable to update parameters\n",
config_item_name(&nt->group.cg_item));
ret = -EINVAL; goto out_unlock;
}
ret = kstrtobool(buf, &extended); if (ret) goto out_unlock;
ret = strnlen(buf, count);
out_unlock:
mutex_unlock(&dynamic_netconsole_mutex); return ret;
}
/* Count number of entries we have in extradata. * This is important because the extradata_complete only supports * MAX_EXTRADATA_ITEMS entries. Before enabling any new {user,sys}data * feature, number of entries needs to checked for available space.
*/ static size_t count_extradata_entries(struct netconsole_target *nt)
{
size_t entries;
/* Userdata entries */
entries = list_count_nodes(&nt->userdata_group.cg_children); /* Plus sysdata entries */ if (nt->sysdata_fields & SYSDATA_CPU_NR)
entries += 1; if (nt->sysdata_fields & SYSDATA_TASKNAME)
entries += 1; if (nt->sysdata_fields & SYSDATA_RELEASE)
entries += 1; if (nt->sysdata_fields & SYSDATA_MSGID)
entries += 1;
/* Skip userdata with no value set */ if (strnlen(udm_item->value, MAX_EXTRADATA_VALUE_LEN) == 0) continue;
/* This doesn't overflow extradata_complete since it will write * one entry length (1/MAX_EXTRADATA_ITEMS long), entry count is * checked to not exceed MAX items with child_count above
*/
complete_idx += scnprintf(&nt->extradata_complete[complete_idx],
MAX_EXTRADATA_ENTRY_LEN, " %s=%s\n",
item->ci_name, udm_item->value);
}
nt->userdata_length = strnlen(nt->extradata_complete, sizeof(nt->extradata_complete));
}
ret = kstrtobool(buf, &cpu_nr_enabled); if (ret) return ret;
mutex_lock(&netconsole_subsys.su_mutex);
mutex_lock(&dynamic_netconsole_mutex);
curr = !!(nt->sysdata_fields & SYSDATA_CPU_NR); if (cpu_nr_enabled == curr) /* no change requested */ goto unlock_ok;
if (cpu_nr_enabled &&
count_extradata_entries(nt) >= MAX_EXTRADATA_ITEMS) { /* user wants the new feature, but there is no space in the * buffer.
*/
ret = -ENOSPC; goto unlock;
}
if (cpu_nr_enabled)
nt->sysdata_fields |= SYSDATA_CPU_NR; else /* This is special because extradata_complete might have * remaining data from previous sysdata, and it needs to be * cleaned.
*/
disable_sysdata_feature(nt, SYSDATA_CPU_NR);
/* Checking if a target by this name was created at boot time. If so, * attach a configfs entry to that target. This enables dynamic * control.
*/ if (!strncmp(name, NETCONSOLE_PARAM_TARGET_PREFIX,
strlen(NETCONSOLE_PARAM_TARGET_PREFIX))) {
nt = find_cmdline_target(name); if (nt) {
init_target_config_group(nt, name); return &nt->group;
}
}
nt = alloc_and_init(); if (!nt) return ERR_PTR(-ENOMEM);
/* Initialize the config_group member */
init_target_config_group(nt, name);
/* Adding, but it is disabled */
spin_lock_irqsave(&target_list_lock, flags);
list_add(&nt->list, &target_list);
spin_unlock_irqrestore(&target_list_lock, flags);
/* * The target may have never been enabled, or was manually disabled * before being removed so netpoll may have already been cleaned up.
*/ if (nt->enabled)
netpoll_cleanup(&nt->np);
/* * prepare_extradata - append sysdata at extradata_complete in runtime * @nt: target to send message to
*/ staticint prepare_extradata(struct netconsole_target *nt)
{ int extradata_len;
/* userdata was appended when configfs write helper was called * by update_userdata().
*/
extradata_len = nt->userdata_length;
if (!nt->sysdata_fields) goto out;
if (nt->sysdata_fields & SYSDATA_CPU_NR)
extradata_len += sysdata_append_cpu_nr(nt, extradata_len); if (nt->sysdata_fields & SYSDATA_TASKNAME)
extradata_len += sysdata_append_taskname(nt, extradata_len); if (nt->sysdata_fields & SYSDATA_RELEASE)
extradata_len += sysdata_append_release(nt, extradata_len); if (nt->sysdata_fields & SYSDATA_MSGID)
extradata_len += sysdata_append_msgid(nt, extradata_len);
/** * send_udp - Wrapper for netpoll_send_udp that counts errors * @nt: target to send message to * @msg: message to send * @len: length of message * * Calls netpoll_send_udp and classifies the return value. If an error * occurred it increments statistics in nt->stats accordingly. * Only calls netpoll_send_udp if CONFIG_NETCONSOLE_DYNAMIC is disabled.
*/ staticvoid send_udp(struct netconsole_target *nt, constchar *msg, int len)
{ int result = netpoll_send_udp(&nt->np, msg, len);
staticvoid send_fragmented_body(struct netconsole_target *nt, constchar *msgbody, int header_len, int msgbody_len, int extradata_len)
{ int sent_extradata, preceding_bytes; constchar *extradata = NULL; int body_len, offset = 0;
/* body_len represents the number of bytes that will be sent. This is * bigger than MAX_PRINT_CHUNK, thus, it will be split in multiple * packets
*/
body_len = msgbody_len + extradata_len;
/* In each iteration of the while loop below, we send a packet * containing the header and a portion of the body. The body is * composed of two parts: msgbody and extradata. We keep track of how * many bytes have been sent so far using the offset variable, which * ranges from 0 to the total length of the body.
*/ while (offset < body_len) { int this_header = header_len; bool msgbody_written = false; int this_offset = 0; int this_chunk = 0;
/* Not all msgbody data has been written yet */ if (offset < msgbody_len) {
this_chunk = min(msgbody_len - offset,
MAX_PRINT_CHUNK - this_header); if (WARN_ON_ONCE(this_chunk <= 0)) return;
memcpy(nt->buf + this_header, msgbody + offset,
this_chunk);
this_offset += this_chunk;
}
/* msgbody was finally written, either in the previous * messages and/or in the current buf. Time to write * the extradata.
*/
msgbody_written |= offset + this_offset >= msgbody_len;
/* Msg body is fully written and there is pending extradata to * write, append extradata in this chunk
*/ if (msgbody_written && offset + this_offset < body_len) { /* Track how much user data was already sent. First * time here, sent_userdata is zero
*/
sent_extradata = (offset + this_offset) - msgbody_len; /* offset of bytes used in current buf */
preceding_bytes = this_chunk + this_header;
if (WARN_ON_ONCE(sent_extradata < 0)) return;
this_chunk = min(extradata_len - sent_extradata,
MAX_PRINT_CHUNK - preceding_bytes); if (WARN_ON_ONCE(this_chunk < 0)) /* this_chunk could be zero if all the previous * message used all the buffer. This is not a * problem, extradata will be sent in the next * iteration
*/ return;
staticvoid send_msg_fragmented(struct netconsole_target *nt, constchar *msg, int msg_len, int release_len, int extradata_len)
{ int header_len, msgbody_len; constchar *msgbody;
/* need to insert extra header fields, detect header and msgbody */
msgbody = memchr(msg, ';', msg_len); if (WARN_ON_ONCE(!msgbody)) return;
/* * Transfer multiple chunks with the following extra header. * "ncfrag=<byte-offset>/<total-bytes>"
*/ if (release_len)
append_release(nt->buf);
/* Copy the header into the buffer */
memcpy(nt->buf + release_len, msg, header_len);
header_len += release_len;
/* for now on, the header will be persisted, and the msgbody * will be replaced
*/
send_fragmented_body(nt, msgbody, header_len, msgbody_len,
extradata_len);
}
/** * send_ext_msg_udp - send extended log message to target * @nt: target to send message to * @msg: extended log message to send * @msg_len: length of message * * Transfer extended log @msg to @nt. If @msg is longer than * MAX_PRINT_CHUNK, it'll be split and transmitted in multiple chunks with * ncfrag header field added to identify them.
*/ staticvoid send_ext_msg_udp(struct netconsole_target *nt, constchar *msg, int msg_len)
{ int release_len = 0; int extradata_len;
extradata_len = prepare_extradata(nt);
if (nt->release)
release_len = strlen(init_utsname()->release) + 1;
if (oops_only && !oops_in_progress) return; /* Avoid taking lock and disabling interrupts unnecessarily */ if (list_empty(&target_list)) return;
spin_lock_irqsave(&target_list_lock, flags);
list_for_each_entry(nt, &target_list, list) { if (!nt->extended && nt->enabled && netif_running(nt->np.dev)) { /* * We nest this inside the for-each-target loop above * so that we're able to get as much logging out to * at least one target if we die inside here, instead * of unnecessarily keeping all targets in lock-step.
*/
tmp = msg; for (left = len; left;) {
frag = min(left, MAX_PRINT_CHUNK);
send_udp(nt, tmp, frag);
tmp += frag;
left -= frag;
}
}
}
spin_unlock_irqrestore(&target_list_lock, flags);
}
staticint netpoll_parse_ip_addr(constchar *str, union inet_addr *addr)
{ constchar *end;
/* Allocate new target (from boot/module param) and setup netpoll for it */ staticstruct netconsole_target *alloc_param_target(char *target_config, int cmdline_count)
{ struct netconsole_target *nt; int err;
nt = alloc_and_init(); if (!nt) {
err = -ENOMEM; goto fail;
}
if (*target_config == '+') {
nt->extended = true;
target_config++;
}
/* Parse parameters and setup netpoll */
err = netconsole_parser_cmdline(&nt->np, target_config); if (err) goto fail;
err = netpoll_setup(&nt->np); if (err) {
pr_err("Not enabling netconsole for %s%d. Netpoll setup failed\n",
NETCONSOLE_PARAM_TARGET_PREFIX, cmdline_count); if (!IS_ENABLED(CONFIG_NETCONSOLE_DYNAMIC)) /* only fail if dynamic reconfiguration is set, * otherwise, keep the target in the list, but disabled.
*/ goto fail;
} else {
nt->enabled = true;
}
populate_configfs_item(nt, cmdline_count);
return nt;
fail:
kfree(nt); return ERR_PTR(err);
}
/* Cleanup netpoll for given target (from boot/module param) and free it */ staticvoid free_param_target(struct netconsole_target *nt)
{
netpoll_cleanup(&nt->np);
kfree(nt);
}
/* * Remove all targets and destroy them (only targets created * from the boot/module option exist here). Skipping the list * lock is safe here, and netpoll_cleanup() will sleep.
*/
list_for_each_entry_safe(nt, tmp, &target_list, list) {
list_del(&nt->list);
free_param_target(nt);
}
if (console_is_registered(&netconsole_ext))
unregister_console(&netconsole_ext); if (console_is_registered(&netconsole))
unregister_console(&netconsole);
dynamic_netconsole_exit();
unregister_netdevice_notifier(&netconsole_netdev_notifier);
/* * Targets created via configfs pin references on our module * and would first be rmdir(2)'ed from userspace. We reach * here only when they are already destroyed, and only those * created from the boot/module option are left, so remove and * destroy them. Skipping the list lock is safe here, and * netpoll_cleanup() will sleep.
*/
list_for_each_entry_safe(nt, tmp, &target_list, list) {
list_del(&nt->list);
free_param_target(nt);
}
}
/* * Use late_initcall to ensure netconsole is * initialized after network device driver if built-in. * * late_initcall() and module_init() are identical if built as module.
*/
late_initcall(init_netconsole);
module_exit(cleanup_netconsole);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.