勉強日記

チラ裏

ジェネリクスと変位について覚え書き -- なぜimmutableに書くのか

「setterをむやみに生やさずにimmutableに書こう」

という理由のひとつとして、「共変にできる」というのもあるんだなぁと思ったのでメモ。


よく聞く「T[]は不変(invariant)だよ」というやつ

  • 電車で考えてみる
    • Male extends Human, Female extends Human
    • Train<Human> ... 一般車両
    • Train<Female> ... 女性専用車両
  • Train<Female> extends Train<Human>と言えるか?
  • 一般には言えない
  • 反例: train.push(aMale)
  • Train<Female>push<Human>(aHuman: Human)を実装できないのでTrain<Human>としては使えない
  • is-a関係が崩れるので、とくに継承関係のない無関係な型になる(invariant)

必ずinvariantかというと、そうではない

  • Train<T>::at<T>(index:int): T これは問題ない
    • Train<Female>から取り出したFemaleはもれなくHumanとして使える
    • Train<Female>::at<Female>(index:int): FemaleTrain<Human>::at<Human>(index:int): Human として使えるということ
    • 「共変戻り値」という
  • Train<T>::push<T>(t: T): T これが駄目
    • 引数の場合、型を拡大できても、狭めることはできない
    • 「反変引数」という
    • 電車の例でいうと、女性が通常車両に乗るぶんには問題ない
  • 「共変かつ反変」というのが無理なので不変になるよ、という話
  • 反変なメソッドさえ生えていなければ、共変になりうる

共変なジェネリクスの例: ScalaのimmutableのList[+A]

  • Scalaだと型パラメータを[]で書くらしい
    • +が共変、-が反変を表す

www.scala-lang.org

def head: A
    Selects the first element of this iterable collection.
  • 要素取得。共変戻り値
final def map[B](f: (A) => B): List[B]
          Builds a new list by applying a function to all elements of this list.
  • コールバック関数fの型が(A) => B
  • 「反変引数」なので、引数型を広げるぶんには問題ない。(Female) => Bとして(Human) => Bを使うことができる
  • つまり、List[Female]List[Human]の機能を満たす。まだ共変
def appended[B >: A](elem: B): List[B]
    A copy of this sequence with an element appended.
  • リストに変更を加えるのではなく、新しいリストを返す
  • 引数型にも戻り値型にもList[A]の型パラメータAが出てこないので、変位にはとくに影響しない。共変を崩さない

そんなこんなでScalaのimmutableのList[A]は共変らしい


追記

PHPでは、PsalmとPHPDocコメントを使って共変の変位指定ができるらしい

CCNA試験対策 下巻ch12: Miscellaneous IP Services

https://www.ciscopress.com/store/ccna-200-301-official-cert-guide-volume-2-9781587147135www.ciscopress.com


First Hop Redundancy Protocol

The Need for Redundancy in Networks

The Need for a First Hop Redundancy Protocol

The Three Solutions for First-Hop Redundancy

HSRP VRRP GLBP
Full Name Hot Standby Router Protocol Virtual Router Redundancy Protocol Gateway Load Balancing Protocol
Origin Cisco RFC 6798 Cisco
Redundancy Approach active/standby active/standby active/active
Load Balancing per... subnet subnet host
仮想MACアドレス 0000.0c07.acxx
xxはHSRPのグループ番号を16進数にしたもの
0000.5e00.01xx
xxVRRPのグループ番号を16進数にしたもの
0007.b400.xxyy
xxはGLBPグループ番号
yyはその中でのシーケンス(01-04)

HSRP Concepts

f:id:wand_ta:20200704142231p:plain

R2(config)#interface g0/0/0
R2(config-if)#
R2(config-if)#ip address 10.0.0.200 255.255.255.0
R2(config-if)#
R2(config-if)#standby 10 ip 10.0.0.254
R2(config-if)#
R2(config-if)#no shutdown
  • PCから確認
    • 仮想IPv4アドレス10.0.0.254pingが通っている
    • 仮想MACアドレス0000.0c07.ac0aが振られている
      • HSRPグループ番号10 -> 0x0a
C:\>ping 10.0.0.254

Pinging 10.0.0.254 with 32 bytes of data:

Reply from 10.0.0.254: bytes=32 time=1ms TTL=255
Reply from 10.0.0.254: bytes=32 time<1ms TTL=255
Reply from 10.0.0.254: bytes=32 time<1ms TTL=255
Reply from 10.0.0.254: bytes=32 time<1ms TTL=255

Ping statistics for 10.0.0.254:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 0ms, Maximum = 1ms, Average = 0ms

C:\>arp -a
  Internet Address      Physical Address      Type
  10.0.0.254            0000.0c07.ac0a        dynamic
  • echoは両routerに届くが、echo-replyはactive routerからのみ帰る (今回はR2)

f:id:wand_ta:20200704142243p:plain

  • active/standby
    • priority値でどのrouterがactiveになるか決まる
      • tie-breakerは物理IPアドレス
      • 最も大きいものがactiveになる
    • 後からpriority値の高いrouterを追加しても、OSPFのDR/BDRよろしくpreemptionはおきない
    • インタフェースコンフィグでstandby <group-number> preemptを設定することでpreemptionを有効化できる

HSRP Failover

  • MACアドレスを仮想化するのでホストのARPテーブルに変更は生じない

HSRP Load Balancing

  • HSRPではVLANごとにHSRPグループを作成し、仮想IPアドレスを割り当てる
    • 各HSRPグループでpriority値を調整し、「VLAN1ではR1をactive」「VLAN2ではR2をactive」という形で負荷分散を実現できる
  • cf. GLBPは1つの仮想IPアドレスに対して複数の仮想MACアドレスARP応答し、負荷分散を実現する

Simple Network Management Protocol

  • interface, IPアドレス、バッファといった変数を標準化したデータベースを作ることで、監視や管理を簡単にするという思想のもと生まれた
  • 機器上でagentを動かし、managerから操作を行う
  • MIB: Management Information Base
    • 「変数のデータベース」
    • ツリー構造
  • NMS: Network Management Station
    • managerが動作するPCやサーバ

SNMP Variable Reading and Writing: SNMP Get and Set

SNMP Notifications: Traps and Informs

The Management Information Base

Securing SNMP

  • SNMPv3でようやく本格的なセキュリティが搭載された
    • Message integrity
    • Authentication
    • Encryption

FTP and TFTP

Managing Cisco IOS Images with FTP/TFTP

The IOS File System

Router#show file systems
File Systems:

       Size(b)       Free(b)      Type  Flags  Prefixes
*   3249049600    2761893909     flash     rw  flash:
         29688         23590     nvram     rw  nvram:
  • NVRAM: startup configが格納されている
  • FLASH: IOSなどが格納される
Router#show flash:

System flash directory:
File  Length   Name/status
  3   486899872isr4300-universalk9.16.06.04.SPA.bin
  2   28282    sigdef-category.xml
  1   227537   sigdef-default.xml
[487155691 bytes used, 2761893909 available, 3249049600 total]
3.17338e+06K bytes of processor board System flash (Read/Write)

Upgrading IOS Images

Copying a New IOS Image to a Local IOS File System Using TFTP

  • TFTPサーバからコピーする
Router#copy tftp flash
Address or name of remote host []? 10.0.0.1
Source filename []? c2960-lanbasek9-mz.150-2.SE4.bin
Destination filename [c2960-lanbasek9-mz.150-2.SE4.bin]? 

Accessing tftp://10.0.0.1/c2960-lanbasek9-mz.150-2.SE4.bin...
Loading c2960-lanbasek9-mz.150-2.SE4.bin from 10.0.0.1: !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
[OK - 4670455 bytes]

4670455 bytes copied in 0.058 secs (6473925 bytes/sec)
  • コピーしたファイルを確認
Router#show flash:

System flash directory:
File  Length   Name/status
  4   4670455  c2960-lanbasek9-mz.150-2.SE4.bin
  3   486899872isr4300-universalk9.16.06.04.SPA.bin
  2   28282    sigdef-category.xml
  1   227537   sigdef-default.xml
[491826146 bytes used, 2757223454 available, 3249049600 total]
3.17338e+06K bytes of processor board System flash (Read/Write)


Router#dir flash0:
Directory of flash:/

    4  -rw-     4670455          <no date>  c2960-lanbasek9-mz.150-2.SE4.bin
    3  -rw-   486899872          <no date>  isr4300-universalk9.16.06.04.SPA.bin
    2  -rw-       28282          <no date>  sigdef-category.xml
    1  -rw-      227537          <no date>  sigdef-default.xml

3249049600 bytes total (2757223454 bytes free)

Verifying IOS Code Integrity with MD5

Copying Images with FTP

Router#copy ?
  flash:          Copy from flash: file system
  ftp:            Copy from ftp: file system
  running-config  Copy from current system configuration
  scp:            Copy from scp: file system
  startup-config  Copy from startup configuration
  tftp:           Copy from tftp: file system
  • ftpやscpも使える

The FTP and TFTP Protocols

FTP Protocol Basics

  • over TCP
  • 認証がある

FTP Active and Passive Modes

  • 2本のTCPコネクションを用いる
  • FTP control connection
    • TCP21番
  • FTP data connection
    • TCP20番が予約されているが、client/serverともにephemeral portを使うことが多い
    • active mode
      1. clientはdata connectionに用いるephemeral portを確保する
      2. clientはcontrol connection上でserverにFTP PORTコマンドを送信し、clientのIPとephemeral portを伝える
      3. serverはclientにTCP SYNを送信し、data connectionの確立を開始する
    • passive mode
      1. clientはserverにFTP PASVコマンドを送る
      2. serverはdata connectionに用いるephemeral portを確保する
      3. serverはcontrol connection上でclientにFTP PORTコマンドを送信し、serverのIPとephemeral portを伝える
      4. clientはserverにTCP SYNを送信し、data connectionの確立を開始する
  • active mode誕生の経緯
    • TCP20番を決めて使ってしまうと、複数のFTP clientを動かせない
  • passive mode誕生の経緯
    • サーバー側からTCPコネクションを確立してしまうとファイアウォールに阻まれる
      • ランダムなEphemeral Port全部開けるわけにもいかない
    • クライアント側からTCPコネクションを確立すればファイアウォールに阻まれない

FTP over TLS (FTP Secure)

  • FTP自体に暗号化がないのでTLSで補う
    • FTP AUTHコマンド

TFTP Protocol Basics

  • Trivial File Transfer Protocol
  • over UDP
  • 認証なし

CCNA試験対策 下巻ch17: Cisco Software-Defined Access (SDA)

https://www.ciscopress.com/store/ccna-200-301-official-cert-guide-volume-2-9781587147135www.ciscopress.com


SDA Fabric, Underlay, and Overlay

  • Fabric
    • Underlayとoverlayが組み合わさった全体をさす言葉
  • Underlay
    • 物理的なL2/L3ネットワーク
  • Overlay
    • Underlayの上で構築される論理的なネットワーク
      • VXLAN tunnelなど

The SDA Underlay

Using Existing Gear for the SDA Underlay

  • brownfield design
  • ノードには役割があり、要求するスペックが公式にまとめられている
    • Fabric edge node
      • 従来のaccess switchに相当
    • Fabric border node
      • WAN edgeに相当
    • Fabric control node
      • 論理と物理の変換を担う (LISP)
      • 多くのCPUとメモリを要するので、必要なスペックも高い
  • 既存のデバイスを用いる場合、このカタログに照らす必要がある

Using New Gear for the SDA Underlay

  • greenfield design
  • カタログ通りの製品を買えばいいだけ
  • プロビジョニング自体は依然として必要なことに留意する
  • Routed Access Layer Design
    • 全SwitchをL3にするすすめ
      • STPいらず
      • FHRPもいらなくなる

The SDA Overlay

VXLAN Tunnens in the Overlay (Data Plane)

  • VXLAN: Virtual Extensible LAN
  • VXLANを用いて、Underlayの物理的なネットワークを隠蔽してOverlay上の論理的なネットワークを構築する
    • 論理的なメッセージをVXLANでカプセルし、物理的なUDP,IP,Ethernetで再度包むことになる

LISP For Overlay Discovery and Location (Control Plane)

  • EID: endpoint identifier
    • Underlay Network上のサブネット
  • RLOC: Routing Location
    • Overlay上のアドレス
  • LISP map server
    • EIDとRLOCとの対応を管理する
    • Fabric Control Node上で動作する

DNA Center and SDA Operation

Cisco DNA Center

  • Cisco DNA Center対応機器を買うとプリインストールされているアプリケーション

Cisco DNA Center and Scalable Groups

Issues with Traditional IP-Based Security

  • 従来型のACLのつらみ
    • ACE: Access Control Entryが増えてくると収集がつかなくなる
      • 複数要件をまたいだACE
      • いつなぜ追加されたかわからないACE
      • 消すに消せない

SDA Security Based on User Groups

  • SGT: Scalable Group Tag
    • Employee, Internet, Partner, Guest といった論理的なタグ付けを行う
    • タグ間でアクセス制限を行う

DNA Center as a Network Management Platform

  • Ciscoは他にもネットワーク管理プラットフォームを提供している
    • Cisco PI: Prime Infrastructure など
  • 従来の管理プラットフォームと比較したときの特徴は

DNA Center Similarities to Traditional Management

  • トポロジマップの生成・表示、直感的なGUIなどは従来と差がない

DNA Center Differences with Traditional Management

  • 最大の違い
    • Cisco DNA CenterはSDAに対応している
    • PI等、従来のものは対応していない
  • Cisco DNA Center特有の機能
    • EasyQoS
    • Encrypted traffic analysis
    • Device 360 and Client 360
      • 360 = 包括的なヘルスチェック
    • Network time travel
      • 機器の、過去のある時点でのパフォーマンスを見られる
    • Path trace

CCNA試験対策 下巻ch16: Introduction to Controller-Based Networking

https://www.ciscopress.com/store/ccna-200-301-official-cert-guide-volume-2-9781587147135www.ciscopress.com


SDN and Controller-Based Networks

The Data, Control, and Management Planes

  • ネットワークの再定義

The Data Plane

  • Data Plane: データの通信そのものに関する要素
    • Forwarding Planeとも
  • これがないとそもそも通信できない

The Control Plane

  • これがなくても通信はできるが、運用やパフォーマンスに難があるよ、というもの
    • IPルーティングプロトコル
      • ないと静的ルートを設定しないといけない
    • IPv4 ARP
    • IPv6 NDP
    • SwitchのMACアドレス学習
      • これがないとbroadcastしなければならない
    • STP
      • ないとフレームが永久に回り続ける

The Management Plane

  • これがなくても通信そのものに別に影響はないもの

Cisco Switch Data Plane Internals

  • SwitchはData Planeにあたる高負荷の処理を専用のハードウェアでこなしている
    • ASIC: application-specific integrated circuit
    • TCAM: ternary content-addressable memory
  • Control Planeにあたるものは汎用CPUで処理している

Controllers and Software-Defined Architecture

Controllers and Centralized Control

  • 従来は各デバイスで分散管理していたControl Planeを一元化する
Application
 ↓ call NBI
Controller
 ↓ call SBI
Network Devices

The Southbound Interface

  • SBI: Southbound Interface
  • 「紙面上で下側のAPI」の意
    • 図面上でネットワーク機器はコントローラよりも下側に描かれることから
  • ネットワーク機器が提供する
SBIs ACI Cisco APIC-EM Cisco SDA
OpenFlow o
OpFlex o o
CLI(SSH/Telnet) o o
SNMP o
NETCONF/RESTCONF o

The Northbound Interface

  • NBI: Northbound Interface
  • 「紙面上で上側のAPI」の意
    • 図面上でアプリケーションはコントローラよりも上側に描かれることから
  • コントローラが提供する
  • RESTful API

Examples of Network Programmability and SDN

OpenDaylight and OpenFlow

  • ONF: Open Networking Foundation
  • OpenFlow
    • ONFによりつくられたSBI
    • Switchの抽象化・標準化など

The OpenDaylight Controller

  • OpenDaylight
    • オープンソースのSDNコントローラ
    • OpenFlow含むさまざまなSBIを叩き、NBIを提供する

The Cisco Open SDN Controller (OSC)

  • OSC: Open SDN Controller
    • CiscoによるOpenDaylightの商用フォーク
  • すでに終わった製品

Cisco Application Centric Infrastructure (ACI)

ACI Physical Design: Spine and Leaf

ACI Operating Model with Intent-Based Networking

  • IBN: Intent-Based Networking
    • CiscoはOpenFlow的なSDNからIBN的な方向へ舵を切っていくことに
    • ネットワークのインフラを設定するのではなく、ネットワークやアプリケーションを設定する
  • ACI: Application Centric Infrastructure

Cisco APIC Enterprise Module

  • APIC: Application Policy Infrastructure

APIC-EM Basics

  • APIC-EM: APIC Enterprise Module
  • 古い機器を大量に抱えている大企業にもSDNを提供する
  • 古い機器でもTelnet/SSHSNMPを使ってできることはある
    • Topology Map
    • Path Trace
    • Plug and Play
      • 開梱して物理的に接続してL3疎通するまで (Day 0 Installation) を自動化
    • Easy QoS
      • QoSの設定はもっとも骨の折れる部類
      • これを簡略に行える

APIC-EM Replacement

  • APIC-EMは終わろうとしている製品
  • DNA Center (DNAC)へ移行しよう

Comparing Traditional Versus Controller-Based Networks

How Automation Impacts Network Management

  • 汎用プログラム言語でNBI (RESTful)を叩いて設定を行えるように
  • 従来型のネットワーク管理から変わったこと
    • 自動化が簡単に
      • 堅牢
    • ネットワークの再定義 -- SDN
    • インフラのデータをControllerに集約して分析することで、より意味のあるデータを得られるように
      • Path Traceなど
    • 運用コスト(時間)の削減

Comparing Traditional Networks with Controller-Based Networks

  • 従来型から変わったこと
    • 「デバイスごとの個別の設定」よりもすぐれた運用モデル
    • NBIの提供するIntent-Basedな設定
    • SBI経由の一貫した設定
    • ネットワークにもDevOpsが適用可能に
      • 「高い品質を確保しつつ、システムへの変更をコミットしてから通常の運用に移るまでの時間を短縮することを目的とした一連のプラクティス」のこと

英語

  • overt
    • 明白な
  • tidbit
    • ひと口、一片
    • 役立つ情報

CCNA試験対策 下巻ch19: Understanding Ansible, Puppet, and Chef

https://www.ciscopress.com/store/ccna-200-301-official-cert-guide-volume-2-9781587147135www.ciscopress.com


Device Configuration Challenges and Solutions

  • ネットワークエンジニアが1人で1台のデバイスを設定しているうちは configure terminal で設定して copy running-config startup-config していればよい
  • 複数人のネットワークエンジニアで数百数千のデバイスを管理するようになると破綻する

Configuration Drift

  • バイス上で手作業で直接設定を行うと、誰がいつ・どの行の・何を変更し・何を削除したか、といった情報がない
  • 一貫性のない状態になる

Centralized Configuration Files and Version Control

  • 設定ファイルを一元化する
  • 方針は2通り考えられる
    • バイス上の設定を正として、一元管理しているものはバックアップとする
    • 一元管理しているほうを正とする
  • 後者だといろいろなツールの恩恵を得られて良い
  • バージョン管理使え

Configuration Monitoring and Enforcement

  • 設定を一元管理して、バージョン管理もするようになりました
  • まだ問題がある
    • 一元管理している設定ファイルをどうやってデバイスに反映するの?
    • 人の手が介在すると、ここでも構成ドリフトのおそれが生じる
  • Configuration Monitoring, Configuration Enforcementという類のツールを使え
    • 構成ドリフトを監視する
    • 修正もしくはネットワークエンジニアに通知する

Configuration Provisioning

  • 構成管理システムで変更した設定のプロビジョン・デプロイ
  • 求められる機能
    • メイン: 一元管理している設定ファイルのデプロイ
    • デプロイ対象の管理
      • 全部
      • 一部
    • 設定変更が受理/拒否されたときのアクションの設定
    • revert
    • dry-run
    • 設定が正しく反映されていることの確認
    • running-configからstartup-configに永続化するかどうか選べる
    • 設定ファイルのテンプレート化
    • 自動デプロイ

Configuration Templates and Variables

  • 設定ファイルをテンプレート化する
    • バイスによって変える部分は変数として流し込む
    • snowflake(個別のコンフィグファイル)をやめる

Files That Control Configuration Automation

  • 構成管理システムの構成も構成ファイルで行えること

Ansible, Puppet, and Chef Basics

Ansible Puppet Chef
設定ファイル Playbook Manifest Recipe,Runlist
Protocol SSH, NETCONF HTTP (RESTful) HTTP (RESTful)
agentのインストールが必要 no yes yes
push/pull push pull pull
  • IOSにエージェントをインストールできないので、普通はansible

CCNA試験対策 下巻ch15: Cloud Architecture

https://www.ciscopress.com/store/ccna-200-301-official-cert-guide-volume-2-9781587147135www.ciscopress.com


Server Virtualization

Cisco Server Hardware

  • No KVM
    • keyboard, video display, mouseがない、の意
    • 人が前に座って操作する代物ではない
  • 人が直接操作しないので、省スペースが重要になってくる
  • データセンターのラックにはホスト(物理マシン)がぎっしり

Server Virtualization Basics

  • 今日びのハードウェアは1台で1つのOS・アプリケーションを動かすにはリソースが余りがち
  • なので、Hypervisorを動かして、その上でOSを複数動かす
    • 各OSに適切にリソースを割り当てる
  • Hypervisor製品の有名どころ

Networking with Virtual Switches on a Virtualized Host

  • virtual switch (vSwitch)
    • HypervisorやCiscoが提供
    • physical switchのphysical NICVMのvirtual NICを繋ぐ
    • VM間をVLANで接続できる
    • VLAN trunkingもできる

The Physical Data Center Network

  • 物理ホストは当然物理スイッチに接続されている必要がある
  • host - ToR SW - EoR SW というトポロジー
    • ToR: Top of Rack
      • サーバが詰まっているラックの最上段にいるswitch
      • access layerに相当
    • EoR: End of Row
      • distribution layerに相当
      • host, ToRとは別のラックに詰まってるやつ

Workflow with a Virtualized Data Center

  • 従来は、データセンターのサーバ仮想化エンジニアにお願いしてVMをセットアップしてもらっていた
  • 人が介在するので、まだ「クラウド」とはいえない

Cloud Computing Services

  • クラウド」って何?
  • NISTによる定義
    • On-demand self-service
      • データセンターのサーバ仮想化エンジニアに作業依頼するのはこの条件を満たさない
    • Broad network access
      • 様々な種類のネットワークをサポートしていること
        • インターネット含む
    • Resource pooling
    • Rapid elasticity
    • Measured service
      • 透明性と従量課金のため

Private Cloud (On-Premise)

  • NISTによる「クラウド」の定義5つを満たすサービスを、社内向けに提供する形態のこと

Public Cloud

  • 外部向けに売り出すやつ

Cloud and the "As a Service" Model

  • Infrastructure as a Service
  • Software as as Service
  • (Development) Platform as a Service

しってるので略

WAN Traffic Paths to Reach Cloud Services

  • public cloud利用時、WANの向こう側のpublic cloudにどうやって接続するか
  • 良し悪し
Internet Internet VPN MPLS VPN Ethernet WAN Intercloud Exchange
セキュリティ x o o o o
QoS x x o o o
キャパシティプランニング必要 o o o o o
クラウド乗り換えの障壁が低い o o x x o
導入の速さ o o x x x
  • Internet
    • 導入が速い、安い
    • セキュリティに難あり
    • QoSもサポートしない
    • SLAも普通ない
  • Internet VPN
    • Internetのセキュリティ面は大幅改善
    • QoSはサポートしない、SLAも普通ない
  • Private WAN (MPLS VPN, Ethernet WAN)
    • セキュア
    • QoSSLA対応
    • 回線開設に時間と費用がかかる
  • Intercloud WAN
    • Private WANの間接層としてはたらき、クラウド乗り換え障壁の問題を解消する
      • 顧客はIntercloud WANサービスへのPrivate WANを開設する
      • クラウド乗り換え時は新たにPrivate WANを開設する必要がない
    • 間に1社挟まるのがデメリット
  • いずれの場合もネットワークのキャパシティプランニングは必要である
    • 例) 「本社 - (Private WAN) - 支社 - (Internet)」 という構造の場合