|
| 1 | +# Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +# pylint: disable=invalid-name, too-few-public-methods |
| 16 | + |
| 17 | +"""DOCA OFED building block""" |
| 18 | + |
| 19 | +from __future__ import absolute_import |
| 20 | +from __future__ import unicode_literals |
| 21 | +from __future__ import print_function |
| 22 | + |
| 23 | +from packaging.version import Version |
| 24 | +import posixpath |
| 25 | + |
| 26 | +import hpccm.config |
| 27 | +import hpccm.templates.annotate |
| 28 | + |
| 29 | +from hpccm.building_blocks.base import bb_base |
| 30 | +from hpccm.building_blocks.packages import packages |
| 31 | +from hpccm.common import cpu_arch, linux_distro |
| 32 | +from hpccm.primitives.comment import comment |
| 33 | +from hpccm.primitives.label import label |
| 34 | + |
| 35 | +class doca_ofed(bb_base, hpccm.templates.annotate, hpccm.templates.rm, |
| 36 | + hpccm.templates.tar, hpccm.templates.wget): |
| 37 | + """The `doca_ofed` building block downloads and installs the [NVIDIA |
| 38 | + DOCA Software Framework](https://developer.nvidia.com/networking/doca). |
| 39 | +
|
| 40 | + # Parameters |
| 41 | +
|
| 42 | + annotate: Boolean flag to specify whether to include annotations |
| 43 | + (labels). The default is False. |
| 44 | +
|
| 45 | + archlabel: The CPU architecture label assigned by Mellanox to the |
| 46 | + package repository. The default value is `x86_64` for x86_64 |
| 47 | + processors and `arm64-sbsa` for aarch64 processors. |
| 48 | +
|
| 49 | + oslabel: The Linux distribution label assigned by Mellanox to the |
| 50 | + package repository. For Ubuntu, the default value is |
| 51 | + `ubuntuXX.04` where `XX` is derived from the base image. For |
| 52 | + RHEL-base Linux distributions, the default value is `rhelX.Y` |
| 53 | + where `X.Y` is `9.2` for RHEL 9.x and `8.6` for RHEL 8.x. |
| 54 | +
|
| 55 | + ospackages: List of OS packages to install prior to installing |
| 56 | + DOCA OFED. The default values are `ca-certificates`, `gnupg`, and |
| 57 | + `wget`. |
| 58 | +
|
| 59 | + packages: List of packages to install from Mellanox OFED. For |
| 60 | + Ubuntu, the default values are `ibverbs-providers`, |
| 61 | + `ibverbs-utils` `libibmad-dev`, `libibmad5`, `libibumad3`, |
| 62 | + `libibumad-dev`, `libibverbs-dev` `libibverbs1`, `librdmacm-dev`, |
| 63 | + and `librdmacm1`. For RHEL-based Linux distributions, the default |
| 64 | + values are `libibumad`, `libibverbs`, `libibverbs-utils`, |
| 65 | + `librdmacm`, `rdma-core`, and `rdma-core-devel`. |
| 66 | +
|
| 67 | + version: The version of DOCA OFED to download. The default value |
| 68 | + is `2.10.0`. |
| 69 | +
|
| 70 | + # Examples |
| 71 | +
|
| 72 | + ```python |
| 73 | + doca_ofed(version='2.10.0') |
| 74 | + ``` |
| 75 | +
|
| 76 | + """ |
| 77 | + |
| 78 | + def __init__(self, **kwargs): |
| 79 | + """Initialize building block""" |
| 80 | + |
| 81 | + super(doca_ofed, self).__init__(**kwargs) |
| 82 | + |
| 83 | + self.__archlabel = kwargs.get('archlabel', '') # Filled in by __cpu_arch |
| 84 | + self.__key = 'https://linux.mellanox.com/public/repo/doca/GPG-KEY-Mellanox.pub' |
| 85 | + self.__oslabel = kwargs.get('oslabel', '') # Filled in by __distro |
| 86 | + self.__ospackages = kwargs.get('ospackages', |
| 87 | + ['ca-certificates', 'gnupg', 'wget']) |
| 88 | + self.__packages = kwargs.get('packages', []) # Filled in by __distro |
| 89 | + self.__version = kwargs.get('version', '2.10.0') |
| 90 | + |
| 91 | + # Add annotation |
| 92 | + self.add_annotation('version', self.__version) |
| 93 | + |
| 94 | + # Set the CPU architecture specific parameters |
| 95 | + self.__cpu_arch() |
| 96 | + |
| 97 | + # Set the Linux distribution specific parameters |
| 98 | + self.__distro() |
| 99 | + |
| 100 | + # Fill in container instructions |
| 101 | + self.__instructions() |
| 102 | + |
| 103 | + def __instructions(self): |
| 104 | + """Fill in container instructions""" |
| 105 | + |
| 106 | + self += comment('DOCA OFED version {}'.format(self.__version)) |
| 107 | + |
| 108 | + self += packages(ospackages=self.__ospackages) |
| 109 | + |
| 110 | + self += packages( |
| 111 | + apt_keys=[self.__key], |
| 112 | + apt_repositories=['deb [signed-by=/usr/share/keyrings/{3}] https://linux.mellanox.com/public/repo/doca/{0}/{1}/{2}/ ./'.format(self.__version, self.__oslabel, self.__archlabel, posixpath.basename(self.__key).replace('.pub', '.gpg'))], |
| 113 | + ospackages=self.__packages, |
| 114 | + yum_keys=[self.__key], |
| 115 | + yum_repositories=['https://linux.mellanox.com/public/repo/doca/{0}/{1}/{2}'.format(self.__version, self.__oslabel, self.__archlabel)]) |
| 116 | + |
| 117 | + self += label(metadata=self.annotate_step()) |
| 118 | + |
| 119 | + def __cpu_arch(self): |
| 120 | + """Based on the CPU architecture, set values accordingly. A user |
| 121 | + specified value overrides any defaults.""" |
| 122 | + |
| 123 | + if not self.__archlabel: |
| 124 | + if hpccm.config.g_cpu_arch == cpu_arch.AARCH64: |
| 125 | + self.__archlabel = 'arm64-sbsa' |
| 126 | + elif hpccm.config.g_cpu_arch == cpu_arch.X86_64: |
| 127 | + self.__archlabel = 'x86_64' |
| 128 | + else: # pragma: no cover |
| 129 | + raise RuntimeError('Unknown CPU architecture') |
| 130 | + |
| 131 | + def __distro(self): |
| 132 | + """Based on the Linux distribution, set values accordingly. A user |
| 133 | + specified value overrides any defaults.""" |
| 134 | + |
| 135 | + if hpccm.config.g_linux_distro == linux_distro.UBUNTU: |
| 136 | + if not self.__oslabel: |
| 137 | + if hpccm.config.g_linux_version >= Version('24.0'): |
| 138 | + self.__oslabel = 'ubuntu24.04' |
| 139 | + elif hpccm.config.g_linux_version >= Version('22.0'): |
| 140 | + self.__oslabel = 'ubuntu22.04' |
| 141 | + else: |
| 142 | + self.__oslabel = 'ubuntu20.04' |
| 143 | + |
| 144 | + if not self.__packages: |
| 145 | + self.__packages = ['libibverbs1', 'libibverbs-dev', |
| 146 | + 'ibverbs-providers', 'ibverbs-utils', |
| 147 | + 'libibmad5', 'libibmad-dev', |
| 148 | + 'libibumad3', 'libibumad-dev', |
| 149 | + 'librdmacm-dev', 'librdmacm1'] |
| 150 | + |
| 151 | + elif hpccm.config.g_linux_distro == linux_distro.CENTOS: |
| 152 | + if not self.__oslabel: |
| 153 | + if hpccm.config.g_linux_version >= Version('9.0'): |
| 154 | + self.__oslabel = 'rhel9.2' |
| 155 | + else: |
| 156 | + self.__oslabel = 'rhel8.6' |
| 157 | + |
| 158 | + if not self.__packages: |
| 159 | + self.__packages = ['libibverbs', 'libibverbs-utils', |
| 160 | + 'libibumad', 'librdmacm', |
| 161 | + 'rdma-core', 'rdma-core-devel'] |
| 162 | + |
| 163 | + else: # pragma: no cover |
| 164 | + raise RuntimeError('Unknown Linux distribution') |
| 165 | + |
| 166 | + def runtime(self, _from='0'): |
| 167 | + """Generate the set of instructions to install the runtime specific |
| 168 | + components from a build in a previous stage. |
| 169 | +
|
| 170 | + # Examples |
| 171 | +
|
| 172 | + ```python |
| 173 | + d = doca_ofed(...) |
| 174 | + Stage0 += d |
| 175 | + Stage1 += d.runtime() |
| 176 | + ``` |
| 177 | + """ |
| 178 | + |
| 179 | + return str(self) |
0 commit comments