| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238 |
- // 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
- */
- /****************************************************************
- *
- ****************************************************************/
- #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
- #include <linux/mm.h>
- #include <linux/init.h>
- #include <linux/module.h>
- #include <linux/slab.h>
- #include <linux/console.h>
- #include <linux/moduleparam.h>
- #include <linux/kernel.h>
- #include <linux/string.h>
- #include <linux/netpoll.h>
- #include <linux/inet.h>
- #include <linux/configfs.h>
- #include <linux/etherdevice.h>
- #include <linux/hex.h>
- #include <linux/u64_stats_sync.h>
- #include <linux/utsname.h>
- #include <linux/rtnetlink.h>
- #include <linux/workqueue.h>
- MODULE_AUTHOR("Matt Mackall <mpm@selenic.com>");
- MODULE_DESCRIPTION("Console driver for network interfaces");
- MODULE_LICENSE("GPL");
- #define MAX_PARAM_LENGTH 256
- #define MAX_EXTRADATA_ENTRY_LEN 256
- #define MAX_EXTRADATA_VALUE_LEN 200
- /* The number 3 comes from userdata entry format characters (' ', '=', '\n') */
- #define MAX_EXTRADATA_NAME_LEN (MAX_EXTRADATA_ENTRY_LEN - \
- MAX_EXTRADATA_VALUE_LEN - 3)
- #define MAX_USERDATA_ITEMS 256
- #define MAX_PRINT_CHUNK 1000
- static char config[MAX_PARAM_LENGTH];
- module_param_string(netconsole, config, MAX_PARAM_LENGTH, 0);
- MODULE_PARM_DESC(netconsole, " netconsole=[src-port]@[src-ip]/[dev],[tgt-port]@<tgt-ip>/[tgt-macaddr]");
- static bool oops_only;
- module_param(oops_only, bool, 0600);
- MODULE_PARM_DESC(oops_only, "Only log oops messages");
- #define NETCONSOLE_PARAM_TARGET_PREFIX "cmdline"
- #ifndef MODULE
- static int __init option_setup(char *opt)
- {
- strscpy(config, opt, MAX_PARAM_LENGTH);
- return 1;
- }
- __setup("netconsole=", option_setup);
- #endif /* MODULE */
- /* 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);
- static struct workqueue_struct *netconsole_wq;
- /*
- * Console driver for netconsoles. Register only consoles that have
- * an associated target of the same type.
- */
- static struct console netconsole_ext, netconsole;
- struct netconsole_target_stats {
- u64_stats_t xmit_drop_count;
- u64_stats_t enomem_count;
- struct u64_stats_sync syncp;
- };
- enum console_type {
- CONS_BASIC = BIT(0),
- CONS_EXTENDED = BIT(1),
- };
- /* 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),
- /* Sentinel: highest bit position */
- MAX_SYSDATA_ITEMS = 4,
- };
- enum target_state {
- STATE_DISABLED,
- STATE_ENABLED,
- STATE_DEACTIVATED,
- };
- /**
- * 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
- * @userdata: Cached, formatted string of append
- * @userdata_length: String length of userdata.
- * @sysdata: Cached, formatted string of append
- * @sysdata_fields: Sysdata features enabled.
- * @msgcounter: Message sent counter.
- * @stats: Packet send stats for the target. Used for debugging.
- * @state: State of the target.
- * Visible from userspace (read-write).
- * From a userspace perspective, the target is either enabled or
- * disabled. Internally, although both STATE_DISABLED and
- * STATE_DEACTIVATED correspond to inactive targets, the latter is
- * due to automatic interface state changes and will try
- * recover automatically, if the interface comes back
- * online.
- * Also, other parameters of a target may be modified at
- * runtime only when it is disabled (state != STATE_ENABLED).
- * @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
- * @resume_wq: Workqueue to resume deactivated target
- */
- struct netconsole_target {
- struct list_head list;
- #ifdef CONFIG_NETCONSOLE_DYNAMIC
- struct config_group group;
- struct config_group userdata_group;
- char *userdata;
- size_t userdata_length;
- char sysdata[MAX_EXTRADATA_ENTRY_LEN * MAX_SYSDATA_ITEMS];
- /* bit-wise with sysdata_feature bits */
- u32 sysdata_fields;
- /* protected by target_list_lock */
- u32 msgcounter;
- #endif
- struct netconsole_target_stats stats;
- enum target_state state;
- bool extended;
- bool release;
- struct netpoll np;
- /* protected by target_list_lock */
- char buf[MAX_PRINT_CHUNK];
- struct work_struct resume_wq;
- };
- #ifdef CONFIG_NETCONSOLE_DYNAMIC
- static struct configfs_subsystem netconsole_subsys;
- static DEFINE_MUTEX(dynamic_netconsole_mutex);
- static int __init dynamic_netconsole_init(void)
- {
- config_group_init(&netconsole_subsys.su_group);
- mutex_init(&netconsole_subsys.su_mutex);
- return configfs_register_subsystem(&netconsole_subsys);
- }
- static void __exit dynamic_netconsole_exit(void)
- {
- configfs_unregister_subsystem(&netconsole_subsys);
- }
- /*
- * 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.
- */
- static void netconsole_target_get(struct netconsole_target *nt)
- {
- if (config_item_name(&nt->group.cg_item))
- config_group_get(&nt->group);
- }
- static void netconsole_target_put(struct netconsole_target *nt)
- {
- if (config_item_name(&nt->group.cg_item))
- config_group_put(&nt->group);
- }
- static void dynamic_netconsole_mutex_lock(void)
- {
- mutex_lock(&dynamic_netconsole_mutex);
- }
- static void dynamic_netconsole_mutex_unlock(void)
- {
- mutex_unlock(&dynamic_netconsole_mutex);
- }
- #else /* !CONFIG_NETCONSOLE_DYNAMIC */
- static int __init dynamic_netconsole_init(void)
- {
- return 0;
- }
- static void __exit dynamic_netconsole_exit(void)
- {
- }
- /*
- * No danger of targets going away from under us when dynamic
- * reconfigurability is off.
- */
- static void netconsole_target_get(struct netconsole_target *nt)
- {
- }
- static void netconsole_target_put(struct netconsole_target *nt)
- {
- }
- static void populate_configfs_item(struct netconsole_target *nt,
- int cmdline_count)
- {
- }
- static void dynamic_netconsole_mutex_lock(void)
- {
- }
- static void dynamic_netconsole_mutex_unlock(void)
- {
- }
- #endif /* CONFIG_NETCONSOLE_DYNAMIC */
- /* Check if the target was bound by mac address. */
- static bool bound_by_mac(struct netconsole_target *nt)
- {
- return is_valid_ether_addr(nt->np.dev_mac);
- }
- /* Attempts to resume logging to a deactivated target. */
- static void resume_target(struct netconsole_target *nt)
- {
- if (netpoll_setup(&nt->np)) {
- /* netpoll fails setup once, do not try again. */
- nt->state = STATE_DISABLED;
- return;
- }
- nt->state = STATE_ENABLED;
- pr_info("network logging resumed on interface %s\n", nt->np.dev_name);
- }
- /* Checks if a deactivated target matches a device. */
- static bool deactivated_target_match(struct netconsole_target *nt,
- struct net_device *ndev)
- {
- if (nt->state != STATE_DEACTIVATED)
- return false;
- if (bound_by_mac(nt))
- return !memcmp(nt->np.dev_mac, ndev->dev_addr, ETH_ALEN);
- return !strncmp(nt->np.dev_name, ndev->name, IFNAMSIZ);
- }
- /* Process work scheduled for target resume. */
- static void process_resume_target(struct work_struct *work)
- {
- struct netconsole_target *nt;
- unsigned long flags;
- nt = container_of(work, struct netconsole_target, resume_wq);
- dynamic_netconsole_mutex_lock();
- spin_lock_irqsave(&target_list_lock, flags);
- /* Check if target is still deactivated as it may have been disabled
- * while resume was being scheduled.
- */
- if (nt->state != STATE_DEACTIVATED) {
- spin_unlock_irqrestore(&target_list_lock, flags);
- goto out_unlock;
- }
- /* resume_target is IRQ unsafe, remove target from
- * target_list in order to resume it with IRQ enabled.
- */
- list_del_init(&nt->list);
- spin_unlock_irqrestore(&target_list_lock, flags);
- resume_target(nt);
- /* At this point the target is either enabled or disabled and
- * was cleaned up before getting deactivated. Either way, add it
- * back to target list.
- */
- spin_lock_irqsave(&target_list_lock, flags);
- list_add(&nt->list, &target_list);
- spin_unlock_irqrestore(&target_list_lock, flags);
- out_unlock:
- dynamic_netconsole_mutex_unlock();
- }
- /* Allocate and initialize with defaults.
- * Note that these targets get their config_item fields zeroed-out.
- */
- static struct netconsole_target *alloc_and_init(void)
- {
- struct netconsole_target *nt;
- nt = kzalloc_obj(*nt);
- if (!nt)
- return nt;
- if (IS_ENABLED(CONFIG_NETCONSOLE_EXTENDED_LOG))
- nt->extended = true;
- if (IS_ENABLED(CONFIG_NETCONSOLE_PREPEND_RELEASE))
- nt->release = true;
- nt->np.name = "netconsole";
- strscpy(nt->np.dev_name, "eth0", IFNAMSIZ);
- nt->np.local_port = 6665;
- nt->np.remote_port = 6666;
- eth_broadcast_addr(nt->np.remote_mac);
- nt->state = STATE_DISABLED;
- INIT_WORK(&nt->resume_wq, process_resume_target);
- return nt;
- }
- /* Clean up every target in the cleanup_list and move the clean targets back to
- * the main target_list.
- */
- static void netconsole_process_cleanups_core(void)
- {
- struct netconsole_target *nt, *tmp;
- unsigned long 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->state == STATE_ENABLED);
- do_netpoll_cleanup(&nt->np);
- if (bound_by_mac(nt))
- memset(&nt->np.dev_name, 0, IFNAMSIZ);
- /* 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);
- }
- static void netconsole_print_banner(struct netpoll *np)
- {
- np_info(np, "local port %d\n", np->local_port);
- if (np->ipv6)
- np_info(np, "local IPv6 address %pI6c\n", &np->local_ip.in6);
- else
- np_info(np, "local IPv4 address %pI4\n", &np->local_ip.ip);
- np_info(np, "interface name '%s'\n", np->dev_name);
- np_info(np, "local ethernet address '%pM'\n", np->dev_mac);
- np_info(np, "remote port %d\n", np->remote_port);
- if (np->ipv6)
- np_info(np, "remote IPv6 address %pI6c\n", &np->remote_ip.in6);
- else
- np_info(np, "remote IPv4 address %pI4\n", &np->remote_ip.ip);
- np_info(np, "remote ethernet address %pM\n", np->remote_mac);
- }
- /* Parse the string and populate the `inet_addr` union. Return 0 if IPv4 is
- * populated, 1 if IPv6 is populated, and -1 upon failure.
- */
- static int netpoll_parse_ip_addr(const char *str, union inet_addr *addr)
- {
- const char *end = NULL;
- int len;
- len = strlen(str);
- if (!len)
- return -1;
- if (str[len - 1] == '\n')
- len -= 1;
- if (in4_pton(str, len, (void *)addr, -1, &end) > 0 &&
- (!end || *end == 0 || *end == '\n'))
- return 0;
- if (IS_ENABLED(CONFIG_IPV6) &&
- in6_pton(str, len, (void *)addr, -1, &end) > 0 &&
- (!end || *end == 0 || *end == '\n'))
- return 1;
- return -1;
- }
- #ifdef CONFIG_NETCONSOLE_DYNAMIC
- /*
- * Our subsystem hierarchy is:
- *
- * /sys/kernel/config/netconsole/
- * |
- * <target>/
- * | enabled
- * | release
- * | dev_name
- * | local_port
- * | remote_port
- * | local_ip
- * | remote_ip
- * | local_mac
- * | remote_mac
- * | transmit_errors
- * | userdata/
- * | <key>/
- * | value
- * | ...
- * |
- * <target>/...
- */
- static struct netconsole_target *to_target(struct config_item *item)
- {
- struct config_group *cfg_group;
- cfg_group = to_config_group(item);
- if (!cfg_group)
- return NULL;
- return container_of(to_config_group(item),
- struct netconsole_target, group);
- }
- /* Do the list cleanup with the rtnl lock hold. rtnl lock is necessary because
- * netdev might be cleaned-up by calling __netpoll_cleanup(),
- */
- static void 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 */
- static void 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.
- */
- static ssize_t enabled_show(struct config_item *item, char *buf)
- {
- return sysfs_emit(buf, "%d\n", to_target(item)->state == STATE_ENABLED);
- }
- static ssize_t extended_show(struct config_item *item, char *buf)
- {
- return sysfs_emit(buf, "%d\n", to_target(item)->extended);
- }
- static ssize_t release_show(struct config_item *item, char *buf)
- {
- return sysfs_emit(buf, "%d\n", to_target(item)->release);
- }
- static ssize_t dev_name_show(struct config_item *item, char *buf)
- {
- return sysfs_emit(buf, "%s\n", to_target(item)->np.dev_name);
- }
- static ssize_t local_port_show(struct config_item *item, char *buf)
- {
- return sysfs_emit(buf, "%d\n", to_target(item)->np.local_port);
- }
- static ssize_t remote_port_show(struct config_item *item, char *buf)
- {
- return sysfs_emit(buf, "%d\n", to_target(item)->np.remote_port);
- }
- static ssize_t local_ip_show(struct config_item *item, char *buf)
- {
- struct netconsole_target *nt = to_target(item);
- if (nt->np.ipv6)
- return sysfs_emit(buf, "%pI6c\n", &nt->np.local_ip.in6);
- else
- return sysfs_emit(buf, "%pI4\n", &nt->np.local_ip);
- }
- static ssize_t remote_ip_show(struct config_item *item, char *buf)
- {
- struct netconsole_target *nt = to_target(item);
- if (nt->np.ipv6)
- return sysfs_emit(buf, "%pI6c\n", &nt->np.remote_ip.in6);
- else
- return sysfs_emit(buf, "%pI4\n", &nt->np.remote_ip);
- }
- static ssize_t local_mac_show(struct config_item *item, char *buf)
- {
- struct net_device *dev = to_target(item)->np.dev;
- static const u8 bcast[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
- return sysfs_emit(buf, "%pM\n", dev ? dev->dev_addr : bcast);
- }
- static ssize_t remote_mac_show(struct config_item *item, char *buf)
- {
- return sysfs_emit(buf, "%pM\n", to_target(item)->np.remote_mac);
- }
- static ssize_t transmit_errors_show(struct config_item *item, char *buf)
- {
- struct netconsole_target *nt = to_target(item);
- u64 xmit_drop_count, enomem_count;
- unsigned int start;
- do {
- start = u64_stats_fetch_begin(&nt->stats.syncp);
- xmit_drop_count = u64_stats_read(&nt->stats.xmit_drop_count);
- enomem_count = u64_stats_read(&nt->stats.enomem_count);
- } while (u64_stats_fetch_retry(&nt->stats.syncp, start));
- return sysfs_emit(buf, "%llu\n", xmit_drop_count + enomem_count);
- }
- /* configfs helper to display if cpu_nr sysdata feature is enabled */
- static ssize_t sysdata_cpu_nr_enabled_show(struct config_item *item, char *buf)
- {
- struct netconsole_target *nt = to_target(item->ci_parent);
- bool cpu_nr_enabled;
- dynamic_netconsole_mutex_lock();
- cpu_nr_enabled = !!(nt->sysdata_fields & SYSDATA_CPU_NR);
- dynamic_netconsole_mutex_unlock();
- return sysfs_emit(buf, "%d\n", cpu_nr_enabled);
- }
- /* configfs helper to display if taskname sysdata feature is enabled */
- static ssize_t sysdata_taskname_enabled_show(struct config_item *item,
- char *buf)
- {
- struct netconsole_target *nt = to_target(item->ci_parent);
- bool taskname_enabled;
- dynamic_netconsole_mutex_lock();
- taskname_enabled = !!(nt->sysdata_fields & SYSDATA_TASKNAME);
- dynamic_netconsole_mutex_unlock();
- return sysfs_emit(buf, "%d\n", taskname_enabled);
- }
- static ssize_t sysdata_release_enabled_show(struct config_item *item,
- char *buf)
- {
- struct netconsole_target *nt = to_target(item->ci_parent);
- bool release_enabled;
- dynamic_netconsole_mutex_lock();
- release_enabled = !!(nt->sysdata_fields & SYSDATA_RELEASE);
- dynamic_netconsole_mutex_unlock();
- return sysfs_emit(buf, "%d\n", release_enabled);
- }
- /* Iterate in the list of target, and make sure we don't have any console
- * register without targets of the same type
- */
- static void unregister_netcons_consoles(void)
- {
- struct netconsole_target *nt;
- u32 console_type_needed = 0;
- unsigned long flags;
- spin_lock_irqsave(&target_list_lock, flags);
- list_for_each_entry(nt, &target_list, list) {
- if (nt->extended)
- console_type_needed |= CONS_EXTENDED;
- else
- console_type_needed |= CONS_BASIC;
- }
- spin_unlock_irqrestore(&target_list_lock, flags);
- if (!(console_type_needed & CONS_EXTENDED) &&
- console_is_registered(&netconsole_ext))
- unregister_console(&netconsole_ext);
- if (!(console_type_needed & CONS_BASIC) &&
- console_is_registered(&netconsole))
- unregister_console(&netconsole);
- }
- static ssize_t sysdata_msgid_enabled_show(struct config_item *item,
- char *buf)
- {
- struct netconsole_target *nt = to_target(item->ci_parent);
- bool msgid_enabled;
- dynamic_netconsole_mutex_lock();
- msgid_enabled = !!(nt->sysdata_fields & SYSDATA_MSGID);
- dynamic_netconsole_mutex_unlock();
- return sysfs_emit(buf, "%d\n", msgid_enabled);
- }
- /*
- * 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,
- const char *buf, size_t count)
- {
- struct netconsole_target *nt = to_target(item);
- bool enabled, current_enabled;
- unsigned long flags;
- ssize_t ret;
- dynamic_netconsole_mutex_lock();
- ret = kstrtobool(buf, &enabled);
- if (ret)
- goto out_unlock;
- /* When the user explicitly enables or disables a target that is
- * currently deactivated, reset its state to disabled. The DEACTIVATED
- * state only tracks interface-driven deactivation and should _not_
- * persist when the user manually changes the target's enabled state.
- */
- if (nt->state == STATE_DEACTIVATED)
- nt->state = STATE_DISABLED;
- ret = -EINVAL;
- current_enabled = nt->state == STATE_ENABLED;
- if (enabled == current_enabled) {
- pr_info("network logging has already %s\n",
- current_enabled ? "started" : "stopped");
- goto out_unlock;
- }
- if (enabled) { /* true */
- if (nt->release && !nt->extended) {
- pr_err("Not enabling netconsole. Release feature requires extended log message");
- 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->state = STATE_ENABLED;
- 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->state == STATE_ENABLED
- */
- mutex_lock(&target_cleanup_list_lock);
- spin_lock_irqsave(&target_list_lock, flags);
- nt->state = STATE_DISABLED;
- /* 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();
- }
- ret = strnlen(buf, count);
- /* Deferred cleanup */
- netconsole_process_cleanups();
- out_unlock:
- dynamic_netconsole_mutex_unlock();
- return ret;
- }
- static ssize_t release_store(struct config_item *item, const char *buf,
- size_t count)
- {
- struct netconsole_target *nt = to_target(item);
- bool release;
- ssize_t ret;
- dynamic_netconsole_mutex_lock();
- if (nt->state == STATE_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:
- dynamic_netconsole_mutex_unlock();
- return ret;
- }
- static ssize_t extended_store(struct config_item *item, const char *buf,
- size_t count)
- {
- struct netconsole_target *nt = to_target(item);
- bool extended;
- ssize_t ret;
- dynamic_netconsole_mutex_lock();
- if (nt->state == STATE_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;
- nt->extended = extended;
- ret = strnlen(buf, count);
- out_unlock:
- dynamic_netconsole_mutex_unlock();
- return ret;
- }
- static ssize_t dev_name_store(struct config_item *item, const char *buf,
- size_t count)
- {
- struct netconsole_target *nt = to_target(item);
- dynamic_netconsole_mutex_lock();
- if (nt->state == STATE_ENABLED) {
- pr_err("target (%s) is enabled, disable to update parameters\n",
- config_item_name(&nt->group.cg_item));
- dynamic_netconsole_mutex_unlock();
- return -EINVAL;
- }
- strscpy(nt->np.dev_name, buf, IFNAMSIZ);
- trim_newline(nt->np.dev_name, IFNAMSIZ);
- dynamic_netconsole_mutex_unlock();
- return strnlen(buf, count);
- }
- static ssize_t local_port_store(struct config_item *item, const char *buf,
- size_t count)
- {
- struct netconsole_target *nt = to_target(item);
- ssize_t ret = -EINVAL;
- dynamic_netconsole_mutex_lock();
- if (nt->state == STATE_ENABLED) {
- pr_err("target (%s) is enabled, disable to update parameters\n",
- config_item_name(&nt->group.cg_item));
- goto out_unlock;
- }
- ret = kstrtou16(buf, 10, &nt->np.local_port);
- if (ret < 0)
- goto out_unlock;
- ret = strnlen(buf, count);
- out_unlock:
- dynamic_netconsole_mutex_unlock();
- return ret;
- }
- static ssize_t remote_port_store(struct config_item *item,
- const char *buf, size_t count)
- {
- struct netconsole_target *nt = to_target(item);
- ssize_t ret = -EINVAL;
- dynamic_netconsole_mutex_lock();
- if (nt->state == STATE_ENABLED) {
- pr_err("target (%s) is enabled, disable to update parameters\n",
- config_item_name(&nt->group.cg_item));
- goto out_unlock;
- }
- ret = kstrtou16(buf, 10, &nt->np.remote_port);
- if (ret < 0)
- goto out_unlock;
- ret = strnlen(buf, count);
- out_unlock:
- dynamic_netconsole_mutex_unlock();
- return ret;
- }
- static ssize_t local_ip_store(struct config_item *item, const char *buf,
- size_t count)
- {
- struct netconsole_target *nt = to_target(item);
- ssize_t ret = -EINVAL;
- int ipv6;
- dynamic_netconsole_mutex_lock();
- if (nt->state == STATE_ENABLED) {
- pr_err("target (%s) is enabled, disable to update parameters\n",
- config_item_name(&nt->group.cg_item));
- goto out_unlock;
- }
- ipv6 = netpoll_parse_ip_addr(buf, &nt->np.local_ip);
- if (ipv6 == -1)
- goto out_unlock;
- nt->np.ipv6 = !!ipv6;
- ret = strnlen(buf, count);
- out_unlock:
- dynamic_netconsole_mutex_unlock();
- return ret;
- }
- static ssize_t remote_ip_store(struct config_item *item, const char *buf,
- size_t count)
- {
- struct netconsole_target *nt = to_target(item);
- ssize_t ret = -EINVAL;
- int ipv6;
- dynamic_netconsole_mutex_lock();
- if (nt->state == STATE_ENABLED) {
- pr_err("target (%s) is enabled, disable to update parameters\n",
- config_item_name(&nt->group.cg_item));
- goto out_unlock;
- }
- ipv6 = netpoll_parse_ip_addr(buf, &nt->np.remote_ip);
- if (ipv6 == -1)
- goto out_unlock;
- nt->np.ipv6 = !!ipv6;
- ret = strnlen(buf, count);
- out_unlock:
- dynamic_netconsole_mutex_unlock();
- return ret;
- }
- /* Count number of entries we have in userdata.
- * This is important because userdata only supports MAX_USERDATA_ITEMS
- * entries. Before enabling any new userdata feature, number of entries needs
- * to checked for available space.
- */
- static size_t count_userdata_entries(struct netconsole_target *nt)
- {
- return list_count_nodes(&nt->userdata_group.cg_children);
- }
- static ssize_t remote_mac_store(struct config_item *item, const char *buf,
- size_t count)
- {
- struct netconsole_target *nt = to_target(item);
- u8 remote_mac[ETH_ALEN];
- ssize_t ret = -EINVAL;
- dynamic_netconsole_mutex_lock();
- if (nt->state == STATE_ENABLED) {
- pr_err("target (%s) is enabled, disable to update parameters\n",
- config_item_name(&nt->group.cg_item));
- goto out_unlock;
- }
- if (!mac_pton(buf, remote_mac))
- goto out_unlock;
- if (buf[MAC_ADDR_STR_LEN] && buf[MAC_ADDR_STR_LEN] != '\n')
- goto out_unlock;
- memcpy(nt->np.remote_mac, remote_mac, ETH_ALEN);
- ret = strnlen(buf, count);
- out_unlock:
- dynamic_netconsole_mutex_unlock();
- return ret;
- }
- struct userdatum {
- struct config_item item;
- char value[MAX_EXTRADATA_VALUE_LEN];
- };
- static struct userdatum *to_userdatum(struct config_item *item)
- {
- return container_of(item, struct userdatum, item);
- }
- struct userdata {
- struct config_group group;
- };
- static struct userdata *to_userdata(struct config_item *item)
- {
- return container_of(to_config_group(item), struct userdata, group);
- }
- static struct netconsole_target *userdata_to_target(struct userdata *ud)
- {
- struct config_group *netconsole_group;
- netconsole_group = to_config_group(ud->group.cg_item.ci_parent);
- return to_target(&netconsole_group->cg_item);
- }
- static ssize_t userdatum_value_show(struct config_item *item, char *buf)
- {
- return sysfs_emit(buf, "%s\n", &(to_userdatum(item)->value[0]));
- }
- /* Navigate configfs and calculate the lentgh of the formatted string
- * representing userdata.
- * Must be called holding netconsole_subsys.su_mutex
- */
- static int calc_userdata_len(struct netconsole_target *nt)
- {
- struct userdatum *udm_item;
- struct config_item *item;
- struct list_head *entry;
- int len = 0;
- list_for_each(entry, &nt->userdata_group.cg_children) {
- item = container_of(entry, struct config_item, ci_entry);
- udm_item = to_userdatum(item);
- /* Skip userdata with no value set */
- if (udm_item->value[0]) {
- len += snprintf(NULL, 0, " %s=%s\n", item->ci_name,
- udm_item->value);
- }
- }
- return len;
- }
- static int update_userdata(struct netconsole_target *nt)
- {
- struct userdatum *udm_item;
- struct config_item *item;
- struct list_head *entry;
- char *old_buf = NULL;
- char *new_buf = NULL;
- unsigned long flags;
- int offset = 0;
- int len;
- /* Calculate required buffer size */
- len = calc_userdata_len(nt);
- if (WARN_ON_ONCE(len > MAX_EXTRADATA_ENTRY_LEN * MAX_USERDATA_ITEMS))
- return -ENOSPC;
- /* Allocate new buffer */
- if (len) {
- new_buf = kmalloc(len + 1, GFP_KERNEL);
- if (!new_buf)
- return -ENOMEM;
- }
- /* Write userdata to new buffer */
- list_for_each(entry, &nt->userdata_group.cg_children) {
- item = container_of(entry, struct config_item, ci_entry);
- udm_item = to_userdatum(item);
- /* Skip userdata with no value set */
- if (udm_item->value[0]) {
- offset += scnprintf(&new_buf[offset], len + 1 - offset,
- " %s=%s\n", item->ci_name,
- udm_item->value);
- }
- }
- WARN_ON_ONCE(offset != len);
- /* Switch to new buffer and free old buffer */
- spin_lock_irqsave(&target_list_lock, flags);
- old_buf = nt->userdata;
- nt->userdata = new_buf;
- nt->userdata_length = offset;
- spin_unlock_irqrestore(&target_list_lock, flags);
- kfree(old_buf);
- return 0;
- }
- static ssize_t userdatum_value_store(struct config_item *item, const char *buf,
- size_t count)
- {
- struct userdatum *udm = to_userdatum(item);
- struct netconsole_target *nt;
- struct userdata *ud;
- ssize_t ret;
- if (count > MAX_EXTRADATA_VALUE_LEN)
- return -EMSGSIZE;
- mutex_lock(&netconsole_subsys.su_mutex);
- dynamic_netconsole_mutex_lock();
- ret = strscpy(udm->value, buf, sizeof(udm->value));
- if (ret < 0)
- goto out_unlock;
- trim_newline(udm->value, sizeof(udm->value));
- ud = to_userdata(item->ci_parent);
- nt = userdata_to_target(ud);
- ret = update_userdata(nt);
- if (ret < 0)
- goto out_unlock;
- ret = count;
- out_unlock:
- dynamic_netconsole_mutex_unlock();
- mutex_unlock(&netconsole_subsys.su_mutex);
- return ret;
- }
- /* disable_sysdata_feature - Disable sysdata feature and clean sysdata
- * @nt: target that is disabling the feature
- * @feature: feature being disabled
- */
- static void disable_sysdata_feature(struct netconsole_target *nt,
- enum sysdata_feature feature)
- {
- nt->sysdata_fields &= ~feature;
- nt->sysdata[0] = 0;
- }
- static ssize_t sysdata_msgid_enabled_store(struct config_item *item,
- const char *buf, size_t count)
- {
- struct netconsole_target *nt = to_target(item->ci_parent);
- bool msgid_enabled, curr;
- ssize_t ret;
- ret = kstrtobool(buf, &msgid_enabled);
- if (ret)
- return ret;
- mutex_lock(&netconsole_subsys.su_mutex);
- dynamic_netconsole_mutex_lock();
- curr = !!(nt->sysdata_fields & SYSDATA_MSGID);
- if (msgid_enabled == curr)
- goto unlock_ok;
- if (msgid_enabled)
- nt->sysdata_fields |= SYSDATA_MSGID;
- else
- disable_sysdata_feature(nt, SYSDATA_MSGID);
- unlock_ok:
- ret = strnlen(buf, count);
- dynamic_netconsole_mutex_unlock();
- mutex_unlock(&netconsole_subsys.su_mutex);
- return ret;
- }
- static ssize_t sysdata_release_enabled_store(struct config_item *item,
- const char *buf, size_t count)
- {
- struct netconsole_target *nt = to_target(item->ci_parent);
- bool release_enabled, curr;
- ssize_t ret;
- ret = kstrtobool(buf, &release_enabled);
- if (ret)
- return ret;
- mutex_lock(&netconsole_subsys.su_mutex);
- dynamic_netconsole_mutex_lock();
- curr = !!(nt->sysdata_fields & SYSDATA_RELEASE);
- if (release_enabled == curr)
- goto unlock_ok;
- if (release_enabled)
- nt->sysdata_fields |= SYSDATA_RELEASE;
- else
- disable_sysdata_feature(nt, SYSDATA_RELEASE);
- unlock_ok:
- ret = strnlen(buf, count);
- dynamic_netconsole_mutex_unlock();
- mutex_unlock(&netconsole_subsys.su_mutex);
- return ret;
- }
- static ssize_t sysdata_taskname_enabled_store(struct config_item *item,
- const char *buf, size_t count)
- {
- struct netconsole_target *nt = to_target(item->ci_parent);
- bool taskname_enabled, curr;
- ssize_t ret;
- ret = kstrtobool(buf, &taskname_enabled);
- if (ret)
- return ret;
- mutex_lock(&netconsole_subsys.su_mutex);
- dynamic_netconsole_mutex_lock();
- curr = !!(nt->sysdata_fields & SYSDATA_TASKNAME);
- if (taskname_enabled == curr)
- goto unlock_ok;
- if (taskname_enabled)
- nt->sysdata_fields |= SYSDATA_TASKNAME;
- else
- disable_sysdata_feature(nt, SYSDATA_TASKNAME);
- unlock_ok:
- ret = strnlen(buf, count);
- dynamic_netconsole_mutex_unlock();
- mutex_unlock(&netconsole_subsys.su_mutex);
- return ret;
- }
- /* configfs helper to sysdata cpu_nr feature */
- static ssize_t sysdata_cpu_nr_enabled_store(struct config_item *item,
- const char *buf, size_t count)
- {
- struct netconsole_target *nt = to_target(item->ci_parent);
- bool cpu_nr_enabled, curr;
- ssize_t ret;
- ret = kstrtobool(buf, &cpu_nr_enabled);
- if (ret)
- return ret;
- mutex_lock(&netconsole_subsys.su_mutex);
- dynamic_netconsole_mutex_lock();
- curr = !!(nt->sysdata_fields & SYSDATA_CPU_NR);
- if (cpu_nr_enabled == curr)
- /* no change requested */
- goto unlock_ok;
- if (cpu_nr_enabled)
- nt->sysdata_fields |= SYSDATA_CPU_NR;
- else
- /* This is special because sysdata might have remaining data
- * from previous sysdata, and it needs to be cleaned.
- */
- disable_sysdata_feature(nt, SYSDATA_CPU_NR);
- unlock_ok:
- ret = strnlen(buf, count);
- dynamic_netconsole_mutex_unlock();
- mutex_unlock(&netconsole_subsys.su_mutex);
- return ret;
- }
- CONFIGFS_ATTR(userdatum_, value);
- CONFIGFS_ATTR(sysdata_, cpu_nr_enabled);
- CONFIGFS_ATTR(sysdata_, taskname_enabled);
- CONFIGFS_ATTR(sysdata_, release_enabled);
- CONFIGFS_ATTR(sysdata_, msgid_enabled);
- static struct configfs_attribute *userdatum_attrs[] = {
- &userdatum_attr_value,
- NULL,
- };
- static void userdatum_release(struct config_item *item)
- {
- kfree(to_userdatum(item));
- }
- static struct configfs_item_operations userdatum_ops = {
- .release = userdatum_release,
- };
- static const struct config_item_type userdatum_type = {
- .ct_item_ops = &userdatum_ops,
- .ct_attrs = userdatum_attrs,
- .ct_owner = THIS_MODULE,
- };
- static struct config_item *userdatum_make_item(struct config_group *group,
- const char *name)
- {
- struct netconsole_target *nt;
- struct userdatum *udm;
- struct userdata *ud;
- if (strlen(name) > MAX_EXTRADATA_NAME_LEN)
- return ERR_PTR(-ENAMETOOLONG);
- ud = to_userdata(&group->cg_item);
- nt = userdata_to_target(ud);
- if (count_userdata_entries(nt) >= MAX_USERDATA_ITEMS)
- return ERR_PTR(-ENOSPC);
- udm = kzalloc_obj(*udm);
- if (!udm)
- return ERR_PTR(-ENOMEM);
- config_item_init_type_name(&udm->item, name, &userdatum_type);
- return &udm->item;
- }
- static void userdatum_drop(struct config_group *group, struct config_item *item)
- {
- struct netconsole_target *nt;
- struct userdata *ud;
- ud = to_userdata(&group->cg_item);
- nt = userdata_to_target(ud);
- dynamic_netconsole_mutex_lock();
- update_userdata(nt);
- config_item_put(item);
- dynamic_netconsole_mutex_unlock();
- }
- static struct configfs_attribute *userdata_attrs[] = {
- &sysdata_attr_cpu_nr_enabled,
- &sysdata_attr_taskname_enabled,
- &sysdata_attr_release_enabled,
- &sysdata_attr_msgid_enabled,
- NULL,
- };
- static struct configfs_group_operations userdata_ops = {
- .make_item = userdatum_make_item,
- .drop_item = userdatum_drop,
- };
- static const struct config_item_type userdata_type = {
- .ct_item_ops = &userdatum_ops,
- .ct_group_ops = &userdata_ops,
- .ct_attrs = userdata_attrs,
- .ct_owner = THIS_MODULE,
- };
- CONFIGFS_ATTR(, enabled);
- CONFIGFS_ATTR(, extended);
- CONFIGFS_ATTR(, dev_name);
- CONFIGFS_ATTR(, local_port);
- CONFIGFS_ATTR(, remote_port);
- CONFIGFS_ATTR(, local_ip);
- CONFIGFS_ATTR(, remote_ip);
- CONFIGFS_ATTR_RO(, local_mac);
- CONFIGFS_ATTR(, remote_mac);
- CONFIGFS_ATTR(, release);
- CONFIGFS_ATTR_RO(, transmit_errors);
- static struct configfs_attribute *netconsole_target_attrs[] = {
- &attr_enabled,
- &attr_extended,
- &attr_release,
- &attr_dev_name,
- &attr_local_port,
- &attr_remote_port,
- &attr_local_ip,
- &attr_remote_ip,
- &attr_local_mac,
- &attr_remote_mac,
- &attr_transmit_errors,
- NULL,
- };
- /*
- * Item operations and type for netconsole_target.
- */
- static void netconsole_target_release(struct config_item *item)
- {
- struct netconsole_target *nt = to_target(item);
- kfree(nt->userdata);
- kfree(nt);
- }
- static struct configfs_item_operations netconsole_target_item_ops = {
- .release = netconsole_target_release,
- };
- static const struct config_item_type netconsole_target_type = {
- .ct_attrs = netconsole_target_attrs,
- .ct_item_ops = &netconsole_target_item_ops,
- .ct_owner = THIS_MODULE,
- };
- static void init_target_config_group(struct netconsole_target *nt,
- const char *name)
- {
- config_group_init_type_name(&nt->group, name, &netconsole_target_type);
- config_group_init_type_name(&nt->userdata_group, "userdata",
- &userdata_type);
- configfs_add_default_group(&nt->userdata_group, &nt->group);
- }
- static struct netconsole_target *find_cmdline_target(const char *name)
- {
- struct netconsole_target *nt, *ret = NULL;
- unsigned long flags;
- spin_lock_irqsave(&target_list_lock, flags);
- list_for_each_entry(nt, &target_list, list) {
- if (!strcmp(nt->group.cg_item.ci_name, name)) {
- ret = nt;
- break;
- }
- }
- spin_unlock_irqrestore(&target_list_lock, flags);
- return ret;
- }
- /*
- * Group operations and type for netconsole_subsys.
- */
- static struct config_group *make_netconsole_target(struct config_group *group,
- const char *name)
- {
- struct netconsole_target *nt;
- unsigned long flags;
- /* 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);
- return &nt->group;
- }
- static void drop_netconsole_target(struct config_group *group,
- struct config_item *item)
- {
- struct netconsole_target *nt = to_target(item);
- unsigned long flags;
- dynamic_netconsole_mutex_lock();
- spin_lock_irqsave(&target_list_lock, flags);
- /* Disable deactivated target to prevent races between resume attempt
- * and target removal.
- */
- if (nt->state == STATE_DEACTIVATED)
- nt->state = STATE_DISABLED;
- list_del(&nt->list);
- spin_unlock_irqrestore(&target_list_lock, flags);
- dynamic_netconsole_mutex_unlock();
- /* Now that the target has been marked disabled no further work
- * can be scheduled. Existing work will skip as targets are not
- * deactivated anymore. Cancel any scheduled resume and wait for
- * completion.
- */
- cancel_work_sync(&nt->resume_wq);
- /*
- * The target may have never been enabled, or was manually disabled
- * before being removed so netpoll may have already been cleaned up.
- */
- if (nt->state == STATE_ENABLED)
- netpoll_cleanup(&nt->np);
- config_item_put(&nt->group.cg_item);
- }
- static struct configfs_group_operations netconsole_subsys_group_ops = {
- .make_group = make_netconsole_target,
- .drop_item = drop_netconsole_target,
- };
- static const struct config_item_type netconsole_subsys_type = {
- .ct_group_ops = &netconsole_subsys_group_ops,
- .ct_owner = THIS_MODULE,
- };
- /* The netconsole configfs subsystem */
- static struct configfs_subsystem netconsole_subsys = {
- .su_group = {
- .cg_item = {
- .ci_namebuf = "netconsole",
- .ci_type = &netconsole_subsys_type,
- },
- },
- };
- static void populate_configfs_item(struct netconsole_target *nt,
- int cmdline_count)
- {
- char target_name[16];
- snprintf(target_name, sizeof(target_name), "%s%d",
- NETCONSOLE_PARAM_TARGET_PREFIX, cmdline_count);
- init_target_config_group(nt, target_name);
- }
- static int sysdata_append_cpu_nr(struct netconsole_target *nt, int offset,
- struct nbcon_write_context *wctxt)
- {
- return scnprintf(&nt->sysdata[offset],
- MAX_EXTRADATA_ENTRY_LEN, " cpu=%u\n",
- wctxt->cpu);
- }
- static int sysdata_append_taskname(struct netconsole_target *nt, int offset,
- struct nbcon_write_context *wctxt)
- {
- return scnprintf(&nt->sysdata[offset],
- MAX_EXTRADATA_ENTRY_LEN, " taskname=%s\n",
- wctxt->comm);
- }
- static int sysdata_append_release(struct netconsole_target *nt, int offset)
- {
- return scnprintf(&nt->sysdata[offset],
- MAX_EXTRADATA_ENTRY_LEN, " release=%s\n",
- init_utsname()->release);
- }
- static int sysdata_append_msgid(struct netconsole_target *nt, int offset)
- {
- wrapping_assign_add(nt->msgcounter, 1);
- return scnprintf(&nt->sysdata[offset],
- MAX_EXTRADATA_ENTRY_LEN, " msgid=%u\n",
- nt->msgcounter);
- }
- /*
- * prepare_sysdata - append sysdata in runtime
- * @nt: target to send message to
- * @wctxt: nbcon write context containing message metadata
- */
- static int prepare_sysdata(struct netconsole_target *nt,
- struct nbcon_write_context *wctxt)
- {
- int sysdata_len = 0;
- if (!nt->sysdata_fields)
- goto out;
- if (nt->sysdata_fields & SYSDATA_CPU_NR)
- sysdata_len += sysdata_append_cpu_nr(nt, sysdata_len, wctxt);
- if (nt->sysdata_fields & SYSDATA_TASKNAME)
- sysdata_len += sysdata_append_taskname(nt, sysdata_len, wctxt);
- if (nt->sysdata_fields & SYSDATA_RELEASE)
- sysdata_len += sysdata_append_release(nt, sysdata_len);
- if (nt->sysdata_fields & SYSDATA_MSGID)
- sysdata_len += sysdata_append_msgid(nt, sysdata_len);
- WARN_ON_ONCE(sysdata_len >
- MAX_EXTRADATA_ENTRY_LEN * MAX_SYSDATA_ITEMS);
- out:
- return sysdata_len;
- }
- #endif /* CONFIG_NETCONSOLE_DYNAMIC */
- /* Handle network interface device notifications */
- static int netconsole_netdev_event(struct notifier_block *this,
- unsigned long event, void *ptr)
- {
- struct net_device *dev = netdev_notifier_info_to_dev(ptr);
- struct netconsole_target *nt, *tmp;
- bool stopped = false;
- unsigned long flags;
- if (!(event == NETDEV_CHANGENAME || event == NETDEV_UNREGISTER ||
- event == NETDEV_RELEASE || event == NETDEV_JOIN ||
- event == NETDEV_REGISTER))
- goto done;
- mutex_lock(&target_cleanup_list_lock);
- spin_lock_irqsave(&target_list_lock, flags);
- list_for_each_entry_safe(nt, tmp, &target_list, list) {
- netconsole_target_get(nt);
- if (nt->np.dev == dev) {
- switch (event) {
- case NETDEV_CHANGENAME:
- strscpy(nt->np.dev_name, dev->name, IFNAMSIZ);
- break;
- case NETDEV_RELEASE:
- case NETDEV_JOIN:
- /* transition target to DISABLED instead of
- * DEACTIVATED when (de)enslaving devices as
- * their targets should not be automatically
- * resumed when the interface is brought up.
- */
- nt->state = STATE_DISABLED;
- list_move(&nt->list, &target_cleanup_list);
- stopped = true;
- break;
- case NETDEV_UNREGISTER:
- nt->state = STATE_DEACTIVATED;
- list_move(&nt->list, &target_cleanup_list);
- stopped = true;
- }
- }
- if ((event == NETDEV_REGISTER || event == NETDEV_CHANGENAME) &&
- deactivated_target_match(nt, dev))
- /* Schedule resume on a workqueue as it will attempt
- * to UP the device, which can't be done as part of this
- * notifier.
- */
- queue_work(netconsole_wq, &nt->resume_wq);
- netconsole_target_put(nt);
- }
- spin_unlock_irqrestore(&target_list_lock, flags);
- mutex_unlock(&target_cleanup_list_lock);
- if (stopped) {
- const char *msg = "had an event";
- switch (event) {
- case NETDEV_UNREGISTER:
- msg = "unregistered";
- break;
- case NETDEV_RELEASE:
- msg = "released slaves";
- break;
- case NETDEV_JOIN:
- msg = "is joining a master device";
- break;
- }
- pr_info("network logging stopped on interface %s as it %s\n",
- dev->name, msg);
- }
- /* Process target_cleanup_list entries. By the end, target_cleanup_list
- * should be empty
- */
- netconsole_process_cleanups_core();
- done:
- return NOTIFY_DONE;
- }
- static struct notifier_block netconsole_netdev_notifier = {
- .notifier_call = netconsole_netdev_event,
- };
- /**
- * 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.
- */
- static void send_udp(struct netconsole_target *nt, const char *msg, int len)
- {
- int result = netpoll_send_udp(&nt->np, msg, len);
- if (IS_ENABLED(CONFIG_NETCONSOLE_DYNAMIC)) {
- if (result == NET_XMIT_DROP) {
- u64_stats_update_begin(&nt->stats.syncp);
- u64_stats_inc(&nt->stats.xmit_drop_count);
- u64_stats_update_end(&nt->stats.syncp);
- } else if (result == -ENOMEM) {
- u64_stats_update_begin(&nt->stats.syncp);
- u64_stats_inc(&nt->stats.enomem_count);
- u64_stats_update_end(&nt->stats.syncp);
- }
- }
- }
- static void send_msg_no_fragmentation(struct netconsole_target *nt,
- const char *msg,
- int msg_len,
- int release_len)
- {
- const char *userdata = NULL;
- const char *sysdata = NULL;
- const char *release;
- #ifdef CONFIG_NETCONSOLE_DYNAMIC
- userdata = nt->userdata;
- sysdata = nt->sysdata;
- #endif
- if (release_len) {
- release = init_utsname()->release;
- scnprintf(nt->buf, MAX_PRINT_CHUNK, "%s,%.*s", release,
- msg_len, msg);
- msg_len += release_len;
- } else {
- memcpy(nt->buf, msg, msg_len);
- }
- if (userdata)
- msg_len += scnprintf(&nt->buf[msg_len],
- MAX_PRINT_CHUNK - msg_len, "%s",
- userdata);
- if (sysdata)
- msg_len += scnprintf(&nt->buf[msg_len],
- MAX_PRINT_CHUNK - msg_len, "%s",
- sysdata);
- send_udp(nt, nt->buf, msg_len);
- }
- static void append_release(char *buf)
- {
- const char *release;
- release = init_utsname()->release;
- scnprintf(buf, MAX_PRINT_CHUNK, "%s,", release);
- }
- static void send_fragmented_body(struct netconsole_target *nt,
- const char *msgbody_ptr, int header_len,
- int msgbody_len, int sysdata_len)
- {
- const char *userdata_ptr = NULL;
- const char *sysdata_ptr = NULL;
- int data_len, data_sent = 0;
- int userdata_offset = 0;
- int sysdata_offset = 0;
- int msgbody_offset = 0;
- int userdata_len = 0;
- #ifdef CONFIG_NETCONSOLE_DYNAMIC
- userdata_ptr = nt->userdata;
- sysdata_ptr = nt->sysdata;
- userdata_len = nt->userdata_length;
- #endif
- if (WARN_ON_ONCE(!userdata_ptr && userdata_len != 0))
- return;
- if (WARN_ON_ONCE(!sysdata_ptr && sysdata_len != 0))
- return;
- /* data_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
- */
- data_len = msgbody_len + userdata_len + sysdata_len;
- /* In each iteration of the while loop below, we send a packet
- * containing the header and a portion of the data. The data is
- * composed of three parts: msgbody, userdata, and sysdata.
- * We keep track of how many bytes have been sent from each part using
- * the *_offset variables.
- * We keep track of how many bytes have been sent overall using the
- * data_sent variable, which ranges from 0 to the total bytes to be
- * sent.
- */
- while (data_sent < data_len) {
- int userdata_left = userdata_len - userdata_offset;
- int sysdata_left = sysdata_len - sysdata_offset;
- int msgbody_left = msgbody_len - msgbody_offset;
- int buf_offset = 0;
- int this_chunk = 0;
- /* header is already populated in nt->buf, just append to it */
- buf_offset = header_len;
- buf_offset += scnprintf(nt->buf + buf_offset,
- MAX_PRINT_CHUNK - buf_offset,
- ",ncfrag=%d/%d;", data_sent,
- data_len);
- /* append msgbody first */
- this_chunk = min(msgbody_left, MAX_PRINT_CHUNK - buf_offset);
- memcpy(nt->buf + buf_offset, msgbody_ptr + msgbody_offset,
- this_chunk);
- msgbody_offset += this_chunk;
- buf_offset += this_chunk;
- data_sent += this_chunk;
- /* after msgbody, append userdata */
- if (userdata_ptr && userdata_left) {
- this_chunk = min(userdata_left,
- MAX_PRINT_CHUNK - buf_offset);
- memcpy(nt->buf + buf_offset,
- userdata_ptr + userdata_offset, this_chunk);
- userdata_offset += this_chunk;
- buf_offset += this_chunk;
- data_sent += this_chunk;
- }
- /* after userdata, append sysdata */
- if (sysdata_ptr && sysdata_left) {
- this_chunk = min(sysdata_left,
- MAX_PRINT_CHUNK - buf_offset);
- memcpy(nt->buf + buf_offset,
- sysdata_ptr + sysdata_offset, this_chunk);
- sysdata_offset += this_chunk;
- buf_offset += this_chunk;
- data_sent += this_chunk;
- }
- /* if all is good, send the packet out */
- if (WARN_ON_ONCE(data_sent > data_len))
- return;
- send_udp(nt, nt->buf, buf_offset);
- }
- }
- static void send_msg_fragmented(struct netconsole_target *nt,
- const char *msg,
- int msg_len,
- int release_len,
- int sysdata_len)
- {
- int header_len, msgbody_len;
- const char *msgbody;
- /* need to insert extra header fields, detect header and msgbody */
- msgbody = memchr(msg, ';', msg_len);
- if (WARN_ON_ONCE(!msgbody))
- return;
- header_len = msgbody - msg;
- msgbody_len = msg_len - header_len - 1;
- msgbody++;
- /*
- * 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,
- sysdata_len);
- }
- /**
- * send_ext_msg_udp - send extended log message to target
- * @nt: target to send message to
- * @wctxt: nbcon write context containing message and metadata
- *
- * Transfer extended log message to @nt. If message is longer than
- * MAX_PRINT_CHUNK, it'll be split and transmitted in multiple chunks with
- * ncfrag header field added to identify them.
- */
- static void send_ext_msg_udp(struct netconsole_target *nt,
- struct nbcon_write_context *wctxt)
- {
- int userdata_len = 0;
- int release_len = 0;
- int sysdata_len = 0;
- int len;
- #ifdef CONFIG_NETCONSOLE_DYNAMIC
- sysdata_len = prepare_sysdata(nt, wctxt);
- userdata_len = nt->userdata_length;
- #endif
- if (nt->release)
- release_len = strlen(init_utsname()->release) + 1;
- len = wctxt->len + release_len + sysdata_len + userdata_len;
- if (len <= MAX_PRINT_CHUNK)
- return send_msg_no_fragmentation(nt, wctxt->outbuf,
- wctxt->len, release_len);
- return send_msg_fragmented(nt, wctxt->outbuf, wctxt->len, release_len,
- sysdata_len);
- }
- static void send_msg_udp(struct netconsole_target *nt, const char *msg,
- unsigned int len)
- {
- const char *tmp = msg;
- int frag, left = len;
- while (left > 0) {
- frag = min(left, MAX_PRINT_CHUNK);
- send_udp(nt, tmp, frag);
- tmp += frag;
- left -= frag;
- }
- }
- /**
- * netconsole_write - Generic function to send a msg to all targets
- * @wctxt: nbcon write context
- * @extended: "true" for extended console mode
- *
- * Given an nbcon write context, send the message to the netconsole targets
- */
- static void netconsole_write(struct nbcon_write_context *wctxt, bool extended)
- {
- struct netconsole_target *nt;
- if (oops_only && !oops_in_progress)
- return;
- list_for_each_entry(nt, &target_list, list) {
- if (nt->extended != extended || nt->state != STATE_ENABLED ||
- !netif_running(nt->np.dev))
- continue;
- /* If nbcon_enter_unsafe() fails, just return given netconsole
- * lost the ownership, and iterating over the targets will not
- * be able to re-acquire.
- */
- if (!nbcon_enter_unsafe(wctxt))
- return;
- if (extended)
- send_ext_msg_udp(nt, wctxt);
- else
- send_msg_udp(nt, wctxt->outbuf, wctxt->len);
- nbcon_exit_unsafe(wctxt);
- }
- }
- static void netconsole_write_ext(struct console *con __always_unused,
- struct nbcon_write_context *wctxt)
- {
- netconsole_write(wctxt, true);
- }
- static void netconsole_write_basic(struct console *con __always_unused,
- struct nbcon_write_context *wctxt)
- {
- netconsole_write(wctxt, false);
- }
- static void netconsole_device_lock(struct console *con __always_unused,
- unsigned long *flags)
- __acquires(&target_list_lock)
- {
- spin_lock_irqsave(&target_list_lock, *flags);
- }
- static void netconsole_device_unlock(struct console *con __always_unused,
- unsigned long flags)
- __releases(&target_list_lock)
- {
- spin_unlock_irqrestore(&target_list_lock, flags);
- }
- static int netconsole_parser_cmdline(struct netpoll *np, char *opt)
- {
- bool ipversion_set = false;
- char *cur = opt;
- char *delim;
- int ipv6;
- if (*cur != '@') {
- delim = strchr(cur, '@');
- if (!delim)
- goto parse_failed;
- *delim = 0;
- if (kstrtou16(cur, 10, &np->local_port))
- goto parse_failed;
- cur = delim;
- }
- cur++;
- if (*cur != '/') {
- ipversion_set = true;
- delim = strchr(cur, '/');
- if (!delim)
- goto parse_failed;
- *delim = 0;
- ipv6 = netpoll_parse_ip_addr(cur, &np->local_ip);
- if (ipv6 < 0)
- goto parse_failed;
- else
- np->ipv6 = (bool)ipv6;
- cur = delim;
- }
- cur++;
- if (*cur != ',') {
- /* parse out dev_name or dev_mac */
- delim = strchr(cur, ',');
- if (!delim)
- goto parse_failed;
- *delim = 0;
- np->dev_name[0] = '\0';
- eth_broadcast_addr(np->dev_mac);
- if (!strchr(cur, ':'))
- strscpy(np->dev_name, cur, sizeof(np->dev_name));
- else if (!mac_pton(cur, np->dev_mac))
- goto parse_failed;
- cur = delim;
- }
- cur++;
- if (*cur != '@') {
- /* dst port */
- delim = strchr(cur, '@');
- if (!delim)
- goto parse_failed;
- *delim = 0;
- if (*cur == ' ' || *cur == '\t')
- np_info(np, "warning: whitespace is not allowed\n");
- if (kstrtou16(cur, 10, &np->remote_port))
- goto parse_failed;
- cur = delim;
- }
- cur++;
- /* dst ip */
- delim = strchr(cur, '/');
- if (!delim)
- goto parse_failed;
- *delim = 0;
- ipv6 = netpoll_parse_ip_addr(cur, &np->remote_ip);
- if (ipv6 < 0)
- goto parse_failed;
- else if (ipversion_set && np->ipv6 != (bool)ipv6)
- goto parse_failed;
- else
- np->ipv6 = (bool)ipv6;
- cur = delim + 1;
- if (*cur != 0) {
- /* MAC address */
- if (!mac_pton(cur, np->remote_mac))
- goto parse_failed;
- }
- netconsole_print_banner(np);
- return 0;
- parse_failed:
- np_info(np, "couldn't parse config at '%s'!\n", cur);
- return -1;
- }
- /* Allocate new target (from boot/module param) and setup netpoll for it */
- static struct 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++;
- }
- if (*target_config == 'r') {
- if (!nt->extended) {
- pr_err("Netconsole configuration error. Release feature requires extended log message");
- err = -EINVAL;
- goto fail;
- }
- nt->release = 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->state = STATE_ENABLED;
- }
- 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 */
- static void free_param_target(struct netconsole_target *nt)
- {
- cancel_work_sync(&nt->resume_wq);
- netpoll_cleanup(&nt->np);
- #ifdef CONFIG_NETCONSOLE_DYNAMIC
- kfree(nt->userdata);
- #endif
- kfree(nt);
- }
- static struct console netconsole_ext = {
- .name = "netcon_ext",
- .flags = CON_ENABLED | CON_EXTENDED | CON_NBCON | CON_NBCON_ATOMIC_UNSAFE,
- .write_thread = netconsole_write_ext,
- .write_atomic = netconsole_write_ext,
- .device_lock = netconsole_device_lock,
- .device_unlock = netconsole_device_unlock,
- };
- static struct console netconsole = {
- .name = "netcon",
- .flags = CON_ENABLED | CON_NBCON | CON_NBCON_ATOMIC_UNSAFE,
- .write_thread = netconsole_write_basic,
- .write_atomic = netconsole_write_basic,
- .device_lock = netconsole_device_lock,
- .device_unlock = netconsole_device_unlock,
- };
- static int __init init_netconsole(void)
- {
- int err;
- struct netconsole_target *nt, *tmp;
- u32 console_type_needed = 0;
- unsigned int count = 0;
- unsigned long flags;
- char *target_config;
- char *input = config;
- if (strnlen(input, MAX_PARAM_LENGTH)) {
- while ((target_config = strsep(&input, ";"))) {
- nt = alloc_param_target(target_config, count);
- if (IS_ERR(nt)) {
- if (IS_ENABLED(CONFIG_NETCONSOLE_DYNAMIC))
- continue;
- err = PTR_ERR(nt);
- goto fail;
- }
- /* Dump existing printks when we register */
- if (nt->extended) {
- console_type_needed |= CONS_EXTENDED;
- netconsole_ext.flags |= CON_PRINTBUFFER;
- } else {
- console_type_needed |= CONS_BASIC;
- netconsole.flags |= CON_PRINTBUFFER;
- }
- spin_lock_irqsave(&target_list_lock, flags);
- list_add(&nt->list, &target_list);
- spin_unlock_irqrestore(&target_list_lock, flags);
- count++;
- }
- }
- netconsole_wq = alloc_workqueue("netconsole", WQ_UNBOUND, 0);
- if (!netconsole_wq) {
- err = -ENOMEM;
- goto fail;
- }
- err = register_netdevice_notifier(&netconsole_netdev_notifier);
- if (err)
- goto fail;
- err = dynamic_netconsole_init();
- if (err)
- goto undonotifier;
- if (console_type_needed & CONS_EXTENDED)
- register_console(&netconsole_ext);
- if (console_type_needed & CONS_BASIC)
- register_console(&netconsole);
- pr_info("network logging started\n");
- return err;
- undonotifier:
- unregister_netdevice_notifier(&netconsole_netdev_notifier);
- fail:
- pr_err("cleaning up\n");
- if (netconsole_wq)
- flush_workqueue(netconsole_wq);
- /*
- * 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 (netconsole_wq)
- destroy_workqueue(netconsole_wq);
- return err;
- }
- static void __exit cleanup_netconsole(void)
- {
- struct netconsole_target *nt, *tmp;
- 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);
- flush_workqueue(netconsole_wq);
- /*
- * 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);
- }
- destroy_workqueue(netconsole_wq);
- }
- /*
- * 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);
|