]> git.saurik.com Git - apple/xnu.git/blame - bsd/net/if_low_power_mode.c
xnu-7195.81.3.tar.gz
[apple/xnu.git] / bsd / net / if_low_power_mode.c
CommitLineData
d9a64523 1/*
f427ee49 2 * Copyright (c) 2018-2020 Apple Inc. All rights reserved.
d9a64523
A
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29#include <sys/sysctl.h>
30#include <sys/systm.h>
31#include <sys/eventhandler.h>
32
33#include <net/dlil.h>
34#include <net/if.h>
35#include <net/if_var.h>
36#include <net/nwk_wq.h>
37
38#include <os/log.h>
39
40typedef enum {
41 IF_LOW_POWER_EVENT_OFF = 0,
42 IF_LOW_POWER_EVENT_ON = 1
43} if_low_power_ev_code_t;
44
45struct if_low_power_ev_args {
46 struct ifnet *ifp;
47 if_low_power_ev_code_t event_code;
48};
49
50struct if_low_power_ev_nwk_wq_entry {
51 struct nwk_wq_entry nwk_wqe;
52 struct if_low_power_ev_args ev_args;
53};
54
55
56typedef void (*if_low_power_event_fn) (struct eventhandler_entry_arg,
57 struct ifnet *, if_low_power_ev_code_t);
58EVENTHANDLER_DECLARE(if_low_power_event, if_low_power_event_fn);
59
60struct eventhandler_lists_ctxt if_low_power_evhdlr_ctx;
61
62static void if_low_power_evhdlr_callback(__unused struct eventhandler_entry_arg arg,
63 struct ifnet *ifp, if_low_power_ev_code_t event_code);
64
65#if 0
66static void if_low_power_nwk_ev_callback(void *arg);
67static void if_low_power_event_enqueue_nwk_wq_entry(struct ifnet *ifp,
68 if_low_power_ev_code_t event_code);
69#endif
70
71extern void shutdown_sockets_on_interface(struct ifnet *ifp);
72
73SYSCTL_DECL(_net_link_generic_system);
74SYSCTL_NODE(_net_link_generic_system, OID_AUTO, low_power,
75 CTLFLAG_RW | CTLFLAG_LOCKED, 0, "low power mode");
76
77int if_low_power_verbose = 0;
78int if_low_power_restricted = 1;
79
80#if (DEVELOPMENT || DEBUG)
81SYSCTL_INT(_net_link_generic_system_low_power, OID_AUTO, verbose,
82 CTLFLAG_RW | CTLFLAG_LOCKED,
83 &if_low_power_verbose, 0, "");
84SYSCTL_INT(_net_link_generic_system_low_power, OID_AUTO, restricted,
85 CTLFLAG_RW | CTLFLAG_LOCKED,
86 &if_low_power_restricted, 0, "");
87#endif /* (DEVELOPMENT || DEBUG) */
88
89
90static void
91if_low_power_evhdlr_callback(__unused struct eventhandler_entry_arg arg,
92 struct ifnet *ifp, if_low_power_ev_code_t event_code)
93{
94 struct kev_dl_low_power_mode kev;
95
0a7de745 96 if (!IF_FULLY_ATTACHED(ifp)) {
d9a64523 97 return;
0a7de745 98 }
d9a64523
A
99
100 if (if_low_power_verbose > 0) {
101 os_log_info(OS_LOG_DEFAULT,
102 "%s: ifp %s event_code %d", __func__,
103 if_name(ifp), event_code);
104 }
105
d9a64523 106 if (event_code == IF_LOW_POWER_EVENT_OFF) {
f427ee49 107 if_clear_xflags(ifp, IFXF_LOW_POWER);
d9a64523 108 } else {
f427ee49 109 if_set_xflags(ifp, IFXF_LOW_POWER);
d9a64523 110 }
d9a64523
A
111
112 if (event_code == IF_LOW_POWER_EVENT_ON) {
113 atomic_add_32(&ifp->if_low_power_gencnt, 1);
114
115 if (if_low_power_restricted != 0) {
116 shutdown_sockets_on_interface(ifp);
117 intf_event_enqueue_nwk_wq_entry(ifp, NULL,
118 INTF_EVENT_CODE_LOW_POWER_UPDATE);
119 }
120 }
121
122 bzero(&kev, sizeof(struct kev_dl_low_power_mode));
123 kev.low_power_event = event_code;
124 dlil_post_msg(ifp,
125 KEV_DL_SUBCLASS,
126 KEV_DL_LOW_POWER_MODE_CHANGED,
127 (struct net_event_data *)&kev,
128 sizeof(struct kev_dl_low_power_mode));
129}
130
131void
132if_low_power_evhdlr_init(void)
133{
134 eventhandler_lists_ctxt_init(&if_low_power_evhdlr_ctx);
135
cb323159 136 (void)EVENTHANDLER_REGISTER(&if_low_power_evhdlr_ctx,
d9a64523 137 if_low_power_event,
0a7de745 138 if_low_power_evhdlr_callback,
d9a64523
A
139 eventhandler_entry_dummy_arg,
140 EVENTHANDLER_PRI_ANY);
141}
142
143#if 0
144static void
145if_low_power_nwk_ev_callback(void *arg)
146{
147 struct if_low_power_ev_args *if_low_power_ev_args =
148 (struct if_low_power_ev_args *)arg;
0a7de745 149
d9a64523
A
150 EVENTHANDLER_INVOKE(&if_low_power_evhdlr_ctx,
151 if_low_power_event,
152 if_low_power_ev_args->ifp,
153 if_low_power_ev_args->event_code);
154}
155
156static void
157if_low_power_event_enqueue_nwk_wq_entry(struct ifnet *ifp,
158 if_low_power_ev_code_t event_code)
159{
160 struct if_low_power_ev_nwk_wq_entry *event_nwk_wq_entry = NULL;
161
162 MALLOC(event_nwk_wq_entry, struct if_low_power_ev_nwk_wq_entry *,
163 sizeof(struct if_low_power_ev_nwk_wq_entry),
164 M_NWKWQ, M_WAITOK | M_ZERO);
165
166 event_nwk_wq_entry->ev_args.ifp = ifp;
167 event_nwk_wq_entry->ev_args.event_code = event_code;
168
169 event_nwk_wq_entry->nwk_wqe.func = if_low_power_nwk_ev_callback;
170 event_nwk_wq_entry->nwk_wqe.is_arg_managed = TRUE;
171 event_nwk_wq_entry->nwk_wqe.arg = &event_nwk_wq_entry->ev_args;
172
173 nwk_wq_enqueue((struct nwk_wq_entry*)event_nwk_wq_entry);
174}
175#endif
176
177int
178if_set_low_power(ifnet_t ifp, bool on)
179{
180 int error = 0;
181
0a7de745
A
182 if (ifp == NULL) {
183 return EINVAL;
184 }
d9a64523
A
185
186 os_log(OS_LOG_DEFAULT,
187 "%s: ifp %s low_power mode %d", __func__, if_name(ifp), on);
188
f427ee49
A
189 if (on) {
190 if_set_xflags(ifp, IFXF_LOW_POWER);
191 } else {
192 if_clear_xflags(ifp, IFXF_LOW_POWER);
193 }
0a7de745 194 return error;
d9a64523 195}