1/*
2 * Copyright (c) 2018-2022 Apple Inc. All rights reserved.
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
96 if (!IF_FULLY_ATTACHED(ifp)) {
97 return;
98 }
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
106 if (event_code == IF_LOW_POWER_EVENT_OFF) {
107 if_clear_xflags(ifp, IFXF_LOW_POWER);
108 } else {
109 if_set_xflags(ifp, IFXF_LOW_POWER);
110 }
111
112 if (event_code == IF_LOW_POWER_EVENT_ON) {
113 os_atomic_inc(&ifp->if_low_power_gencnt, relaxed);
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: INTF_EVENT_CODE_LOW_POWER_UPDATE);
119 }
120 }
121
122 bzero(s: &kev, n: 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), FALSE);
129}
130
131void
132if_low_power_evhdlr_init(void)
133{
134 eventhandler_lists_ctxt_init(evthdlr_lists_ctxt: &if_low_power_evhdlr_ctx);
135
136 (void)EVENTHANDLER_REGISTER(&if_low_power_evhdlr_ctx,
137 if_low_power_event,
138 if_low_power_evhdlr_callback,
139 eventhandler_entry_dummy_arg,
140 EVENTHANDLER_PRI_ANY);
141}
142
143#if 0
144static void
145if_low_power_nwk_ev_callback(struct nwk_wq_entry *nwk_item)
146{
147 struct if_low_power_ev_nwk_wq_entry *p_ev;
148
149 p_ev = __container_of(nwk_item,
150 struct if_low_power_ev_nwk_wq_entry, nwk_wqe);
151
152 EVENTHANDLER_INVOKE(&if_low_power_evhdlr_ctx,
153 if_low_power_event, p_ev->ev_args.ifp,
154 p_ev->ev_args.event_code);
155
156 kfree_type(struct if_low_power_ev_nwk_wq_entry, p_ev);
157}
158
159static void
160if_low_power_event_enqueue_nwk_wq_entry(struct ifnet *ifp,
161 if_low_power_ev_code_t event_code)
162{
163 struct if_low_power_ev_nwk_wq_entry *event_nwk_wq_entry = NULL;
164
165 event_nwk_wq_entry = kalloc_type(struct if_low_power_ev_nwk_wq_entry,
166 Z_WAITOK | Z_ZERO | Z_NOFAIL);
167
168 event_nwk_wq_entry->ev_args.ifp = ifp;
169 event_nwk_wq_entry->ev_args.event_code = event_code;
170
171 event_nwk_wq_entry->nwk_wqe.func = if_low_power_nwk_ev_callback;
172
173 nwk_wq_enqueue(&event_nwk_wq_entry->nwk_wqe);
174}
175#endif
176
177int
178if_set_low_power(ifnet_t ifp, bool on)
179{
180 int error = 0;
181
182 if (ifp == NULL) {
183 return EINVAL;
184 }
185
186 os_log(OS_LOG_DEFAULT,
187 "%s: ifp %s low_power mode %d", __func__, if_name(ifp), on);
188
189 if (on) {
190 if_set_xflags(ifp, IFXF_LOW_POWER);
191 } else {
192 if_clear_xflags(ifp, IFXF_LOW_POWER);
193 }
194 return error;
195}
196