Jesse Wilson | 8568db5 | 2011-06-28 19:06:31 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.server; |
| 18 | |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 19 | import android.net.NetworkStats; |
Jeff Sharkey | 62a2c8f | 2011-07-13 15:24:02 -0700 | [diff] [blame] | 20 | import android.os.SystemProperties; |
| 21 | import android.util.Log; |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 22 | import android.util.Slog; |
| 23 | |
Jesse Wilson | 8568db5 | 2011-06-28 19:06:31 -0700 | [diff] [blame] | 24 | import dalvik.system.SocketTagger; |
Alon Albert | eaef351 | 2011-07-19 11:16:09 +0300 | [diff] [blame] | 25 | import libcore.io.IoUtils; |
Jeff Sharkey | 62a2c8f | 2011-07-13 15:24:02 -0700 | [diff] [blame] | 26 | |
Jesse Wilson | 8568db5 | 2011-06-28 19:06:31 -0700 | [diff] [blame] | 27 | import java.io.FileDescriptor; |
Jesse Wilson | 8568db5 | 2011-06-28 19:06:31 -0700 | [diff] [blame] | 28 | import java.io.FileOutputStream; |
| 29 | import java.io.IOException; |
Jeff Sharkey | 62a2c8f | 2011-07-13 15:24:02 -0700 | [diff] [blame] | 30 | import java.math.BigInteger; |
Jesse Wilson | 8568db5 | 2011-06-28 19:06:31 -0700 | [diff] [blame] | 31 | import java.net.SocketException; |
| 32 | import java.nio.charset.Charsets; |
| 33 | |
| 34 | /** |
| 35 | * Assigns tags to sockets for traffic stats. |
| 36 | */ |
| 37 | public final class NetworkManagementSocketTagger extends SocketTagger { |
Jeff Sharkey | 62a2c8f | 2011-07-13 15:24:02 -0700 | [diff] [blame] | 38 | private static final String TAG = "NetworkManagementSocketTagger"; |
| 39 | private static final boolean LOGD = false; |
Jesse Wilson | 8568db5 | 2011-06-28 19:06:31 -0700 | [diff] [blame] | 40 | |
Jeff Sharkey | 62a2c8f | 2011-07-13 15:24:02 -0700 | [diff] [blame] | 41 | /** |
| 42 | * {@link SystemProperties} key that indicates if {@code qtaguid} bandwidth |
| 43 | * controls have been enabled. |
| 44 | */ |
| 45 | // TODO: remove when always enabled, or once socket tagging silently fails. |
| 46 | public static final String PROP_QTAGUID_ENABLED = "net.qtaguid_enabled"; |
Jesse Wilson | 8568db5 | 2011-06-28 19:06:31 -0700 | [diff] [blame] | 47 | |
| 48 | private static ThreadLocal<SocketTags> threadSocketTags = new ThreadLocal<SocketTags>() { |
Jeff Sharkey | 62a2c8f | 2011-07-13 15:24:02 -0700 | [diff] [blame] | 49 | @Override |
| 50 | protected SocketTags initialValue() { |
Jesse Wilson | 8568db5 | 2011-06-28 19:06:31 -0700 | [diff] [blame] | 51 | return new SocketTags(); |
| 52 | } |
| 53 | }; |
| 54 | |
| 55 | public static void install() { |
| 56 | SocketTagger.set(new NetworkManagementSocketTagger()); |
| 57 | } |
| 58 | |
| 59 | public static void setThreadSocketStatsTag(int tag) { |
| 60 | threadSocketTags.get().statsTag = tag; |
| 61 | } |
| 62 | |
Alon Albert | eaef351 | 2011-07-19 11:16:09 +0300 | [diff] [blame] | 63 | public static int getThreadSocketStatsTag() { |
| 64 | return threadSocketTags.get().statsTag; |
| 65 | } |
| 66 | |
Jesse Wilson | 8568db5 | 2011-06-28 19:06:31 -0700 | [diff] [blame] | 67 | public static void setThreadSocketStatsUid(int uid) { |
| 68 | threadSocketTags.get().statsUid = uid; |
| 69 | } |
| 70 | |
Jeff Sharkey | 62a2c8f | 2011-07-13 15:24:02 -0700 | [diff] [blame] | 71 | @Override |
| 72 | public void tag(FileDescriptor fd) throws SocketException { |
Jesse Wilson | 8568db5 | 2011-06-28 19:06:31 -0700 | [diff] [blame] | 73 | final SocketTags options = threadSocketTags.get(); |
Jeff Sharkey | 62a2c8f | 2011-07-13 15:24:02 -0700 | [diff] [blame] | 74 | if (LOGD) { |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame] | 75 | Log.d(TAG, "tagSocket(" + fd.getInt$() + ") with statsTag=0x" |
| 76 | + Integer.toHexString(options.statsTag) + ", statsUid=" + options.statsUid); |
Jesse Wilson | 8568db5 | 2011-06-28 19:06:31 -0700 | [diff] [blame] | 77 | } |
JP Abgrall | 98a4f7e | 2011-09-02 15:36:33 -0700 | [diff] [blame] | 78 | // TODO: skip tagging when options would be no-op |
| 79 | tagSocketFd(fd, options.statsTag, options.statsUid); |
Jesse Wilson | 8568db5 | 2011-06-28 19:06:31 -0700 | [diff] [blame] | 80 | } |
| 81 | |
JP Abgrall | 98a4f7e | 2011-09-02 15:36:33 -0700 | [diff] [blame] | 82 | private void tagSocketFd(FileDescriptor fd, int tag, int uid) { |
JP Abgrall | 98a4f7e | 2011-09-02 15:36:33 -0700 | [diff] [blame] | 83 | if (tag == -1 && uid == -1) return; |
Jesse Wilson | 8568db5 | 2011-06-28 19:06:31 -0700 | [diff] [blame] | 84 | |
Jeff Sharkey | 418d12d | 2011-12-13 15:38:03 -0800 | [diff] [blame] | 85 | if (SystemProperties.getBoolean(PROP_QTAGUID_ENABLED, false)) { |
| 86 | final int errno = native_tagSocketFd(fd, tag, uid); |
| 87 | if (errno < 0) { |
| 88 | Log.i(TAG, "tagSocketFd(" + fd.getInt$() + ", " |
| 89 | + tag + ", " + |
| 90 | + uid + ") failed with errno" + errno); |
| 91 | } |
Jesse Wilson | 8568db5 | 2011-06-28 19:06:31 -0700 | [diff] [blame] | 92 | } |
Jesse Wilson | 8568db5 | 2011-06-28 19:06:31 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Jeff Sharkey | 62a2c8f | 2011-07-13 15:24:02 -0700 | [diff] [blame] | 95 | @Override |
| 96 | public void untag(FileDescriptor fd) throws SocketException { |
| 97 | if (LOGD) { |
| 98 | Log.i(TAG, "untagSocket(" + fd.getInt$() + ")"); |
Jesse Wilson | 8568db5 | 2011-06-28 19:06:31 -0700 | [diff] [blame] | 99 | } |
JP Abgrall | 98a4f7e | 2011-09-02 15:36:33 -0700 | [diff] [blame] | 100 | unTagSocketFd(fd); |
Jesse Wilson | 8568db5 | 2011-06-28 19:06:31 -0700 | [diff] [blame] | 101 | } |
| 102 | |
JP Abgrall | 98a4f7e | 2011-09-02 15:36:33 -0700 | [diff] [blame] | 103 | private void unTagSocketFd(FileDescriptor fd) { |
JP Abgrall | 36bd984 | 2011-08-17 12:06:56 -0700 | [diff] [blame] | 104 | final SocketTags options = threadSocketTags.get(); |
JP Abgrall | 98a4f7e | 2011-09-02 15:36:33 -0700 | [diff] [blame] | 105 | if (options.statsTag == -1 && options.statsUid == -1) return; |
| 106 | |
Jeff Sharkey | 418d12d | 2011-12-13 15:38:03 -0800 | [diff] [blame] | 107 | if (SystemProperties.getBoolean(PROP_QTAGUID_ENABLED, false)) { |
| 108 | final int errno = native_untagSocketFd(fd); |
| 109 | if (errno < 0) { |
| 110 | Log.w(TAG, "untagSocket(" + fd.getInt$() + ") failed with errno " + errno); |
| 111 | } |
JP Abgrall | 98a4f7e | 2011-09-02 15:36:33 -0700 | [diff] [blame] | 112 | } |
Jesse Wilson | 8568db5 | 2011-06-28 19:06:31 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | public static class SocketTags { |
| 116 | public int statsTag = -1; |
| 117 | public int statsUid = -1; |
| 118 | } |
| 119 | |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 120 | public static void setKernelCounterSet(int uid, int counterSet) { |
Jeff Sharkey | 418d12d | 2011-12-13 15:38:03 -0800 | [diff] [blame] | 121 | if (SystemProperties.getBoolean(PROP_QTAGUID_ENABLED, false)) { |
| 122 | final int errno = native_setCounterSet(counterSet, uid); |
| 123 | if (errno < 0) { |
| 124 | Log.w(TAG, "setKernelCountSet(" + uid + ", " + counterSet + ") failed with errno " |
| 125 | + errno); |
| 126 | } |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 127 | } |
| 128 | } |
| 129 | |
| 130 | public static void resetKernelUidStats(int uid) { |
Jeff Sharkey | 418d12d | 2011-12-13 15:38:03 -0800 | [diff] [blame] | 131 | if (SystemProperties.getBoolean(PROP_QTAGUID_ENABLED, false)) { |
| 132 | int errno = native_deleteTagData(0, uid); |
| 133 | if (errno < 0) { |
| 134 | Slog.w(TAG, "problem clearing counters for uid " + uid + " : errno " + errno); |
| 135 | } |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | |
Jesse Wilson | 8568db5 | 2011-06-28 19:06:31 -0700 | [diff] [blame] | 139 | /** |
Jesse Wilson | 8568db5 | 2011-06-28 19:06:31 -0700 | [diff] [blame] | 140 | * Convert {@code /proc/} tag format to {@link Integer}. Assumes incoming |
| 141 | * format like {@code 0x7fffffff00000000}. |
| 142 | */ |
| 143 | public static int kernelToTag(String string) { |
| 144 | // TODO: migrate to direct integer instead of odd shifting |
| 145 | return (int) (Long.decode(string) >> 32); |
| 146 | } |
JP Abgrall | 98a4f7e | 2011-09-02 15:36:33 -0700 | [diff] [blame] | 147 | |
| 148 | private static native int native_tagSocketFd(FileDescriptor fd, int tag, int uid); |
| 149 | private static native int native_untagSocketFd(FileDescriptor fd); |
| 150 | private static native int native_setCounterSet(int uid, int counterSetNum); |
| 151 | private static native int native_deleteTagData(int tag, int uid); |
Jesse Wilson | 8568db5 | 2011-06-28 19:06:31 -0700 | [diff] [blame] | 152 | } |